【问题标题】:Java Swing Alignment problem with BoxLayoutBoxLayout 的 Java Swing 对齐问题
【发布时间】:2021-11-20 08:37:15
【问题描述】:

我在一个自上而下的 boxLayout 中包含三个组件(JLabel、JTextField、JButton)。问题是,当我为标签设置 X 对齐方式时,它看起来好像我会更改按钮的 X 对齐方式,反之亦然,只有当两者具有相同的对齐方式时它才能正常工作。

当屏幕变宽时,两个组件都会出现奇怪的对齐方式。

当两个组件具有相同的对齐方式时,一切正常。

这是我的代码:

public void create(){
    JPanel panel =  new JPanel();
    BoxLayout boxLayout = new BoxLayout(panel, BoxLayout.Y_AXIS);
    panel.setLayout(boxLayout);
    panel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
    
    JLabel etiqueta =  new JLabel("Numero de consultorio: ");
    etiqueta.setBackground(Color.BLUE);
    etiqueta.setOpaque(true);
    etiqueta.setAlignmentX(Component.LEFT_ALIGNMENT);
    panel.add(etiqueta);
    
    JTextField consultorio = new JTextField();
    panel.add(consultorio);
    
    JButton registrar =  new JButton("Registrar");
    registrar.setAlignmentX(Component.LEFT_ALIGNMENT);
    panel.add(registrar);
    
    this.getContentPane().add(panel, BorderLayout.CENTER);
}

【问题讨论】:

  • 在有 ASCII / 艺术绘图的预期 GUI 你最好尝试的 minimal reproducible example 之前不愿意仔细研究这个(不打扰代码 sn -ps)。话虽如此,我可能会通过将具有不同对齐方式的组件放入带有FlowLayoutJPanel 并将面板添加到具有与其他组件相同的布局约束的外框布局中来解决此问题。流布局中组件的对齐方式同样可以使用(流布局)约束进行排序。
  • 只有当两者具有相同的对齐方式时它才能正常工作。 - 正确。这就是它应该工作的方式。阅读Fixing Alignment Problems上的教程

标签: java swing alignment layout-manager boxlayout


【解决方案1】:

这里是 Andrew Thompson 提出的解决方案:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

public class TestFrame {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new TestFrame()::create);
    }

    private void create() {
        JPanel panel = new JPanel();
        BoxLayout boxLayout = new BoxLayout(panel, BoxLayout.Y_AXIS);
        panel.setLayout(boxLayout);
        panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        JLabel etiqueta = new JLabel("Numero de consultorio: ");
        etiqueta.setBackground(Color.BLUE);
        etiqueta.setOpaque(true);
        JPanel layout = new JPanel(new FlowLayout(FlowLayout.LEADING, 0, 0));
        layout.add(etiqueta);
        panel.add(layout);

        JTextField consultorio = new JTextField();
        panel.add(consultorio);

        JButton registrar = new JButton("Registrar");
        layout = new JPanel(new FlowLayout(FlowLayout.TRAILING, 0, 0));
        layout.add(registrar);
        panel.add(layout);

        JFrame frm = new JFrame("Test");
        frm.getContentPane().add(panel, BorderLayout.CENTER);
        frm.pack();
        frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frm.setLocationRelativeTo(null);
        frm.setVisible(true);
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-06
    • 2012-07-28
    • 2021-06-21
    • 2011-07-08
    • 1970-01-01
    • 1970-01-01
    • 2015-06-15
    相关资源
    最近更新 更多