我以前曾在我的计算机上运行过 GUI,但通过这个程序,我想尝试实现 GridBag,这样我就可以制作一个简单的游戏。我不知道为什么它没有运行。这是代码:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class GridBagTest extends JFrame{
public static void main(String[] args){
new GridBagTest();
}
public void GridBagTest(){
JButton atk, mag, fort, pot1, pot2, flee;
JPanel gamePanel = new JPanel();
gamePanel.setLayout(new GridBagLayout());
JFrame gameFrame = new JFrame("FightQuest");
gameFrame.getContentPane().add(gamePanel);
gameFrame.setSize(800, 600);
gameFrame.pack();
gameFrame.setVisible(true);
atk = new JButton("Strike");
mag = new JButton("Magic");
fort = new JButton("Fortify");
pot1 = new JButton("Potion 1");
pot2 = new JButton("Potion 2");
flee = new JButton("Flee");
addItem(gamePanel, atk, 0, 0, 1, 1, GridBagConstraints.SOUTHEAST);
addItem(gamePanel, mag, 1, 0, 1, 1, GridBagConstraints.SOUTH);
addItem(gamePanel, fort, 2, 0, 1, 1, GridBagConstraints.SOUTHWEST);
addItem(gamePanel, pot1, 0, 1, 1, 1, GridBagConstraints.NORTHEAST);
addItem(gamePanel, pot2, 1, 1, 1, 1, GridBagConstraints.NORTH);
addItem(gamePanel, flee, 2, 1, 1, 1, GridBagConstraints.NORTHWEST);
}
private void addItem(JPanel p, JComponent c, int x, int y, int width, int height, int align){
GridBagConstraints gc = new GridBagConstraints();
gc.gridx = x;
gc.gridy = y;
gc.gridwidth = width;
gc.gridheight = height;
gc.weightx = 100.0;
gc.weighty = 100.0;
gc.insets = new Insets(0, 0, 0, 0);
gc.anchor = align;
gc.fill = GridBagConstraints.NONE;
p.add(c, gc);
}
}
我不知道这是否有什么区别,但我从 Java 6 的 Java 引用书中获得了大部分代码,尽管我运行的是 Java 7,因为这是我学校的全部代码。我还在 XFCE 操作系统上编写所有代码。
最佳答案
更改此行
public void GridBagTest()
至
public GridBagTest()
构造函数没有返回类型。此外,您还应该在将组件添加到容器后调用 pack()
和 setVisible(true)
来调整和显示组件的大小。
另请注意,在这种情况下不需要扩展。
改变
public class GridBagTest extends JFrame
至
public class GridBagTest{
代码中显示的更改:
public class GridBagTest{
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new GridBagTest();
}
});
}
public GridBagTest(){
JButton atk, mag, fort, pot1, pot2, flee;
JPanel gamePanel = new JPanel();
gamePanel.setLayout(new GridBagLayout());
JFrame gameFrame = new JFrame("FightQuest");
gameFrame.getContentPane().add(gamePanel);
gameFrame.setSize(800, 600);
atk = new JButton("Strike");
mag = new JButton("Magic");
fort = new JButton("Fortify");
pot1 = new JButton("Potion 1");
pot2 = new JButton("Potion 2");
flee = new JButton("Flee");
addItem(gamePanel, atk, 0, 0, 1, 1, GridBagConstraints.SOUTHEAST);
addItem(gamePanel, mag, 1, 0, 1, 1, GridBagConstraints.SOUTH);
addItem(gamePanel, fort, 2, 0, 1, 1, GridBagConstraints.SOUTHWEST);
addItem(gamePanel, pot1, 0, 1, 1, 1, GridBagConstraints.NORTHEAST);
addItem(gamePanel, pot2, 1, 1, 1, 1, GridBagConstraints.NORTH);
addItem(gamePanel, flee, 2, 1, 1, 1, GridBagConstraints.NORTHWEST);
gameFrame.pack();
gameFrame.setVisible(true);
}
private void addItem(JPanel p, JComponent c, int x, int y, int width, int height, int align){
GridBagConstraints gc = new GridBagConstraints();
gc.gridx = x;
gc.gridy = y;
gc.gridwidth = width;
gc.gridheight = height;
gc.weightx = 100.0;
gc.weighty = 100.0;
gc.insets = new Insets(0, 0, 0, 0);
gc.anchor = align;
gc.fill = GridBagConstraints.NONE;
p.add(c, gc);
}
}
你会得到你的输出:
关于java - GUI 未显示在我的屏幕上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20478125/