显示JCheckBox项的完整名称

时间:2014-06-26 08:13:40

标签: java swing awt layout-manager jcheckbox

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

代码

 public class FindDemo extends JFrame
 {
 Label l1;
 JCheckBox matchCase,MatchWholeWords;
 TextField tf;
 JButton find_next,cancel;
 public FindDemo()
 {
    l1 = new Label("Find What: ");
    matchCase=new JCheckBox();
    matchCase.setText("Match Case ");
    MatchWholeWords=new JCheckBox("Match Whole Words ");
    tf = new TextField(30);
    find_next = new JButton("Find Next");
    cancel = new JButton("Cancel");

    setLayout(null);
    int label_w = 80;
    int label_h = 20;
    int tf_w    = 120;

    l1.setBounds(10,10, label_w, label_h);
    add(l1);
    tf.setBounds(10+label_w, 10, tf_w, 20);
    add(tf);

    matchCase.setBounds(10, 10+label_h+10, label_w, label_h);
    add(matchCase);
    MatchWholeWords.setBounds(10, 10+label_h+35, label_w, label_h);
    add(MatchWholeWords);

    find_next.setBounds(250, 10, 100, 20);
    add(find_next);
    cancel.setBounds(250, 40, 100, 20);
    add(cancel);
    int w = 400;
    int h = 200;
    setSize(w,h);
    Point center = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
    setLocation(center.x-w/2, center.y-h/2);
    setVisible(true);
}
 public static void main(String args[]){
  new FindDemo();
 }
}

1 个答案:

答案 0 :(得分:2)

  1. 使用Swing组件代替AWT组件(LabelJLabel; TextFieldJTextField,...)
  2. 不要使用空布局管理器(setLayout(null))和setBounds()。检查Using Layout Managers
  3. 而不是setSize(w, h)使用pack()方法。
  4.   

    不显示全名

    因为您使用setBounds()和null布局。 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();
        }
    }
    

    enter image description here