JAVA GUI学习 继续
之前的登录界面并没有完成 因此把监听部分补上
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand()=="登录")
{
password_check();
if("".equals(username_text.getText()))
{
JOptionPane.showMessageDialog(null, "用户名不可为空!");
}
else if("".equals(String.valueOf(password_text.getPassword())))
{
JOptionPane.showMessageDialog(null, "密码不可为空!");
}
else if(!flag_login)
{
JOptionPane.showMessageDialog(null, "用户名或密码错误!");
}
username_text.setText("");//保证安全,用户名和密码设0
password_text.setText("");
}
if(e.getActionCommand()=="注册")
{
new GUI_register();//开注册界面
}
if(flag_login==true)
{
System.out.println("跳转中");//无意义,写着玩
new GUI_select();//开选题界面
username_text.setText("");
password_text.setText("");
flag_login=false;
login_frame.dispose();
//跳转至选择界面
}
}
总共5个界面+一个自动出题,重复部分很多,因此只拿出一部分来
密码核对:每一行为一组用户名和密码,以分号分隔,创建文件,逐行读取核对即可
用户查重等与此近似
由于是小作业因此只用TXT保存
void password_check()
{
try
{
File autopro_dir=new File("D:\\autopro");
if(!autopro_dir.exists())
{
autopro_dir.mkdir();
}
File password_dir=new File("D:\\autopro\\password");
if(!password_dir.exists())
{
password_dir.mkdir();
}
File password_file=new File("D:\\autopro\\password\\password.txt");
if(!password_file.exists())
{
password_file.createNewFile();
}
//以上password文件生成
String password_path;
password_path="D:\\autopro\\password\\password.txt";
InputStreamReader password_reader=new InputStreamReader(new FileInputStream(password_path));
BufferedReader password_br=new BufferedReader(password_reader);
String password_line="";
String[] password_unp=new String[2];//用户名和密码
while((password_line = password_br.readLine()) != null)
{
password_unp=password_line.split(";");//以;分隔,因此密码中不可以有;
if(password_unp[0].equals(username_text.getText()))
{
if(password_unp[1].equals(String.valueOf(password_text.getPassword())))
{
username_login=username_text.getText();
password_login=String.valueOf(password_text.getPassword());
flag_login=true;
}
}
}
password_br.close();
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("Password check failed.");
}
}
字体设置:
Font font = new Font(Font.DIALOG, Font.BOLD, 20);
title_label.setFont(font);
关闭页面和下拉选项:
select_frame.dispose();//关闭页面
grade_choice=new Choice();//下拉选项
grade_choice.add("小学");
grade_choice.add("初中");
grade_choice.add("高中");
grade_choice.setBounds(170, 88, 60, 20);
JRadioButton组:
choice1=new JRadioButton();
choice2=new JRadioButton();
choice3=new JRadioButton();
choice4=new JRadioButton();
choice_group=new ButtonGroup();
choice_group.add(choice1);
choice1.setBounds(80, 130, 20, 20);
choice_group.add(choice2);
choice2.setBounds(180, 130, 20, 20);
choice_group.add(choice3);
choice3.setBounds(280, 130, 20, 20);
choice_group.add(choice4);
choice4.setBounds(380, 130, 20, 20);
choice1.setSelected(false);//设为未选中
choice2.setSelected(false);
choice3.setSelected(false);
choice4.setSelected(false);
choice_group.clearSelection();
choice1.isSelected()//此按钮是否被选中,boolean型
//更改标签文本 choice1_label.setText(String.valueOf(String.valueOf(choice_output[question_now][0])));
choice2_label.setText(String.valueOf(String.valueOf(choice_output[question_now][1])));
choice3_label.setText(String.valueOf(String.valueOf(choice_output[question_now][2])));
choice4_label.setText(String.valueOf(String.valueOf(choice_output[question_now][3])));
由于button不支持settext,所以改用JButton来实现更改文本
JButton button_previous;
JButton button_next;
button_previous=new JButton("上一题");
button_next=new JButton("下一题");
button_previous.addActionListener(this);
button_previous.setBounds(160, 170, 80, 20);
button_next.addActionListener(this);
button_next.setBounds(280, 170, 80, 20);
button_next.setText("完成");
就这点 = =
效果截图:
丑吧,丑就对了,溜了,告辞