Java 显示JCheckBox项的全名

Java 显示JCheckBox项的全名,java,swing,awt,layout-manager,jcheckbox,Java,Swing,Awt,Layout Manager,Jcheckbox,我设计了一个带有一些组件的JFrame。在该JCheckBox组件中,我已经给出了一个名称,但在运行应用程序时不会显示全名。我想显示给定的名称 代码 使用Swing组件而不是AWT组件(Label→ JLabel;文本字段→ JTextField,…) 不要使用空布局管理器(setLayout(null))和setBounds()。检查 使用pack()方法代替setSize(w,h) 全名不显示 因为您使用了setBounds()和空布局matchCase.setBounds(10,10+la

我设计了一个带有一些组件的
JFrame
。在该
JCheckBox
组件中,我已经给出了一个名称,但在运行应用程序时不会显示全名。我想显示给定的名称

代码
  • 使用Swing组件而不是AWT组件(
    Label
    → <代码>JLabel;
    文本字段
    → <代码>JTextField,…)
  • 不要使用空布局管理器(
    setLayout(null)
    )和
    setBounds()
    。检查
  • 使用
    pack()
    方法代替
    setSize(w,h)
  • 全名不显示

    因为您使用了
    setBounds()
    和空布局
    matchCase.setBounds(10,10+label_h+10,label_w,label_h)
    您没有为
    JCheckBox设置足够的空间,因为它用“…”显示

    例如,您的面板带有
    GridBagLayout

    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    
    import javax.swing.JButton;
    import javax.swing.JCheckBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    
    public class TestFrame extends JFrame {
    
        JLabel l1;
        JCheckBox matchCase, MatchWholeWords;
        JTextField tf;
        JButton find_next, cancel;
    
        public TestFrame() {
            l1 = new JLabel("Find What: ");
            matchCase = new JCheckBox();
            matchCase.setText("Match Case ");
            MatchWholeWords = new JCheckBox("Match Whole Words ");
            tf = new JTextField(30);
            find_next = new JButton("Find Next");
            cancel = new JButton("Cancel");
    
            setLayout(new GridBagLayout());
    
            GridBagConstraints c = new GridBagConstraints();
            c.insets = new Insets(5, 5, 5, 5);
            c.gridx = 0;
            c.gridy = 0;
            c.anchor = GridBagConstraints.WEST;
            add(l1, c);
            c.gridx++;
            c.fill = GridBagConstraints.HORIZONTAL;
            c.weightx = 1;
            add(tf, c);
            c.fill = GridBagConstraints.NONE;
            c.weightx = 0;
            c.gridx++;
            add(find_next, c);
            c.gridx = 0;
            c.gridy = 1;
            c.gridwidth = 2;
            add(matchCase, c);
            c.gridx++;
            c.gridx++;
            c.gridwidth = 1;
            c.fill = GridBagConstraints.HORIZONTAL;
            add(cancel, c);
            c.gridy++;
            c.gridx = 0;
            c.gridwidth = 2;
            add(MatchWholeWords, c);
            setLocationRelativeTo(null);
            pack();
            setVisible(true);
        }
    
        public static void main(String[] args) {
            new TestFrame();
        }
    }
    

    Re。布局。有关详细信息,请参见和布局填充和边框。
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    
    import javax.swing.JButton;
    import javax.swing.JCheckBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    
    public class TestFrame extends JFrame {
    
        JLabel l1;
        JCheckBox matchCase, MatchWholeWords;
        JTextField tf;
        JButton find_next, cancel;
    
        public TestFrame() {
            l1 = new JLabel("Find What: ");
            matchCase = new JCheckBox();
            matchCase.setText("Match Case ");
            MatchWholeWords = new JCheckBox("Match Whole Words ");
            tf = new JTextField(30);
            find_next = new JButton("Find Next");
            cancel = new JButton("Cancel");
    
            setLayout(new GridBagLayout());
    
            GridBagConstraints c = new GridBagConstraints();
            c.insets = new Insets(5, 5, 5, 5);
            c.gridx = 0;
            c.gridy = 0;
            c.anchor = GridBagConstraints.WEST;
            add(l1, c);
            c.gridx++;
            c.fill = GridBagConstraints.HORIZONTAL;
            c.weightx = 1;
            add(tf, c);
            c.fill = GridBagConstraints.NONE;
            c.weightx = 0;
            c.gridx++;
            add(find_next, c);
            c.gridx = 0;
            c.gridy = 1;
            c.gridwidth = 2;
            add(matchCase, c);
            c.gridx++;
            c.gridx++;
            c.gridwidth = 1;
            c.fill = GridBagConstraints.HORIZONTAL;
            add(cancel, c);
            c.gridy++;
            c.gridx = 0;
            c.gridwidth = 2;
            add(MatchWholeWords, c);
            setLocationRelativeTo(null);
            pack();
            setVisible(true);
        }
    
        public static void main(String[] args) {
            new TestFrame();
        }
    }