急求:java如何用jframe的jtextfield中的文字创建复选框
比如说在jframe的jtextfield中输入10个候选人的名字,用空格分开,然后点确定按钮后能用这10个人的名字生成10个复选框...
比如说在jframe的jtextfield中输入10个候选人的名字,用空格分开,然后点确定按钮后能用这 10个人的名字生成10个复选框
展开
1个回答
展开全部
你运行一下,应该达到你的要求了
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.StringTokenizer;
public class CheckApp extends JFrame {
private JTextField textField;
public CheckApp() {
setSize(400, 600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().setLayout(null);
textField = new JTextField();
textField.setBounds(10, 10, 359, 21);
getContentPane().add(textField);
textField.setColumns(10);
JButton btnGenerate = new JButton("Generate");
btnGenerate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String str = textField.getText();
StringTokenizer token = new StringTokenizer(str);
int i = 0;
int space = 25;
while (token.hasMoreElements()) {
JCheckBox checkBox = new JCheckBox(token.nextToken());
checkBox.setBounds(10, 70 + (space * i++), 103, 23);
getContentPane().add(checkBox);
}
repaint();
}
});
btnGenerate.setBounds(10, 41, 93, 23);
getContentPane().add(btnGenerate);
setVisible(true);
}
public static void main(String[] args) {
new CheckApp();
}
}
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.StringTokenizer;
public class CheckApp extends JFrame {
private JTextField textField;
public CheckApp() {
setSize(400, 600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().setLayout(null);
textField = new JTextField();
textField.setBounds(10, 10, 359, 21);
getContentPane().add(textField);
textField.setColumns(10);
JButton btnGenerate = new JButton("Generate");
btnGenerate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String str = textField.getText();
StringTokenizer token = new StringTokenizer(str);
int i = 0;
int space = 25;
while (token.hasMoreElements()) {
JCheckBox checkBox = new JCheckBox(token.nextToken());
checkBox.setBounds(10, 70 + (space * i++), 103, 23);
getContentPane().add(checkBox);
}
repaint();
}
});
btnGenerate.setBounds(10, 41, 93, 23);
getContentPane().add(btnGenerate);
setVisible(true);
}
public static void main(String[] args) {
new CheckApp();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询