java swing布局两个组件

发布于 2024-12-11 03:37:49 字数 1649 浏览 0 评论 0原文

    +--------------------------------------------+
    |                 +-------+      +----------+|
    |                 | +---+ |      |  +-----+ ||
    |                 | | A | |      |  |  B  | ||
    |                 | +---+ |      |  +-----+ ||
    |                 +-------+      +----------+|
    +--------------------------------------------+
                          ^
                          |
                          |
                        Center

背景:

  • 一个 JButton(“A”),大小 50x25,在 JPanel(FlowLayout.CENTER)内
  • 一个 JLabel(“B”),大小 100x25,在 JPanel(FlowLayout.RIGHT)
  • 内两个 JPanel 位于一个 JPanel 中

期望的结果:我想要

  • JButton“A”始终水平居中,
  • JLabel “B”始终向右齐平。

我尝试过的事情:这些对我不起作用

  • BorderLayout 对我不起作用,因为 JButton“A”向左移动:
  • 我不想将不可见的组件放在 WEST 上撤消移位

    <前><代码>+-------------------------------------------------------- -+ | +--------+ +----------+| | | +---+ | | +-----+ || | | |一个 | | | |乙| || | | +---+ | | +-----+ || | +--------+ +----------+| +--------------------------------------------------------+ ^^ | | | | |中心 | 左移
  • GridLayout 不起作用,因为我不希望“A”和“B”展开

感谢任何建议!

ps

JButton/JLabels 均位于其自己的 JPanel 内部,因为如果没有 Jpanel,BorderLayout.CENTER 就会将 JButton 扩展到主面板的整个宽度(直至 JLabel 的左边缘)。 JPanel 对于问题的陈述来说不是必需的/关键的

结论

  • 我选择了下面发布的“Hovercraft Full Of Eels”答案。谢谢!
    +--------------------------------------------+
    |                 +-------+      +----------+|
    |                 | +---+ |      |  +-----+ ||
    |                 | | A | |      |  |  B  | ||
    |                 | +---+ |      |  +-----+ ||
    |                 +-------+      +----------+|
    +--------------------------------------------+
                          ^
                          |
                          |
                        Center

Background: I have

  • a JButton ("A"), size 50x25, within a JPanel (FlowLayout.CENTER)
  • a JLabel ("B"), size 100x25, within a JPanel (FlowLayout.RIGHT)
  • the two JPanels are in a JPanel

Desired Result: I want

  • The JButton "A" to always be centered horizontally,
  • The JLabel "B" to always be flush right.

Thing I've tried: These didn't work for me

  • BorderLayout is not working for me because JButton "A" is shifted LEFT:
  • I'd prefer not to put an invisible component WEST to undo the shift

    +--------------------------------------------+
    |            +-------+           +----------+|
    |            | +---+ |           |  +-----+ ||
    |            | | A | |           |  |  B  | ||
    |            | +---+ |           |  +-----+ ||
    |            +-------+           +----------+|
    +--------------------------------------------+
                     ^    ^
                     |    |
                     |    |
                     |  Center
                     |
                   Shifted Left
    
  • GridLayout won't work because I don't want the "A" and "B" to be expanded

Appreciate any suggestions!

p.s.

The JButton/JLabels are each inside of their own JPanels because WITHOUT the Jpanel, BorderLayout.CENTER expands the JButton across the entire width of the major panel (up to the left edge of the JLabel). The JPanels are not needed/critical to the statement of the problem

Conclusion:

  • I went with "Hovercraft Full Of Eels" answer posted below. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

孤单情人 2024-12-18 03:37:49

您应该嵌套 JPanel 并使用布局组合。将包含 JButton 的面板放置到另一个使用 GridLayout(1, 0)(1 行,可变列数)的 JPanel 中是可行的,并且将该 JPanel 放置到使用 BorderLayout 的 JPanel 的 BorderLayout.NORTH 位置中也是可行的。

例如

import java.awt.*;
import javax.swing.*;

public class Foo003 {

   private static void createAndShowGui() {
      JButton btnA = new JButton("A");
      JButton btnB = new JButton("B");

      btnA.setPreferredSize(new Dimension(50, 25));
      btnB.setPreferredSize(new Dimension(100, 25));

      JPanel btnAPanel = new JPanel(); // uses default FlowLayout.CENTER
      JPanel btnBPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
      btnAPanel.add(btnA);
      btnBPanel.add(btnB);

      JPanel topPanel = new JPanel(new GridLayout(1, 0));
      topPanel.add(new JLabel("")); // empty placeholder label
      topPanel.add(btnAPanel);
      topPanel.add(btnBPanel);

      JPanel mainPanel = new JPanel(new BorderLayout());
      mainPanel.add(topPanel, BorderLayout.NORTH);

      mainPanel.setPreferredSize(new Dimension(400, 300));

      JFrame frame = new JFrame("Foo003");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

You should nest JPanels and use a combination of layouts. Placing the panels holding the JButtons into another JPanel that uses GridLayout(1, 0) (1 row, variable number of columns) could work, and placing that JPanel into the BorderLayout.NORTH position of a BorderLayout-using JPanel could work.

For example

import java.awt.*;
import javax.swing.*;

public class Foo003 {

   private static void createAndShowGui() {
      JButton btnA = new JButton("A");
      JButton btnB = new JButton("B");

      btnA.setPreferredSize(new Dimension(50, 25));
      btnB.setPreferredSize(new Dimension(100, 25));

      JPanel btnAPanel = new JPanel(); // uses default FlowLayout.CENTER
      JPanel btnBPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
      btnAPanel.add(btnA);
      btnBPanel.add(btnB);

      JPanel topPanel = new JPanel(new GridLayout(1, 0));
      topPanel.add(new JLabel("")); // empty placeholder label
      topPanel.add(btnAPanel);
      topPanel.add(btnBPanel);

      JPanel mainPanel = new JPanel(new BorderLayout());
      mainPanel.add(topPanel, BorderLayout.NORTH);

      mainPanel.setPreferredSize(new Dimension(400, 300));

      JFrame frame = new JFrame("Foo003");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文