【问题标题】:How to Keep JCheckBox Horizontally inside JPanel如何在 JPanel 内保持 JCheckBox 水平
【发布时间】:2022-02-02 07:21:55
【问题描述】:

我有一个JPanel。在面板内部,我保留了一个JLabel 和三个JCheckBox。 我想在 JLabel 之后将所有复选框保留在一行中。这是示例代码和一些屏幕截图。

Output 1 Output 2

当我更改为 X_AXIS 时,所有内容都在一行中,当我切换到 Y_AXIS 时,新行表示垂直。

但我的要求是所有复选框都应该出现在JLabel 之后的下一行。 JLabel 应该排成一行,所有的复选框都应该排成一行。

public class CheckBoxWithJLabel {

    public static void main(String[] args) {
          JFrame f= new JFrame("CheckBox Example");   
          JPanel panel = new JPanel();
          panel.setBounds(40,80,600,200);  
         
          JCheckBox chk_Embrodary=new JCheckBox("Embrodary");
          JCheckBox chk_Cutting=new JCheckBox("Cutting");
          JCheckBox cb_Sewing=new JCheckBox("Sewing");

          panel.setLayout(new javax.swing.BoxLayout(panel, javax.swing.BoxLayout.X_AXIS));
          JLabel lblHeader=new JLabel("Job Work Process Selection");
          panel.add(lblHeader);
          panel.add(chk_Embrodary);
          panel.add(chk_Cutting);
          panel.add(cb_Sewing);
          f.add(panel); 
          f.setSize(600,400);    
          f.setLayout(null);    
          f.setVisible(true);
    }
}

我想要这样的输出 this

如何解决这个问题?

【问题讨论】:

    标签: java swing awt layout-manager


    【解决方案1】:

    我强烈建议您查看 Java Swing Tutorial,尤其是 Laying Out Components Within a Container 部分,因为您似乎对应该如何使用 Swing 及其布局管理器缺乏一些基本的了解。

    关于你的问题:

    目前,您使用的是单个 BoxLayout,它“将组件放在单个行或列中”。不过,您只希望您的 JCheckBoxes 具有这种行为,而不是您的 JLabel。牢记这一点,解决方案是拆分您的组件,而不是将所有组件放在一个 JPanel 中。这样做会让您在设计 GUI 时更加灵活,因为您可以在不同的嵌套面板中使用多种布局。

    你可以这样做(代码 cmets 中的解释):

    public static void main(String[] args) {
        JFrame f = new JFrame("CheckBox Example");
    
        // add a Y_AXIS boxlayout to the JFrames contentpane
        f.getContentPane().setLayout(new BoxLayout(f.getContentPane(), BoxLayout.Y_AXIS));
    
        JCheckBox cbEmbrodary = new JCheckBox("Embrodary");
        JCheckBox cbCutting = new JCheckBox("Cutting");
        JCheckBox cbSewing = new JCheckBox("Sewing");
        // no need to set the bounds, since the layoutmanagers will determine the size
        JPanel labelPanel = new JPanel(); // default layout for JPanel is the FlowLayout
        JLabel lblHeader = new JLabel("Job Work Process Selection");
        labelPanel.add(lblHeader); // JPanel for the label done
    
        // JPanel for the comboboxes with BoxLayout
        JPanel cbPanel = new JPanel();
        cbPanel.setLayout(new BoxLayout(cbPanel, BoxLayout.X_AXIS));
        cbPanel.add(cbEmbrodary);
        cbPanel.add(cbCutting);
        cbPanel.add(cbSewing);
    
        f.add(labelPanel);
        f.add(cbPanel);
    
        // No need to set the size of the JFrame, since the layoutmanagers will
        // determine the size after pack()
        f.pack();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }
    

    输出:

    旁注:

    • 不要通过setSize()setBounds() 为您的组件设置固定大小。 Swing 旨在与适当的 LayoutManager 一起使用,如果您这样做,在设置为可见之前在 JFrame 上调用 pack() 将布局组件并确定它们的适当大小。 (另外,出于同样的原因,不要使用null-layout)

    • 如果您需要 JLabel 不是居中而是左对齐,就像在您的屏幕截图中一样,请使用以下内容:

      FlowLayout layout = (FlowLayout) labelPanel.getLayout();
      layout.setAlignment(FlowLayout.LEFT);
      

    【讨论】:

      猜你喜欢
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多