2013-12-09 131 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(); 
      } 
     });