如何让 JFrame 自动调整大小以显示所有按钮

发布于 2024-10-05 00:17:11 字数 1348 浏览 4 评论 0原文

我有一个简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

罗罗贝儿 2024-10-12 00:17:11

也许尝试

((JFrame)myButton.getTopLevelAncestor()).pack();

其中 myButton 是在执行过程中修改文本的按钮。

Maybe try

((JFrame)myButton.getTopLevelAncestor()).pack();

Where myButton is the button whose text is modified during execution.

划一舟意中人 2024-10-12 00:17:11

与学习任何 GUI 软件一样,实验是最好的。尝试使用嵌套的 JPanels 来处理 BorderLayouts。

最终,您将 JPanel 与 BorderLayout 一起使用(流布局还可以,但实际上在调整窗口大小时,它会严重失败)。请参阅 http://download.oracle.com/javase/tutorial/uiswing/ layout/border.html 了解有关 BorderLayouts 的更多信息。

现在,对于您的布局方案,它应该类似于:

  1. 顶级容器:JFrame
  2. JFrame 包含一个 JPanel(将此称为 JPanel)
    JPanel 1) 带有 BorderLayout。
  3. 这三个按钮应该位于一个
    单独的 jPanel (JPanel 2)。 J面板
    1 应将三个按钮添加为
    边框布局.CENTER。这样,
    如果按钮,窗口将调整大小
    更改其宽度和/或右侧。
  4. JLabel 应添加为
    边框布局.LINE_START。

教程位于: http://download.oracle.com/javase/tutorial /uiswing/layout/border.html 应该可以帮助你。但一般情况下,请使用以下内容:

  • 使用 JPanel 并根据需要嵌套 JPanel
  • BorderLayout.CENTER 将适应大小变化——这是关键! (对此进行实验)
  • JFrame 只能用作顶级容器(对于更复杂的 GUI,这是正确的)。

如果您需要更大的灵活性,请查看 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:

  1. Top Level Container: JFrame
  2. JFrame contains a JPanel (Call this
    JPanel 1) with a BorderLayout.
  3. 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.
  4. The JLabel should be added as
    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:

  • 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).

If you require more flexibility, check out JGoodies: http://www.jgoodies.com/ . This is more along the lines of creating forms.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文