用java实现6种输入文本的GUI
时间: 2024-03-22 10:38:59 浏览: 19
以下是使用 Java Swing 实现 6 种输入文本的 GUI 的示例代码:
1. 文本框(JTextField)
```
import javax.swing.*;
public class JTextFieldExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JTextField Example");
JTextField textField = new JTextField(20);
JLabel label = new JLabel("Enter your name:");
JPanel panel = new JPanel();
panel.add(label);
panel.add(textField);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
```
2. 密码框(JPasswordField)
```
import javax.swing.*;
public class JPasswordFieldExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JPasswordField Example");
JPasswordField passwordField = new JPasswordField(20);
JLabel label = new JLabel("Enter your password:");
JPanel panel = new JPanel();
panel.add(label);
panel.add(passwordField);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
```
3. 多行文本框(JTextArea)
```
import javax.swing.*;
public class JTextAreaExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JTextArea Example");
JTextArea textArea = new JTextArea(5, 20);
JScrollPane scrollPane = new JScrollPane(textArea);
JLabel label = new JLabel("Enter your comments:");
JPanel panel = new JPanel();
panel.add(label);
panel.add(scrollPane);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
```
4. 单选按钮(JRadioButton)
```
import javax.swing.*;
public class JRadioButtonExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JRadioButton Example");
JRadioButton radio1 = new JRadioButton("Male");
JRadioButton radio2 = new JRadioButton("Female");
ButtonGroup group = new ButtonGroup();
group.add(radio1);
group.add(radio2);
JPanel panel = new JPanel();
panel.add(radio1);
panel.add(radio2);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
```
5. 复选框(JCheckBox)
```
import javax.swing.*;
public class JCheckBoxExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JCheckBox Example");
JCheckBox checkBox1 = new JCheckBox("Java");
JCheckBox checkBox2 = new JCheckBox("Python");
JCheckBox checkBox3 = new JCheckBox("C++");
JPanel panel = new JPanel();
panel.add(checkBox1);
panel.add(checkBox2);
panel.add(checkBox3);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
```
6. 下拉列表框(JComboBox)
```
import javax.swing.*;
public class JComboBoxExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JComboBox Example");
String[] items = {"Java", "Python", "C++"};
JComboBox<String> comboBox = new JComboBox<>(items);
JPanel panel = new JPanel();
panel.add(comboBox);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
```
以上是使用 Java Swing 实现 6 种输入文本的 GUI 的示例代码。可以根据实际需求进行修改和扩展。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)