两部分组成的Java Swing布局 [英] java swing layout of two components

查看:198
本文介绍了两部分组成的Java Swing布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

背景:

  • 在一个JButton(A),大小50x25,一个JPanel内(FlowLayout.CENTER)
  • 在一个JLabel(B),尺寸100x25,一个JPanel内(FlowLayout.RIGHT)
  • 两个Jpanel是一个JPanel

所需的结果:我想

  • 将JButtonA始终水平居中,
  • JLabel将B永远右对齐。

的事情,我已经试过:这些并没有为我工作

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

  • 在BorderLayout的工作不适合我,因为JButton的A左移:
  • 我倒是preFER不要放在一个不可见的组件WEST撤消移

  • 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

  • 网格布局将无法工作,因为我不想要的A和B,以扩大

  • GridLayout won't work because I don't want the "A" and "B" to be expanded

    鸭preciate任何建议!

    Appreciate any suggestions!

    P.S。

    的一个JButton / JLabel对齐各自的自身JPanels内,因为没有在JPanel,BorderLayout.CENTER膨胀将JButton横跨主要面板的整个宽度(最多的JLabel的左边缘)。不需要的JPanels /重要的问题的说明

    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

    结论

    • 在我去与气垫船完全鳗鱼的答案贴在下面。谢谢!

    推荐答案

    您应该嵌套JPanels和使用布局相结合。放置保持Jbutton将到使用GridLayout的另一个的JPanel所述面板(1,0)(1行,列的数目可变)可以工作,并把该JPanel的成BorderLayout的-使用JPanel的BorderLayout.NORTH位置可以工作。

    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.

    例如

    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();
             }
          });
       }
    }
    

    这篇关于两部分组成的Java Swing布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆