【发布时间】:2021-07-06 07:43:17
【问题描述】:
我正在尝试制作一个扫雷游戏,它为我们可以点击的笑脸图标和必须点击才能播放的按钮提供不同的空间。
public final class testFrame extends JFrame implements MouseListener, ActionListener {
private JFrame screen = null;
private JPanel composite = new JPanel();
public testFrame() {
screen = new JFrame();
screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
screen.setVisible(true);
screen.setResizable(true);
composite.setLayout(new BorderLayout());
//this button is not showing also
JButton button = new JButton("Text goes here");
composite.add(button);
Container cp = screen.getContentPane(); // JFrame's content-pane
cp.setLayout(new GridLayout(5, 5, 2, 2)); // in 10x10 GridLayout
//codes to add buttons
}
所以我在这里尝试将容器 cp 添加到屏幕上。但是开了 two screen
对不起,如果这看起来是小事,但我对这个 java GUI 真的很陌生,所以请帮助我。
编辑: 我删除了扩展 JFrame 并改用了屏幕。它有点工作,但我似乎无法将容器 cp 放到面板上。要求是我必须使用容器 cp。所以我无法改变。谢谢
public final class TestFrame implements MouseListener, ActionListener {
private JFrame screen = null;
private JPanel composite = new JPanel();
private JPanel topPanel = new JPanel();
public TestFrame() {
screen = new JFrame("TestFrame");
screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
screen.setVisible(true);
topPanel.setLayout(new BorderLayout());
//composite.setLayout(new BorderLayout());
//button in topPanel
JButton button = new JButton("Text goes here");
topPanel.add(button, BorderLayout.PAGE_START);
//Content Pane
Container cp = screen.getContentPane();// JFrame's content-pane
cp.setLayout(new GridLayout(5, 5, 2, 2)); // in 10x10 GridLayout
//composite.add(cp, BorderLayout.CENTER);
screen.add(topPanel);
// screen.add(composite);
}
现在看起来像 this
【问题讨论】:
标签: java swing user-interface jframe