Java简单的GUI程序: 在两个输入框中输入字符串,判断输入的两个值是否一样
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class Test extends JFrame implements ActionListener {
static String[] s = new String[4];
private static final long serialVersionUID = 1L;
JTextField username = new JPasswordField(20);
JTextField usernamecheck = new JPasswordField(20);
JButton submit = new JButton("Submit");
public Test() {
JPanel contentPane = new JPanel();
contentPane.add(new JLabel("\n\n\nUsername: "));
contentPane.add(username);
contentPane.add(new JLabel("\n\n\nusernamecheck:"));
contentPane.add(usernamecheck);
contentPane.add(submit);
submit.addActionListener((this));
add(contentPane);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(350, 200, 800, 480);
setTitle("Window");
setResizable(true);
setVisible(true);
setContentPane(contentPane);
contentPane.setLayout(null);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String user = username.getText();
String usercheck = usernamecheck.getText();
if (user.equals(usercheck))
{
JOptionPane.showMessageDialog(null, "The same!");
}
else
{
JOptionPane.showMessageDialog(null, "Different!");
}
}
public static void main(String[] args) {
new Test();
}
}