2013-12-09 81 views
0

我已经让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参考书的Java 6即使我正在运行的Java 7,因为这是所有我的学校了。我也在XFCE操作系统上执行我所有的代码。

+2

1)为了更好地帮助更快,发布[SSCCE](http://sscce.org/)。 2)请参阅[使用多个JFrames,好/坏实践?](http://stackoverflow.com/a/9554657/418556) –

+0

我不知道如何使这个更短,从代码中删除的东西doesn'使GUI出现。我需要其他人告诉我如何缩短或延长代码。 – JavaNoob

+1

非常恭喜,你完全掌握了四个制作SSCCE的东西中的一个。现在看看***另一个3. *** –

回答

1

改变这一行

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); 
    } 
} 

,你会得到你的输出:

enter image description here

+0

工作。非常感谢。 – JavaNoob

+0

@JavaNoob看到更新! – nachokk

0

不确定这是否是您的问题,但您拨打gameFrame.setSize(800, 600);gameFrame.pack();似乎有冲突,因为pack()将根据帧的当前内容重新设置大小。 (在这点上,你叫pack(),什么也没有)

顺便说一句,你还应该创建和更新你的Swing组件在事件派发线程上。

+0

我从我的代码中删除了pack(),但这并没有让GUI显示出来。 – JavaNoob

0

查看这样的问题时,我首先要看的是应用程序的启动方式。请注意,you are not properly launching your GUI。您应该确保GUI在EDT(Event Dispatch Thread)上启动。

以下是如何启动特定GUI的示例。

SSCCE:

import javax.swing.*; 
import java.awt.*; 

/** 
* http://stackoverflow.com/questions/20478125/gui-not-showing-on-my-screen/20478279#20478279 
*/ 
public class Q20478125 { 
    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       new Q20478125(); 
      } 
     }); 
    } 

    public Q20478125() { 
     JButton atk, mag, fort, pot1, pot2, flee; 
     JPanel gamePanel = new JPanel(); 
     gamePanel.setLayout(new GridBagLayout()); 
     gamePanel.setPreferredSize(new Dimension(800, 600)); 

     JFrame gameFrame = new JFrame("FightQuest"); 
     gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     gameFrame.getContentPane().add(gamePanel); 
     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); 
    } 
} 
0

您正在制作一个psudo构造函数,用于在GridBagTest()之前放置void,它指定GridBagTest()是一个函数而非构造函数。所以请致电:

new GridBagTest().GridBagTest(); 

里面的主要功能。这显然你不应该做的事:而除去返回类型void

public GridBagTest(){ //<--- void is removed 


     JFrame gameFrame = new JFrame("FightQuest"); 
     // other code 
} 

但是,使用SwingUtilities.invokeLater(Runnable)通过其提交到EventQueue中总是开始从EDT程序:

SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 

       new GridBagTest(); 
      } 
     });