该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
代码如下 大概就是写个登陆程序 然后登陆完了就开始答题 可是我这那个问题的页面也显示不出来 而且ABCD选项的按钮也没有出现....后面还要写判断每题的对错...我...
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class LoginDemo extends JFrame implements ActionListener{
JTextField jtfUser,jtfPwd;
JButton btn1,btn2;
String fileName="login.txt";
File file=new File(fileName);
int num=100;
public LoginDemo() {
super("Login Demo");
Container c=getContentPane();
c.setLayout(new GridLayout(3,1));
JLabel label1=new JLabel("User name:");
jtfUser=new JTextField(16);
JPanel panel1=new JPanel();
panel1.add(label1);
panel1.add(jtfUser);
JLabel label2=new JLabel("PassWord:");
jtfPwd=new JTextField(16);
JPanel panel2=new JPanel();
panel2.add(label2);
panel2.add(jtfPwd);
btn1=new JButton("Register");
btn2=new JButton("Login");
JPanel panel3=new JPanel();
panel3.add(btn1);
panel3.add(btn2);
c.add(panel1);
c.add(panel2);
c.add(panel3);
btn1.addActionListener(this);
btn2.addActionListener(this);
setSize(500,500);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btn1) {
writeToFile();
}
if(e.getSource()==btn2) {
readFromFile();
}
}
public void writeToFile() {
try {
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
java.util.List list=new java.util.ArrayList();
User user1=new User(num++,jtfUser.getText().trim(),jtfPwd.getText().trim());
list.add(user1);
oos.writeObject(list);
oos.close();
//JOptionPane.showMessageDialog(this,"register success","Information",JOptionPane.INFORMATION_MESSAGE);
new showDialog(this,"Information");
jtfUser.setText(" ");
jtfPwd.setText(" ");
}catch(Exception e1) {
e1.printStackTrace();
}
}
public void readFromFile() {
try {
FileInputStream fis= new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
java.util.List list=(java.util.List)ois.readObject();
for(User user:list) {
if((jtfUser.getText().trim().equals(user.getName()))&&(jtfPwd.getText().trim().equals(user.getPwd()))) {
System.out.println("login success");
new WelcomeUser(fileName);
this.setVisible(false);
}
else {
JOptionPane.showMessageDialog(this,"Login fail","Error msg",JOptionPane.ERROR_MESSAGE);
}
}
}catch(Exception e1) {
e1.printStackTrace();
}
}
public static void main(String[] args) {
new LoginDemo();
}
}
class User implements Serializable{
int number;
private String name,pwd;
public User(int number,String name,String pwd) {
this.number=number;
this.name=name;
this.pwd=pwd;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
class showDialog extends JDialog{
public showDialog(JFrame frame,String title) {
super(frame,title);
JLabel text=new JLabel("注册成功");
Container con=getContentPane();
con.setLayout(new FlowLayout());
con.add(text);
setSize(150,150);
setVisible(true);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
}
class WelcomeUser extends JFrame implements ActionListener,ItemListener{
File questionFile =new File("question.txt");
String [] test;
JButton btn2,btn3,btn4;
String[] str=new String[100];
String[] answer= new String[25];
JTextArea textArea;
JRadioButton rb1,rb2,rb3,rb4;
JButton btn1=new JButton("show question");
int j=6;
String xuanze;
public WelcomeUser(String name) {
super("Welcome Frame");
Container c=getContentPane();
JLabel label=new JLabel("Welcome"+name);
JPanel panel1=new JPanel();
panel1.add(label);
panel1.add(btn1);
textArea=new JTextArea(15,24);
rb1=new JRadioButton("A",false);
rb2=new JRadioButton("B",false);
rb3=new JRadioButton("C",false);
rb4=new JRadioButton("D",false);
ButtonGroup group=new ButtonGroup();
group.add(rb1); group.add(rb2);
group.add(rb3); group.add(rb4);
JPanel panel2= new JPanel();
panel2.add(textArea);
panel2.add(rb1); panel2.add(rb2);
panel2.add(rb3); panel2.add(rb4);
c.add(panel1,BorderLayout.NORTH);
c.add(new JTextArea(),BorderLayout.CENTER);
btn2=new JButton("next");
btn3=new JButton("submit");
btn4=new JButton("hand in");
JPanel panel3=new JPanel();
panel3.add(btn2);
panel3.add(btn3);
panel3.add(btn4);
c.add(panel3,BorderLayout.SOUTH);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
rb1.addItemListener(this);
rb2.addItemListener(this);
rb3.addItemListener(this);
rb4.addItemListener(this);
setSize(500,500);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
void readFile(){
String m="";
int i=0;
try {
FileReader fr=new FileReader(questionFile);
BufferedReader br=new BufferedReader(fr);
while((m=br.readLine())!=null) {
if((i+1)%6==0) {
answer[(i+1)%6]=m;
}
else {
str[i]=m;
}
i++;
}
br.close();
fr.close();
}catch(Exception e) {
System.out.println(e);
}
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btn1) {
textArea.setText("");
textArea.setText(str[0]+"\n"+str[1]+"\n"+str[2]+"\n"+str[3]+"\n"+str[4]);
}
if(e.getSource()==btn2) {
if(str[j]==null) {JOptionPane.showMessageDialog(this,"答题结束","Information",JOptionPane.INFORMATION_MESSAGE);
return;
}
textArea.setText("");
textArea.setText(str[j]+"\n"+str[j+1]+"\n"+str[j+2]+"\n"+str[j+3]+"\n"+str[j+4]);
j+=6;
}
if(e.getSource()==btn3) {
try {
if(xuanze.equals(answer[j/6])) {
JOptionPane.showMessageDialog(this,"正确","infomation",JOptionPane.INFORMATION_MESSAGE);
}
else {
JOptionPane.showMessageDialog(this,"错误","infomation",JOptionPane.ERROR_MESSAGE);
}
}catch(Exception e1) {
JOptionPane.showMessageDialog(this,"没有选择选项","警告",JOptionPane.ERROR_MESSAGE);
}
}
}
public void itemStateChanged(ItemEvent e) {
if(e.getSource()==rb1) xuanze="A";
if(e.getSource()==rb2) xuanze="B";
if(e.getSource()==rb3) xuanze="C";
if(e.getSource()==rb4) xuanze="D";
}
}