如何让 JFrame 自动调整大小以显示所有按钮
我有一个简单的 swing 应用程序,由一个 JLabel 和三个按钮组成。这三个按钮位于它们自己的 JPanel 中,该 JPanel 与 JLabel 一起位于 JFrame 中。 JPanel 使用 flowlayout 管理器水平排列按钮,JFrame 使用 BorderLayout 管理器垂直排列 JLabel 和 JPanel。
我的问题是,当我启动应用程序时,在使用过程中,其中一个按钮上的文本发生变化,从而增加了其宽度。但是,窗口不会调整大小以适应这一情况,并且其中一个按钮会消失。我想过再次调用 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);
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.
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.
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许尝试
((JFrame)myButton.getTopLevelAncestor()).pack();
其中 myButton 是在执行过程中修改文本的按钮。
Maybe try
((JFrame)myButton.getTopLevelAncestor()).pack();
Where myButton is the button whose text is modified during execution.
与学习任何 GUI 软件一样,实验是最好的。尝试使用嵌套的 JPanels 来处理 BorderLayouts。
最终,您将 JPanel 与 BorderLayout 一起使用(流布局还可以,但实际上在调整窗口大小时,它会严重失败)。请参阅 http://download.oracle.com/javase/tutorial/uiswing/ layout/border.html 了解有关 BorderLayouts 的更多信息。
现在,对于您的布局方案,它应该类似于:
JPanel 1) 带有 BorderLayout。
单独的 jPanel (JPanel 2)。 J面板
1 应将三个按钮添加为
边框布局.CENTER。这样,
如果按钮,窗口将调整大小
更改其宽度和/或右侧。
边框布局.LINE_START。
教程位于: http://download.oracle.com/javase/tutorial /uiswing/layout/border.html 应该可以帮助你。但一般情况下,请使用以下内容:
如果您需要更大的灵活性,请查看 JGoodies:http://www.jgoodies.com/ 。这更符合创建形式的思路。
As with learning any GUI software, experimentation is best. Try messing with BorderLayouts with nested JPanels.
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.html to learn more about BorderLayouts.
Now for your layout scheme it should be something along the lines of:
JPanel 1) with a BorderLayout.
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.
BorderLayout.LINE_START.
The tutorial at: http://download.oracle.com/javase/tutorial/uiswing/layout/border.html should help you with this. But in general, use the following:
If you require more flexibility, check out JGoodies: http://www.jgoodies.com/ . This is more along the lines of creating forms.