编写一个java小程序

2024-11-23 09:20:42
推荐回答(2个)
回答1:

import java.awt.Dimension;
import java.awt.FlowLayout;
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.JTextField;

public class TestSwg extends JFrame
{

private JLabel username;
private JLabel password;
private JTextField userField;
private JTextField passField;
private JButton submit;
private JLabel result;
public TestSwg(){
username = new JLabel("用户名:");
password = new JLabel("密码:");
userField = new JTextField();
userField.setPreferredSize(new Dimension(120,20));
passField = new JTextField();
passField.setPreferredSize(new Dimension(120,20));
submit = new JButton("提交");
result = new JLabel("结果");
this.add(username);
this.add(userField);
this.add(password);
this.add(passField);

this.add(submit);
this.add(result);
this.setLayout(new FlowLayout(FlowLayout.CENTER, 3, 3));
this.setBounds(100, 100, 200, 200);
this.setVisible(true);

submit.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
if(userField.getText().equals("javalearner") && passField.getText().equals("happy")){
result.setText("javahappy");
}else{
result.setText("error data");
}
}

});
}

public static void main(String [] args){
TestSwg ts = new TestSwg();
}
}
自己写得很辛苦请采纳

回答2: