简体   繁体   English

Java Swing JButton 对齐 - BoxLayout

[英]Java Swing JButton alignment - BoxLayout

Below is a small example where two radio buttons are offset too far to the left column.下面是一个小示例,其中两个单选按钮向左列偏移得太远。

单选按钮标签错误地偏移

However, if I remove the "A" button above by commenting out this code:但是,如果我通过注释掉此代码来删除上面的“A”按钮:

在此处输入图片说明

Then the 2 radio buttons are displayed as expected and not offset:然后 2 个单选按钮按预期显示而不是偏移:

在此处输入图片说明

How do I get the radio buttons aligned correctly like the second case, but with the button present?如何让单选按钮像第二种情况一样正确对齐,但按钮存在? Here is the code:这是代码:

Main.java:主.java:

package layoutdemo;

import java.awt.Color;
import java.awt.Frame;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.WindowConstants;

public class Main extends JFrame {  
    public Main() {
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setExtendedState(Frame.MAXIMIZED_BOTH);
        getContentPane().setLayout(new BoxLayout(this.getContentPane(), BoxLayout.X_AXIS));
        getContentPane().setBackground(Color.black);
        add(new LeftPanel());
        pack();
        setVisible(true);
    }

    public static void main(String args[]) {
        new Main();
    }
}

LeftPanel.java:左面板.java:

package layoutdemo;

import java.awt.Component;
import java.awt.Dimension;

import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class LeftPanel extends JPanel {
    public LeftPanel() {
        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        //add(new ButtonPanel());
        add(new JRadioButton("RadioBut2"));
        add(new JRadioButton("RadioBut1"));
    }

    @Override
    public Dimension getPreferredSize() {
        Component p = getParent();
        while(p.getParent() != null) {
            p = p.getParent();
        }
        Dimension dimension = p.getSize();
        return new Dimension(100, (int) (dimension.getHeight()));
    }
    
    @Override 
    public Dimension getMaximumSize() {
        return getPreferredSize();
    }
}

ButtonPanel.java:按钮面板.java:

package layoutdemo;

import java.awt.Component;
import java.awt.Dimension;

import javax.swing.JPanel;
import javax.swing.JToggleButton;

public class ButtonPanel extends JPanel {
    public ButtonPanel() {
        add(new JToggleButton("A"));
    }
    
    @Override
    public Dimension getPreferredSize() {
        Component p = getParent();
        while(p.getParent() != null) {
            p = p.getParent();
        }
        Dimension dimension = p.getSize();
        return new Dimension(dimension.width, 50);
    }
    
    @Override 
    public Dimension getMaximumSize() {
        return getPreferredSize();
    }
}

It could be to do with the "alignmentX" property for those items.这可能与这些项目的“alignmentX”属性有关。 Trying setting all those items to the same alignmentX value.尝试将所有这些项目设置为相同的 alignmentX 值。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM