2014-04-01 21 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

相关问题