.设计下图所示用户界面,界面中包括四个多选按钮,一个文本区,一个标签,点击相应多选按钮,能实现其功能
时间: 2023-12-07 14:02:14 浏览: 51
这里给出一个简单的Java Swing实现的示例代码,实现了图中所示的用户界面,其中包括四个多选按钮、一个文本区和一个标签。点击相应的多选按钮,会在文本区中显示相应的文本信息。
```java
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class UserInterface extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JLabel label;
private JTextArea textArea;
private JCheckBox checkBox1;
private JCheckBox checkBox2;
private JCheckBox checkBox3;
private JCheckBox checkBox4;
public UserInterface() {
// 设置窗口标题
setTitle("用户界面");
// 初始化标签和文本区
label = new JLabel("请选择以下操作:");
textArea = new JTextArea(10, 30);
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
// 初始化多选按钮
checkBox1 = new JCheckBox("选项1");
checkBox1.addActionListener(this);
checkBox2 = new JCheckBox("选项2");
checkBox2.addActionListener(this);
checkBox3 = new JCheckBox("选项3");
checkBox3.addActionListener(this);
checkBox4 = new JCheckBox("选项4");
checkBox4.addActionListener(this);
// 初始化面板
JPanel panel = new JPanel();
panel.add(checkBox1);
panel.add(checkBox2);
panel.add(checkBox3);
panel.add(checkBox4);
// 添加组件到窗口
add(label, BorderLayout.NORTH);
add(scrollPane, BorderLayout.CENTER);
add(panel, BorderLayout.SOUTH);
// 设置窗口大小和可见性
setSize(400, 300);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// 点击多选按钮后,将相应的文本信息显示在文本区中
if (e.getSource() == checkBox1) {
textArea.append("选项1被选中\n");
} else if (e.getSource() == checkBox2) {
textArea.append("选项2被选中\n");
} else if (e.getSource() == checkBox3) {
textArea.append("选项3被选中\n");
} else if (e.getSource() == checkBox4) {
textArea.append("选项4被选中\n");
}
}
public static void main(String[] args) {
new UserInterface();
}
}
```
在上述代码中,我们使用了 Java Swing 提供的组件,包括 JFrame、JLabel、JTextArea、JScrollPane 和 JCheckBox 等。在构造函数中,我们初始化了这些组件,并将它们添加到窗口中。同时,我们还实现了 ActionListener 接口,以便在点击多选按钮时能够将相应的文本信息显示在文本区中。最后,我们创建了一个 UserInterface 对象来显示窗口。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://img-home.csdnimg.cn/images/20210720083646.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)