gpt4 book ai didi

java - 显示 JCheckBox 项目的全名

转载 作者:行者123 更新时间:2023-12-02 05:42:10 24 4
gpt4 key购买 nike

我设计了一个带有一些组件的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. 使用 Swing 组件而不是 AWT 组件 (LabelJLabel; TextFieldJTextField, ... )
  2. 请勿使用 null 布局管理器 (setLayout(null)) 和 setBounds()。检查Using Layout Managers .
  3. 使用 pack() 方法代替 setSize(w, h)

full name is not displayed

因为您使用 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

关于java - 显示 JCheckBox 项目的全名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24425542/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com