【发布时间】:2014-04-01 22:27:14
【问题描述】:
我有一个主 JFrame(屏幕)和单独的 JPanel,它们都包含在它们自己的方法中。主方法调用另一个托管 JFrame 的方法(创建),后者将所有其他 JPanel 添加到其中。我的问题是只显示了其中的 1 个 JPanel,每次运行代码时它都会发生变化。我尝试过拥有 1 个主 JPanel,并且这些方法将它们的组件(JLables ...)添加到其中,但我遇到了同样的问题。那么我怎样才能有多种方法并同时在一个窗口上显示。我相信我可以把它全部放在一种方法中,但这会很长。虽然不是现在,但我会有很多方法都可以将自己独特的东西放在窗户上。请在下面查看我当前的代码:
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;
}
}
谢谢!!!
【问题讨论】:
标签: java user-interface methods jframe jpanel