Java编程 设计一个图形用户界面。界面包括三个单选按钮、两个复选框、一个列表、一个文本区和一个按

2024年12月02日 18:05
有1个网友回答
网友(1):

程序如下:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
public class JFrameDemo extends JFrame implements ActionListener
{
private JPanel panel;
private JButton button;
private JTextArea textArea;
private JCheckBox musicBox;
private JCheckBox danceBox;
private JRadioButton hanButton;
private JRadioButton manButton;
private JRadioButton huiButton;
private ButtonGroup buttonGroup;
public JFrameDemo()
{
panel = new JPanel();
button = new JButton("确定");
textArea = new JTextArea(40,30);
musicBox = new JCheckBox("唱歌");
danceBox = new JCheckBox("跳舞");
huiButton = new JRadioButton("回族");
hanButton = new JRadioButton("汉族");
manButton = new JRadioButton("满族");

buttonGroup = new ButtonGroup();
buttonGroup.add(huiButton);
buttonGroup.add(hanButton);
buttonGroup.add(manButton);

panel.setLayout(new FlowLayout(3));
panel.add(huiButton);
panel.add(hanButton);
panel.add(manButton);
panel.add(musicBox);
panel.add(danceBox);
panel.add(button);
panel.add(textArea);

add(panel);
setTitle("选择兴趣爱好");
setBounds(100, 100, 400, 280);
setResizable(false);
setVisible(true);

this.button.addActionListener(this);

}
public static void main(String[] args)
{
new JFrameDemo();
}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == this.button)
{
String info = "";
if(this.huiButton.isSelected())
{
info += this.huiButton.getText() + "\n";
}
if(this.hanButton.isSelected())
{
info += this.hanButton.getText() + "\n";
}
if(this.manButton.isSelected())
{
info += this.manButton.getText() + "\n";
}
if(this.danceBox.isSelected())
{
info += this.danceBox.getText() + "\n";
}
if(this.musicBox.isSelected())
{
info += this.musicBox.getText() + "\n";
}
this.textArea.setText(info);
}
}
}

有问题欢迎提问,满意请采纳,谢谢!