求java程序代码

1、定义如下界面,当点击“登录”按钮时,当用户名为admin密码为123456时显示“登录成功”否则显示“登录不成功”。 当点击“退出”按钮时,关闭窗口,程序结束运行。
2、编写一个程序,其功能为:在窗口上放一个按钮,当不断单击按钮时显示它被单击的次数。

3编写一个程序设计一个界面,第一行有3个按钮,第二行中间有一个按钮,第三行有两个按钮。

第1个回答  2010-03-17
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

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

/**
* 一个简单的Swing窗口,输入内容单击“确定”按钮后,在文本域中显示输入的内容。
* 单击“取消”按钮,清空页面内容。
* @author yzg
*
*/
public class Test extends JFrame {

private static final long serialVersionUID = 1L;
private JLabel nameLabel;
private JLabel pwdLabel;
private JTextField name;
private JTextField pwd;
public Test(String title) {
super(title);
this.getContentPane().setLayout(null);
// 下面两行是取得屏幕的高度和宽度
double lx = Toolkit.getDefaultToolkit().getScreenSize().getWidth();
double ly = Toolkit.getDefaultToolkit().getScreenSize().getHeight();
this.setLocation(new Point((int) (lx / 2) - 150, (int) (ly / 2) - 200));// 设定窗口出现位置
this.setSize(340, 440);// 设定窗口大小
}

public void showWin() {
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
// 姓名
nameLabel = new JLabel("姓名 :");
nameLabel.setBounds(30, 10, 50, 25);
name = new JTextField();
name.setBounds(80, 10, 120, 20);
pwdLabel = new JLabel("密码 :");
pwdLabel.setBounds(30, 40, 50, 55);
pwd = new JPasswordField();
pwd.setBounds(80, 40, 120, 50);
// 确定按钮
JButton ok = new JButton("确定");
ok.setBounds(50, 190, 60, 25);
ok.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
if ("admin".equals(name.getText())&&"123456".equals(pwd.getText())) {
JOptionPane.showMessageDialog(getContentPane(), "登录成功");
}else {
JOptionPane.showMessageDialog(getContentPane(), "登录不成功");
}
}

public void mouseEntered(MouseEvent e) {
}

public void mouseExited(MouseEvent e) {
}

public void mousePressed(MouseEvent e) {
}

public void mouseReleased(MouseEvent e) {
}
});

// 取消按钮
JButton cancel = new JButton("取消");
cancel.setBounds(120, 190, 60, 25);
cancel.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
}

public void mouseEntered(MouseEvent e) {
}

public void mouseExited(MouseEvent e) {
}

public void mousePressed(MouseEvent e) {
}

public void mouseReleased(MouseEvent e) {
}
});

this.getContentPane().add(nameLabel);
this.getContentPane().add(pwdLabel);
this.getContentPane().add(name);
this.getContentPane().add(pwd);
this.getContentPane().add(ok);
this.getContentPane().add(cancel);

// this.pack();
this.setVisible(true);
}

/**
* @param args
*/
public static void main(String[] args) {
Test reg = new Test("test");
reg.showWin();
}

}

第二个
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
* 一个简单的Swing窗口,输入内容单击“确定”按钮后,在文本域中显示输入的内容。
* 单击“取消”按钮,清空页面内容。
* @author yzg
*
*/
public class Test extends JFrame {
private static int count=0;
private static final long serialVersionUID = 1L;
public Test(String title) {
super(title);
this.getContentPane().setLayout(null);
// 下面两行是取得屏幕的高度和宽度
double lx = Toolkit.getDefaultToolkit().getScreenSize().getWidth();
double ly = Toolkit.getDefaultToolkit().getScreenSize().getHeight();
this.setLocation(new Point((int) (lx / 2) - 150, (int) (ly / 2) - 200));// 设定窗口出现位置
this.setSize(340, 440);// 设定窗口大小
}

public void showWin() {
// 确定按钮
JButton ok = new JButton("确定");
ok.setBounds(50, 190, 60, 25);
ok.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
count++;
JOptionPane.showMessageDialog(getContentPane(),count);
}

public void mouseEntered(MouseEvent e) {
}

public void mouseExited(MouseEvent e) {
}

public void mousePressed(MouseEvent e) {
}

public void mouseReleased(MouseEvent e) {
}
});

this.getContentPane().add(ok);

// this.pack();
this.setVisible(true);
}

/**
* @param args
*/
public static void main(String[] args) {
Test reg = new Test("test");
reg.showWin();
}

}