急! java编写一个小程序

2024-11-22 17:07:16
推荐回答(4个)
回答1:

新建个java文件,T.java

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class T {
public static void main(String[] args) {
new Frame1();
}
}

class Frame1 extends JFrame {

private static final long serialVersionUID = 960670063256235391L;

private static final int text_size = 15;
int n = 0;

private final JLabel usernameLabel = new JLabel("用户名");
private final JTextField usernameField = new JTextField("", text_size);
private final JLabel passwordLabel = new JLabel("密码");
private final JPasswordField passwordField = new JPasswordField("", text_size);
private final JButton loginButton = new JButton("登陆");

protected Frame1() {
setTitle("登陆");
setLayout(new FlowLayout());
add(usernameLabel);
add(usernameField);
add(passwordLabel);
add(passwordField);
add(loginButton);

loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (login()) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new Frame2();
}
});
Frame1.this.dispose();
} else {
n++;
if (n > 2)
JOptionPane.showMessageDialog(Frame1.this, "错误超过两次了", "stop", JOptionPane.ERROR_MESSAGE);
else
JOptionPane.showMessageDialog(Frame1.this, "STOP", "stop", JOptionPane.ERROR_MESSAGE);
}
}
});

setSize(200, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = getSize();
setLocation( (screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
}

private boolean login() {
String username = usernameField.getText();
String password = new String(passwordField.getPassword());
return "admin".equals(username) && "admin1".equals(password);
}

}

class Frame2 extends JFrame {

private static final long serialVersionUID = 6055147068340138622L;

private static final int text_size = 15;

int n = 0;

private final JLabel q1 = new JLabel("问题1:test");
private final JLabel q2 = new JLabel("问题2:test1");
private final JLabel q3 = new JLabel("问题3:test2");
private final JTextField a1 = new JTextField("", text_size);
private final JTextField a2 = new JTextField("", text_size);
private final JTextField a3 = new JTextField("", text_size);
private final JButton loginButton = new JButton("确定");

protected Frame2() {
setTitle("回答问题");
setLayout(new FlowLayout());
add(q1);
add(a1);
add(q2);
add(a2);
add(q3);
add(a3);
add(loginButton);

loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (login()) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new Frame3();
}
});
Frame2.this.dispose();
} else {
n++;
if (n > 2)
JOptionPane.showMessageDialog(Frame2.this, "错误超过两次了", "stop", JOptionPane.ERROR_MESSAGE);
else
JOptionPane.showMessageDialog(Frame2.this, "STOP", "stop", JOptionPane.ERROR_MESSAGE);
}
}
});

setSize(200, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = getSize();
setLocation( (screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
}

private boolean login() {
String as1 = a1.getText();
String as2 = a2.getText();
String as3 = a3.getText();
return "yes".equals(as1) && "yes".equals(as2) && "yes".equals(as3);
}

}

class Frame3 extends JFrame {

private static final long serialVersionUID = 6055147068340138622L;

private final JLabel l = new JLabel("恭喜通关");

protected Frame3() {
setTitle("恭喜通关");
setLayout(new FlowLayout());
add(l);

setSize(200, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = getSize();
setLocation( (screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
}

}

回答2:

逻辑很清晰,不过非得用窗体实现吗,浏览器结构不行啊

回答3:

.......,额,好吧,我帮你,请问如何联系到您

回答4:

饿。窗体不会的路过~