java 学习 GUI - setContentPane() 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35821071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 00:37:54  来源:igfitidea点击:

Learning GUIs - setContentPane() method

javaswinguser-interface

提问by javapalava

I'm learning GUI programming in java, and trying to modify an existing programme to add a vertical left-hand panel containing buttons to my main frame.

我正在学习 Java 中的 GUI 编程,并尝试修改现有程序以将包含按钮的垂直左侧面板添加到我的主框架。

The main method is below. Currently, the MainPanel class extends JPanel and contains the main components of the programme (a basic game). I want to create a new panel to the right of the game.

主要方法如下。目前,MainPanel 类扩展了 JPanel 并包含程序(基本游戏)的主要组件。我想在游戏右侧创建一个新面板。

public static void main(String[] args) {
        JFrame frame = new JFrame("Sokuban");
        MainPanel panel = new MainPanel();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
        frame.setContentPane(panel);
        frame.pack();
        frame.setVisible(true);

}

I understand (I think!) how to add the buttons - I'm not stuck there.

我明白(我想!)如何添加按钮 - 我没有被困在那里。

I've tried things like:

我试过这样的事情:

    frame.add(new MyPanel(), BorderLayout.NORTH);

(MyPanel() being a new class that extends JPanel)

(MyPanel() 是一个扩展 JPanel 的新类)

        window.setContentPane(panel, BoxLayout.X_AXIS);

(wouldn't compile)

(不会编译)

What I am stuck with is a) whether I should try to create two JPanels contained within my JFrame, and then position them side by side using BoxLayout (??). Or, b) whether I should create a second JPanel to sit within the MainPanel, and somehow rearrange it there?

我遇到的问题是 a) 我是否应该尝试创建包含在 JFrame 中的两个 JPanel,然后使用 BoxLayout (??) 并排放置它们。或者,b)我是否应该创建第二个 JPanel 以放置在 MainPanel 中,并以某种方式将其重新排列在那里?

Also, despite reading the literature, I don't understand what the setContentPane() method is doing. Any pointers would be very much appreciated.

此外,尽管阅读了文献,但我不明白 setContentPane() 方法在做什么。任何指针将不胜感激。

回答by Valette_Renoux

By default JFrame already has a JPanel as a ContentPane, which is using BorderLayout. So you can just do:

默认情况下,JFrame 已经有一个 JPanel 作为 ContentPane,它使用 BorderLayout。所以你可以这样做:

frame.add(new ButtonPanel(), BorderLayout.EAST);
frame.add(new MainPanel(), BorderLayout.CENTER);

The JFrame setLayout() and add() method are really just shortcuts for jFrame.getContentPane.setLayout() and jFrame.getContentPane.add(). So in your code, you were setting the layout of the default content panel (a JPanel), just before replacing it with your MainPanel, so it had no effect.

JFrame setLayout() 和 add() 方法实际上只是 jFrame.getContentPane.setLayout() 和 jFrame.getContentPane.add() 的快捷方式。所以在你的代码中,你设置了默认内容面板(一个 JPanel)的布局,就在用 MainPanel 替换它之前,所以它没有效果。

The setContentPane() method allows you to replace the content panel of the JFrame, but it is rarely necessary.

setContentPane() 方法允许您替换 JFrame 的内容面板,但很少需要。

回答by Valette_Renoux

I want to create a new panel to the right of the game.

我想在游戏右侧创建一个新面板。

No need to write this:

不需要写这个:

frame.setLayout(new BorderLayout());
frame.setContentPane(panel);

You can simply use these code snippet:

您可以简单地使用这些代码片段:

frame.getContentPane().add(panel1, BorderLayout.EAST);
frame.getContentPane().add(panel2, BorderLayout.WEST);

In case you want that right panel to spread throughout the remaining area of frame.

如果您希望该右侧面板遍布整个框架的其余区域。

frame.getContentPane().add(panel2, BorderLayout.CENTER);