不是所有的按钮都显示在Java中吗?
当我使用eclipse运行我的程序时,只会显示一个按钮(左上角),但当我在终端中使用javac时(大多数情况下),所有按钮都会显示!我真的很烦。有人能帮忙吗?谢谢不是所有的按钮都显示在Java中吗?,java,eclipse,macos,swing,jbutton,Java,Eclipse,Macos,Swing,Jbutton,当我使用eclipse运行我的程序时,只会显示一个按钮(左上角),但当我在终端中使用javac时(大多数情况下),所有按钮都会显示!我真的很烦。有人能帮忙吗?谢谢这是我的构造函数: public TicTacToe(){ super("Farm Tic-Tac-Toe"); setSize(450,750); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Con
这是我的构造函数:
public TicTacToe(){
super("Farm Tic-Tac-Toe");
setSize(450,750);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container cont = getContentPane();
cont.setLayout(null);
int newLine = 0;
int lineCount = 0;
for(int i = 0; i < buttons.length; i++){
buttons[ i] = new JButton(blank);
if(i == 3 || i == 6){
newLine++;
lineCount = 0;
}
buttons[ i].setBounds(lineCount*150,newLine*150,150,150);
cont.add(buttons[ i]);
buttons[ i].addActionListener(this);
lineCount++;
}
}
public TicTacToe(){
超级(“农场Tic Tac Toe”);
设置大小(450750);
setVisible(真);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container=getContentPane();
cont.setLayout(空);
int newLine=0;
int lineCount=0;
对于(int i=0;i
这是行动听众… public void actionPerformed(ActionEvent e){
for(int i = 0; i < buttons.length; i++){
if(e.getSource()==buttons[ i]){
if(turn%2==0){
buttons[ i].setName("x");
buttons[ i].setIcon(x);
buttons[ i].removeActionListener(this);
}
else{
buttons[ i].setName("o");
buttons[ i].setIcon(o);
}
buttons[ i].removeActionListener(this);
}
}
turn++;
checkWin();
}
public void actionPerformed(ActionEvent e){
对于(int i=0;i
请不要告诉我太多关于我的代码设计是如何糟糕的,因为我(不是初学者,但)不太擅长Java。Eclipse GUI只呈现以特定方式绘制的按钮。如果您的代码做得不同(例如,使用循环),Eclipse将无法绘制它
另外,请使用
LayoutManager
,不要执行类似.setLayout(null)
的操作,Eclipse GUI仅呈现以特定方式绘制的按钮。如果您的代码做得不同(例如,使用循环),Eclipse将无法绘制它
另外,在将所有组件添加到GUI之前,请使用
LayoutManager
,不要执行.setLayout(null)
调用setVisible(true)
之类的操作,因此它不会显示所有组件。不要这样做。而是在添加所有组件后调用setVisible(true)
也
- 正如许多人所建议的,不要使用空布局,而是要了解和使用布局管理器
- 是的,再买一本书
setVisible(true)
,因此它不会显示所有组件。不要这样做。而是在添加所有组件后调用setVisible(true)
也
- 正如许多人所建议的,不要使用空布局,而是要了解和使用布局管理器
- 是的,再买一本书
- 这个问题的解决方法非常简单
第一个是缺少布局管理器,另一个是显示UI的顺序(如前所述)
公共类simplectictoe{
公共静态void main(字符串[]args){
新的simplectictoe();
}
公营机构{
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}捕获(ClassNotFoundException ex){
}catch(实例化异常){
}捕获(非法访问例外){
}捕获(无支持的LookandFeelexception ex){
}
JFrame=新JFrame(“测试”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(新的BorderLayout());
frame.add(新游戏窗格());
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
});
}
公共类游戏窗格扩展了JPanel{
公共游戏窗格(){
setLayout(新的GridLayout(3,3));
对于(int-index=0;index<9;index++){
添加(新的JButton());
}
}
}
}
花点时间通读一遍,掌握基本知识。这个问题的解决方法非常简单 第一个是缺少布局管理器,另一个是显示UI的顺序(如前所述)
公共类simplectictoe{
公共静态void main(字符串[]args){
新的simplectictoe();
}
公营机构{
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}捕获(ClassNotFoundException ex){
}catch(实例化异常){
}捕获(非法访问例外){
}捕获(无支持的LookandFeelexception ex){
}
JFrame=新JFrame(“测试”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(新的BorderLayout());
frame.add(新游戏窗格());
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
});
}
公共类游戏窗格扩展了JPanel{
公共游戏窗格(){
setLayout(新的GridLayout(3,3));
对于(int-index=0;index<9;index++){
添加(新的JButton());
}
}
}
}
花点时间通读一遍,掌握基本知识。我停下来
public class SimpleTicTacToe {
public static void main(String[] args) {
new SimpleTicTacToe();
}
public SimpleTicTacToe() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new GamePane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class GamePane extends JPanel {
public GamePane() {
setLayout(new GridLayout(3, 3));
for (int index = 0; index < 9; index++) {
add(new JButton());
}
}
}
}