在单个JFrame上显示来自不同方法的多个JPanel

时间:2014-04-01 22:27:14

标签: java user-interface methods jframe jpanel

我有一个主JFrame(屏幕)和单独的JPanels都包含在他们自己的方法中。 main方法调用另一个承载JFrame的方法(create),后者将所有其他JPanel添加到它。我的问题是只显示了其中一个JPanel,每次运行代码时它都会改变。我尝试过一个主要的JPanel,并且方法添加了它们的组件(JLables ......),但是我遇到了同样的问题。那么我怎样才能有多种方法并同时在1个窗口上显示所有方法。我相信我可以把它全部放在1种方法中,但这会很长。虽然不是现在,但我会有很多方法将它们自己独特的东西放在窗口上。请参阅下面的我当前的代码:

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

}

感谢!!!

3 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

您需要做的就是为整个班级中使用的变量添加一个jPanel。

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

然后将所有面板放在那个面板中并显示那个面板......

答案 2 :(得分: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
...

然后你也可以调整面板位置,就像设置尺寸一样。

相关问题