2014-04-01 48 views
0

我有一個主JFrame(屏幕)和單獨的JPanels,它們都包含在它們自己的方法中。主方法調用另一個承載JFrame的方法(create),後者將所有其他JPanel添加到它。我的問題是這些JPanel中只有一個顯示,每次運行代碼時它都會更改。我試過有1個主JPanel和方法添加它們的組件(JLables ...),但我得到同樣的問題。那麼我怎麼能有多種方法,並同時在1個窗口中顯示全部。我相信我可以全部用1種方法,但這會變得很長。雖然不是現在,但我會有很多方法來把自己獨特的東西放在窗口上。請看我目前的代碼如下:在單個JFrame上顯示來自不同方法的多個JPanel

import java.awt.Frame; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.util.ArrayList; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

public class GUI extends JPanel 
{ 
/** 
* Creation of variables used throughout the GUI Class. 
*/ 
private static final long serialVersionUID = 1L; 
private int lookReplyX = 5; 
private int lookReplyY = 5; 


public static void main(String[] args) 
{ 
    GUI g = new GUI(); 

    g.create(); 
} 

private void create() 
{ 
    JFrame screen = new JFrame("Dungeon of Doom"); 
    screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    //set size to full screen. 
    screen.setExtendedState(Frame.MAXIMIZED_BOTH); 

    screen.setVisible(true); 

    //Add all JPanes to screen 
    screen.getContentPane().add(lookReplyGUI()); 
    screen.getContentPane().add(titlePanel()); 
} 

private JPanel lookReplyGUI() 
{ 
    JPanel lookReplyGUIPanel = new JPanel(new GridBagLayout()); 
    GridBagConstraints c = new GridBagConstraints(); 

    for(int y = lookReplyY; y>0; y--) 
    { 
     for(int x = lookReplyX; x>0; x--) 
     { 
      JLabel lookx = new JLabel(" " + y + "" + x); 
      c.gridwidth = 1; 
      c.gridheight = 1; 
      c.gridx = x+1; 
      c.gridy = y+1; 
      lookReplyGUIPanel.add(lookx, c); 
     } 
    } 
    return lookReplyGUIPanel; 
} 

private JPanel titlePanel() 
{ 
    JPanel titlePanel = new JPanel(new GridBagLayout()); 
    GridBagConstraints c = new GridBagConstraints(); 

    JLabel title = new JLabel("DOD"); 
    titlePanel.add(title, c); 
    c.gridwidth = 7; 
    c.gridheight = 1; 
    c.gridx = lookReplyX+2; 
    c.gridy = 1; 


    return titlePanel; 
} 

} 

謝謝!

回答

2

問題的主要原因是JFrame默認使用BorderLayout這意味着一次只有一個組件可能佔用五個可用位置中的任何一個。

默認位置是CENTER。通過這樣做......

screen.getContentPane().add(lookReplyGUI()); 
screen.getContentPane().add(titlePanel()); 

lookRelyGUI成分能有效不可見(它的大小和位置都設置爲0x0)。

相反,嘗試使用不同的佈局管理器...

JFrame screen = new JFrame("Dungeon of Doom"); 
screen.setLayout(new GridLayout(2, 0)); 
screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

//set size to full screen. 
screen.setExtendedState(Frame.MAXIMIZED_BOTH); 

//Add all JPanes to screen 
screen.getContentPane().add(lookReplyGUI()); 
screen.getContentPane().add(titlePanel()); 

// Do this only when you're ready 
screen.setVisible(true); 
+0

感謝,這就是工作! ,非常感謝 – pokeairguy

+0

我很高興它幫助 – MadProgrammer

0

你需要做的就是將一個jPanel添加到整個班級使用的變量中。

private static final long serialVersionUID = 1L; 
private int lookReplyX = 5; 
private int lookReplyY = 5; 
private jPanel allPanels = new jPanel; 

然後把所有的面板是一個內部的,並顯示一個...

0

你可以保持你的代碼中使用。你只看到一個小組的原因是因爲第二個小組被第一個小組覆蓋。在默認情況下,您沒有設置大小。爲了解決這個問題,你加

private JPanel lookReplyGUI() 
     { ... 
      JLabel lookx = new JLabel(" " + y + "" + x); 
      c.gridwidth = 1; 
      c.gridheight = 1; 
      c.gridx = x+1; 
      c.gridy = y+1; 
      lookReplyGUIPanel.add(lookx, c); 
      lookReplyGUIPanel.setSize(100,100); //this line for example 
     } 
    ... 

private JPanel titlePanel(){ 
... 
    JLabel title = new JLabel("DOD"); 
    titlePanel.add(title, c); 
    titlePanel.setSize(25,50); //this line for example 
... 

然後你可以adjuest面板位置像你這樣設置大小爲好。

+0

假設該lookRelpyGUI加入到使用佈局管理器的容器,無,的setSize不會有任何效果 – MadProgrammer

相關問題