我设计了一个带有一些组件的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();
}
}
答案 0 :(得分:2)
Label
→JLabel
; TextField
→JTextField
,...)setLayout(null)
)和setBounds()
。检查Using Layout Managers。setSize(w, h)
使用pack()
方法。不显示全名
因为您使用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();
}
}