java 如何让 JFrame 自动调整大小以显示所有按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4269577/
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
How can I make JFrame resize automatically to display all buttons
提问by CalumMcCall
I have a simple swing application which consists of a JLabel and three buttons. The three buttons are in their own JPanel which is in a JFrame along with the JLabel. The JPanel uses flowlayout manager to arrange the buttons horizontally and the JFrame uses the BorderLayout manager to arrange the JLabel and JPanel vertically.
我有一个简单的 Swing 应用程序,它由一个 JLabel 和三个按钮组成。这三个按钮位于它们自己的 JPanel 中,该 JPanel 与 JLabel 一起位于 JFrame 中。JPanel 使用 flowlayout 管理器水平排列按钮,JFrame 使用 BorderLayout 管理器垂直排列 JLabel 和 JPanel。
My problem is when I launch the application, during the course of use the text on one of the buttons changes which increases its width. However, the window doesn't resize to accomdate this and one of the buttons disappears. I thought about calling pack() again, but the JFrame is a local variable in my constructor, also, I shouldn't have to tell my program to resize, right? I haven't been able to find anything on google or here to help me but there must be a simple solution, what am I missing? Code is below.
我的问题是当我启动应用程序时,在使用过程中,其中一个按钮上的文本会发生变化,从而增加了其宽度。但是,窗口不会调整大小以适应这一点,并且其中一个按钮消失了。我想再次调用 pack() ,但 JFrame 是我的构造函数中的局部变量,而且,我不应该告诉我的程序调整大小,对吗?我在谷歌或这里找不到任何帮助我的东西,但必须有一个简单的解决方案,我错过了什么?代码如下。
playButton = new JButton("Play");
pauseButton = new JButton("Pause");
stopButton = new JButton("Stop");
curTrackLabel = new JLabel("No Track Selected");
JFrame myFrame = new JFrame("MediaPlayer");
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setTitle("MediaPlayer");
myFrame.setLocation(400,300);
JPanel topPanel = new JPanel();
topPanel.setLayout(new BorderLayout());
myFrame.add(topPanel);
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(playButton);
buttonPanel.add(pauseButton);
buttonPanel.add(stopButton);
topPanel.add(buttonPanel, BorderLayout.CENTER);
topPanel.add(curTrackLabel, BorderLayout.NORTH);
playButton.addActionListener(new playButtonHandler());
pauseButton.addActionListener(new pauseButtonHandler());
stopButton.addActionListener(new stopButtonHandler());
myFrame.pack();
myFrame.setVisible(true);
回答by karakuricoder
Maybe try
也许试试
((JFrame)myButton.getTopLevelAncestor()).pack();
((JFrame)myButton.getTopLevelAncestor()).pack();
Where myButton is the button whose text is modified during execution.
其中 myButton 是在执行期间修改其文本的按钮。
回答by Carlo del Mundo
As with learning any GUI software, experimentation is best. Try messing with BorderLayouts with nested JPanels.
与学习任何 GUI 软件一样,实验是最好的。尝试使用嵌套的 JPanel 来处理 BorderLayouts。
Ultimately, you use JPanel with a BorderLayout (Flow Layout is OK but really when resizing the window, it epically fails). See http://download.oracle.com/javase/tutorial/uiswing/layout/border.htmlto learn more about BorderLayouts.
最终,您将 JPanel 与 BorderLayout 一起使用(流布局是可以的,但实际上在调整窗口大小时,它会失败)。请参阅http://download.oracle.com/javase/tutorial/uiswing/layout/border.html以了解有关 BorderLayouts 的更多信息。
Now for your layout scheme it should be something along the lines of:
现在对于您的布局方案,它应该是这样的:
- Top Level Container: JFrame
- JFrame contains a JPanel (Call this JPanel 1) with a BorderLayout.
- The three buttons should be in a SEPARATE jPanel (JPanel 2). JPanel 1 should add the three buttons as BorderLayout.CENTER. In this way, the window will resize if the button changes its width and/or hright.
- The JLabel should be added as BorderLayout.LINE_START.
- 顶级容器:JFrame
- JFrame 包含一个带有 BorderLayout 的 JPanel(称为 JPanel 1)。
- 这三个按钮应该在一个单独的 jPanel (JPanel 2) 中。JPanel 1 应将三个按钮添加为 BorderLayout.CENTER。这样,如果按钮更改其宽度和/或 hright,窗口将调整大小。
- JLabel 应添加为 BorderLayout.LINE_START。
The tutorial at: http://download.oracle.com/javase/tutorial/uiswing/layout/border.htmlshould help you with this. But in general, use the following:
位于http://download.oracle.com/javase/tutorial/uiswing/layout/border.html的教程应该可以帮助您解决这个问题。但一般情况下,请使用以下内容:
- Use JPanel and nest JPanels as necessary
- BorderLayout.CENTER will accomodate size changes---this is the key! (Experiment with this)
- JFrame should only be used as a top level container (for more complex GUIs, this is true).
- 根据需要使用 JPanel 并嵌套 JPanel
- BorderLayout.CENTER 将适应尺寸变化---这是关键!(用这个做实验)
- JFrame 应该只用作顶级容器(对于更复杂的 GUI,这是正确的)。
If you require more flexibility, check out JGoodies: http://www.jgoodies.com/. This is more along the lines of creating forms.
如果您需要更大的灵活性,请查看 JGoodies:http://www.jgoodies.com/ 。这更符合创建表单的思路。