这个题怎么做 请编写程序实现学生选课界面,要求:
请编写程序实现学生选课界面,要求:1.通过文本行输入学生姓名,通过单选按钮选择性别,通过复选框选择课程,并在文本框中显示所填写及选择的信息;2.其界面如右图所示;3.用S...
请编写程序实现学生选课界面,要求:
1.通过文本行输入学生姓名,通过单选按钮选择性别,通过复选框选择课程,并在文本框中显示所填写及选择的信息;
2.其界面如右图所示;
3.用Swing组件实现。只有二十分了 展开
1.通过文本行输入学生姓名,通过单选按钮选择性别,通过复选框选择课程,并在文本框中显示所填写及选择的信息;
2.其界面如右图所示;
3.用Swing组件实现。只有二十分了 展开
2个回答
展开全部
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class SelCourse extends JFrame {
public SelCourse(){
super("选课信息");
JPanel panel=new JPanel();
JLabel _name=new JLabel();
_name.setText("姓名:");
final JTextField name=new JTextField(10);
JLabel _sex=new JLabel();
_sex.setText("性别:");
final JRadioButton sex1=new JRadioButton("男");
sex1.setSelected(true);
final JRadioButton sex2=new JRadioButton("女");
sex1.addChangeListener(new ChangeListener(){
public void stateChanged(ChangeEvent e) {
if(sex1.isSelected()){
sex2.setSelected(false);
}
}
});
sex2.addChangeListener(new ChangeListener(){
public void stateChanged(ChangeEvent e) {
if(sex2.isSelected()){
sex1.setSelected(false);
}
}
});
final JCheckBox c=new JCheckBox("C语言 ");
final JCheckBox java=new JCheckBox("JAVA ");
final JButton confirm=new JButton("确定");
final JButton cancel=new JButton("取消薯简");
final JTextArea msg=new JTextArea(8,15);
msg.setEditable(false);
confirm.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
StringBuffer sb=new StringBuffer();
sb.append("姓名:").append(name.getText()).append("\n");
sb.append("性别:").append(sex1.isSelected()?"男":"女").append("\n");
sb.append("所选课程如码樱下:\n");
sb.append(c.isSelected()?"C语言程序设计\n":"");
sb.append(java.isSelected()?"Java语言程序设计":""数模裤);
msg.setText(sb.toString());
}
});
cancel.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
msg.setText("");
}
});
add(panel);
panel.add(_name,"Center");
panel.add(name,"Center");
panel.add(_sex,"Center");
panel.add(sex1,"Center");
panel.add(sex2,"Center");
panel.add(c,"Center");
panel.add(java,"Center");
panel.add(confirm,"Center");
panel.add(cancel,"Center");
panel.add(msg,"Center");
setSize(200,350);
setLocation(500,100);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new SelCourse();
}
}
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class SelCourse extends JFrame {
public SelCourse(){
super("选课信息");
JPanel panel=new JPanel();
JLabel _name=new JLabel();
_name.setText("姓名:");
final JTextField name=new JTextField(10);
JLabel _sex=new JLabel();
_sex.setText("性别:");
final JRadioButton sex1=new JRadioButton("男");
sex1.setSelected(true);
final JRadioButton sex2=new JRadioButton("女");
sex1.addChangeListener(new ChangeListener(){
public void stateChanged(ChangeEvent e) {
if(sex1.isSelected()){
sex2.setSelected(false);
}
}
});
sex2.addChangeListener(new ChangeListener(){
public void stateChanged(ChangeEvent e) {
if(sex2.isSelected()){
sex1.setSelected(false);
}
}
});
final JCheckBox c=new JCheckBox("C语言 ");
final JCheckBox java=new JCheckBox("JAVA ");
final JButton confirm=new JButton("确定");
final JButton cancel=new JButton("取消薯简");
final JTextArea msg=new JTextArea(8,15);
msg.setEditable(false);
confirm.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
StringBuffer sb=new StringBuffer();
sb.append("姓名:").append(name.getText()).append("\n");
sb.append("性别:").append(sex1.isSelected()?"男":"女").append("\n");
sb.append("所选课程如码樱下:\n");
sb.append(c.isSelected()?"C语言程序设计\n":"");
sb.append(java.isSelected()?"Java语言程序设计":""数模裤);
msg.setText(sb.toString());
}
});
cancel.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
msg.setText("");
}
});
add(panel);
panel.add(_name,"Center");
panel.add(name,"Center");
panel.add(_sex,"Center");
panel.add(sex1,"Center");
panel.add(sex2,"Center");
panel.add(c,"Center");
panel.add(java,"Center");
panel.add(confirm,"Center");
panel.add(cancel,"Center");
panel.add(msg,"Center");
setSize(200,350);
setLocation(500,100);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new SelCourse();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询