java GUI多按钮输出显示错误?
java GUI multiple buttons output display error?
我是 Java 的初学者。这是我的第一个项目。
每次我 运行 代码时,代码的 GUI 都会不断变化。
有时输出甚至没有完全加载。
这是用于初始化棋盘 8X8 jbuttons 的代码。
我已经放下图片并检查下面的超链接。
是否有任何解决方案每次执行代码都显示相同的输出?
package chess;
import game.*;
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.*;
public class board{
static JButton [][] spots =new JButton [8][8];
public static void main(String[] args){
board b =new board();
b.initializeboard(spots);
}
public void initializeboard(JButton [][] spots){
JFrame f = new JFrame("CHESS");
f.setVisible(true);
f.setSize(800,800);
GridLayout layout =new GridLayout(8,8,1,1);
f.setLayout(layout);
for(int ver=0;ver<8;ver++){
for(int hor=0;hor<8;hor++){
JButton button = new JButton();
if((ver+hor)%2==0){
button.setBackground(Color.WHITE); }
else{
button.setBackground(new Color(255,205,51)); }
pieces p =new pieces();
spots[ver][hor] = button;
p.setButton(button);
f.add(button);
}
}
} //initialize board
} // close board
Improper Execution
Correct Execution
Incomplete Execution
I am beginner in Java.
首先,class 名称应该以大写字符开头。你有没有见过 JDK 中的 class 不是以大写字符开头的?通过示例学习教科书或教程中的代码。
Is there any solution that shows the same output every time the code executes?
在使框架可见之前,应将所有组件添加到框架中。
当框架可见时,将调用布局管理器并为组件提供 size/location。如果将组件添加到可见面板,则需要在面板上调用 revalidate()
和 repaint()
以确保调用布局管理器。
必须承认,我不确定您为什么会出现这种随机行为。一些组件正在获得 size/location 而其他组件则没有,即使未调用布局管理器。
我建议您重构代码,例如:
JPanel chessboard = new JPanel( new GridLayout(8, 8, 1, 1) );
// add buttons to the panel
JFrame frame = new JFrame("CHESS")
frame.add(chessboard, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible( true );
其他评论:
- 不要设置边框的大小。使用 800 x 800 不会使每个按钮成为 100 x 100。框架大小还包括标题栏和边框,因此每个按钮的大小都将小于您的预期。
相反,您可以在循环之外创建一个变量:
Dimension buttonSize = new Dimension(100, 100)
然后在创建按钮时使用:
button.setPreferredSize( buttonSize );
现在,当调用 pack() 方法时,会将框架调整为添加到框架的所有组件的首选大小。
所有 Swing 组件都应在 Event Dispatch Thread (EDT)
上创建。阅读 Swing 教程中的部分 How to Make Frames。 FrameDemo.java
代码向您展示了一种构造 class 的方法,以便使用 invokeLater(…)
方法确保代码在 EDT 上执行。
不要让你的变量静态化。这表明 class 设计不正确。查看 How to Use Menus 中的 MenuLook.java
示例,了解稍微不同的设计,其中您的 ChessBoard
成为另一个 class 中创建的组件。然后,您可以在 class.
中定义实例变量
我是 Java 的初学者。这是我的第一个项目。 每次我 运行 代码时,代码的 GUI 都会不断变化。 有时输出甚至没有完全加载。 这是用于初始化棋盘 8X8 jbuttons 的代码。
我已经放下图片并检查下面的超链接。
是否有任何解决方案每次执行代码都显示相同的输出?
package chess;
import game.*;
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.*;
public class board{
static JButton [][] spots =new JButton [8][8];
public static void main(String[] args){
board b =new board();
b.initializeboard(spots);
}
public void initializeboard(JButton [][] spots){
JFrame f = new JFrame("CHESS");
f.setVisible(true);
f.setSize(800,800);
GridLayout layout =new GridLayout(8,8,1,1);
f.setLayout(layout);
for(int ver=0;ver<8;ver++){
for(int hor=0;hor<8;hor++){
JButton button = new JButton();
if((ver+hor)%2==0){
button.setBackground(Color.WHITE); }
else{
button.setBackground(new Color(255,205,51)); }
pieces p =new pieces();
spots[ver][hor] = button;
p.setButton(button);
f.add(button);
}
}
} //initialize board
} // close board
Improper Execution
Correct Execution
Incomplete Execution
I am beginner in Java.
首先,class 名称应该以大写字符开头。你有没有见过 JDK 中的 class 不是以大写字符开头的?通过示例学习教科书或教程中的代码。
Is there any solution that shows the same output every time the code executes?
在使框架可见之前,应将所有组件添加到框架中。
当框架可见时,将调用布局管理器并为组件提供 size/location。如果将组件添加到可见面板,则需要在面板上调用 revalidate()
和 repaint()
以确保调用布局管理器。
必须承认,我不确定您为什么会出现这种随机行为。一些组件正在获得 size/location 而其他组件则没有,即使未调用布局管理器。
我建议您重构代码,例如:
JPanel chessboard = new JPanel( new GridLayout(8, 8, 1, 1) );
// add buttons to the panel
JFrame frame = new JFrame("CHESS")
frame.add(chessboard, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible( true );
其他评论:
- 不要设置边框的大小。使用 800 x 800 不会使每个按钮成为 100 x 100。框架大小还包括标题栏和边框,因此每个按钮的大小都将小于您的预期。
相反,您可以在循环之外创建一个变量:
Dimension buttonSize = new Dimension(100, 100)
然后在创建按钮时使用:
button.setPreferredSize( buttonSize );
现在,当调用 pack() 方法时,会将框架调整为添加到框架的所有组件的首选大小。
所有 Swing 组件都应在
Event Dispatch Thread (EDT)
上创建。阅读 Swing 教程中的部分 How to Make Frames。FrameDemo.java
代码向您展示了一种构造 class 的方法,以便使用invokeLater(…)
方法确保代码在 EDT 上执行。不要让你的变量静态化。这表明 class 设计不正确。查看 How to Use Menus 中的
MenuLook.java
示例,了解稍微不同的设计,其中您的ChessBoard
成为另一个 class 中创建的组件。然后,您可以在 class. 中定义实例变量