金庸无双2 五霸岗上树:谁帮忙给我写下 JAVA的代码

来源:百度文库 编辑:中科新闻网 时间:2024/04/30 18:19:18
请高手帮忙:
程序组合设计:根据课本上及网站上所提供源程序,将一些程序进行组合。如将网站上有关组件的实例,设计一个综合组件的程序,输入学生的姓名(文本框)、性别(单选)、专业(下拉框)、兴趣(复选框)等内容。最后通过“显示”按钮,综合所有的输入内容,在一个文本区域框中显示出来。

2、在前一个程序基础上,增加一个保存按钮,将所输入的内容,以文本文件形式保存。以同学的姓名作为文件名。

package com;
import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
public class Save extends JFrame implements ActionListener,ItemListener {

/**
* @param args
*/
String s,b;
JTextField text;
JButton button;
ButtonGroup buttong;
JRadioButton radio1,radio2;
JComboBox com;
String[] str = {"English","Electronics","History","Chinese"};
JCheckBox box1,box2,box3;
JTextArea area;
public Save() {

super("Message");
setLayout(new GridLayout(6,1));
JPanel p = new JPanel();
p.setLayout(new GridLayout(1,2));
add(p);
JLabel label1 = new JLabel("name");
p.add(label1);
text = new JTextField(5);
p.add(text);
text.addActionListener(this);
JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(1,3));
add(p2);
JLabel label2 = new JLabel("sex");
p2.add(label2);
radio1 = new JRadioButton("male",false);
radio2 = new JRadioButton("female",false);
buttong = new ButtonGroup();
buttong.add(radio1);
buttong.add(radio2);
p2.add(radio1);
p2.add(radio2);
radio1.addItemListener(this);
radio2.addItemListener(this);
JPanel p3 = new JPanel();
p3.setLayout(new GridLayout(1,2));
add(p3);
JLabel label3 = new JLabel("special");
p3.add(label3);
com = new JComboBox(str);
com.addActionListener(this);
p3.add(com);
JPanel p4 = new JPanel();
p4.setLayout(new GridLayout(1,4));
add(p4);
JLabel label4 = new JLabel("Hobby");
p4.add(label4);
box1 = new JCheckBox("Basketball",false);
p4.add(box1);
box1.addItemListener(this);
box2 = new JCheckBox("Music",false);
box2.addItemListener(this);

p4.add(box2);
box3 = new JCheckBox("Football",false);
box3.addItemListener(this);
p4.add(box3);
JPanel p1 = new JPanel();
add(p1);
area = new JTextArea(10,26);
area.setFont(new Font("Serif", Font.BOLD, 14));
p1.add(area);
JPanel p5 = new JPanel();
add(p5);
button = new JButton("Show Message");
button.addActionListener(this);
p5.add(button);

setSize(500,300);
setLocation(300,100);
setVisible(true);
}
public void itemStateChanged(ItemEvent e) {
if(e.getSource() == radio1)
s = "male";
if (e.getSource() == radio2)
s = "female";
if(e.getSource() == box1)
b = "basketball";
if(e.getSource()== box2)
b= "music";
if(e.getSource()== box3)
b = "football";
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button) {
area.append("\t"+"name" + "\t" + "sex"+"\t" + "special"+"\t" + "hobby"+"\n");
area.append("\t"+text.getText() + "\t" + s + "\t" + com.getSelectedItem() + "\t" + b);
text.setText("");
}
}
}
第二问就是把冬冬写到文件中。
if (e.getSource() == button1) {
result = text.getText() + " " + s + " " + com.getSelectedItem() +" " + b;
try {

File out = new File (text.getText());
FileWriter fw = new FileWriter(out);
PrintWriter pw = new PrintWriter(fw);
pw.println(result);
fw.close();
} catch (Exception ex) {
System.out.println("File Disappeared");
}
}