JAVA语言编写1个事件处理图象用户界面 30
展开全部
我写了一个登录的dialog代码 有登陆和关闭 输入用户名:Admin和密码123后 提示可以进入主页面 下面是源代码
import javax.swing.JDialog;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.Rectangle;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.JPasswordField;
public class LoginDialog extends JDialog {
private JPanel jPanel = null;
private JButton loginButton = null;
private JButton clolseButton = null;
private JLabel usernameLabel = null;
private JLabel passwordLabel = null;
private JTextField usernameField = null;
private JPasswordField passwordField = null;
/**
* This method initializes
*
*/
public LoginDialog() {
super();
initialize();
}
/**
* This method initializes this
*
*/
private void initialize() {
this.setSize(500,320);
this.setContentPane(getJPanel());
this.setModal(false);
this.setTitle("登录");
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
private void closeDialog()
{
this.dispose();
}
/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel() {
if (jPanel == null) {
passwordLabel = new JLabel();
passwordLabel.setBounds(new Rectangle(140, 117, 52, 32));
passwordLabel.setText("密 码:");
usernameLabel = new JLabel();
usernameLabel.setBounds(new Rectangle(139, 55, 52, 32));
usernameLabel.setText("用户名:");
jPanel = new JPanel();
jPanel.setLayout(null);
jPanel.add(getJButton(), null);
jPanel.add(getJButton1(), null);
jPanel.add(usernameLabel, null);
jPanel.add(passwordLabel, null);
jPanel.add(getJTextField(), null);
jPanel.add(getJPasswordField(), null);
}
return jPanel;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (loginButton == null) {
loginButton = new JButton();
loginButton.setBounds(new Rectangle(138, 219, 80, 32));
loginButton.setText("登录");
loginButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
String username = usernameField.getText();
String password = passwordField.getText();
if (username == null || username.equals("")) {
javax.swing.JOptionPane.showMessageDialog(null,
"用户名不能为空!");
} else if (password == null || password.equals("")) {
javax.swing.JOptionPane.showMessageDialog(null,
"密码不能为空!");
} else if (username.equals("Admin")
&& password.equals("123")) {
javax.swing.JOptionPane.showMessageDialog(null,
"用户名密码正确 可以进入主页面!");
} else
{
javax.swing.JOptionPane.showMessageDialog(null,
"用户名密码不正确!");
usernameField.setText("");
passwordField.setText("");
}
}
});
}
return loginButton;
}
/**
* This method initializes jButton1
*
* @return javax.swing.JButton
*/
private JButton getJButton1() {
if (clolseButton == null) {
clolseButton = new JButton();
clolseButton.setBounds(new Rectangle(288, 218, 80, 32));
clolseButton.setText("关闭");
clolseButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.exit(0);
}
});
}
return clolseButton;
}
/**
* This method initializes jTextField
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField() {
if (usernameField == null) {
usernameField = new JTextField();
usernameField.setBounds(new Rectangle(191, 53, 165, 35));
}
return usernameField;
}
/**
* This method initializes jPasswordField
*
* @return javax.swing.JPasswordField
*/
private JPasswordField getJPasswordField() {
if (passwordField == null) {
passwordField = new JPasswordField();
passwordField.setBounds(new Rectangle(191, 114, 165, 35));
}
return passwordField;
}
public static void main(String args[]) {
LoginDialog dialog = new LoginDialog();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
dialog.setBounds((screenSize.width - 500) / 2,
(screenSize.height - 320) / 2, 500, 320);
dialog.show();
}
} // @jve:decl-index=0:visual-constraint="174,13"
import javax.swing.JDialog;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.Rectangle;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.JPasswordField;
public class LoginDialog extends JDialog {
private JPanel jPanel = null;
private JButton loginButton = null;
private JButton clolseButton = null;
private JLabel usernameLabel = null;
private JLabel passwordLabel = null;
private JTextField usernameField = null;
private JPasswordField passwordField = null;
/**
* This method initializes
*
*/
public LoginDialog() {
super();
initialize();
}
/**
* This method initializes this
*
*/
private void initialize() {
this.setSize(500,320);
this.setContentPane(getJPanel());
this.setModal(false);
this.setTitle("登录");
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
private void closeDialog()
{
this.dispose();
}
/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel() {
if (jPanel == null) {
passwordLabel = new JLabel();
passwordLabel.setBounds(new Rectangle(140, 117, 52, 32));
passwordLabel.setText("密 码:");
usernameLabel = new JLabel();
usernameLabel.setBounds(new Rectangle(139, 55, 52, 32));
usernameLabel.setText("用户名:");
jPanel = new JPanel();
jPanel.setLayout(null);
jPanel.add(getJButton(), null);
jPanel.add(getJButton1(), null);
jPanel.add(usernameLabel, null);
jPanel.add(passwordLabel, null);
jPanel.add(getJTextField(), null);
jPanel.add(getJPasswordField(), null);
}
return jPanel;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (loginButton == null) {
loginButton = new JButton();
loginButton.setBounds(new Rectangle(138, 219, 80, 32));
loginButton.setText("登录");
loginButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
String username = usernameField.getText();
String password = passwordField.getText();
if (username == null || username.equals("")) {
javax.swing.JOptionPane.showMessageDialog(null,
"用户名不能为空!");
} else if (password == null || password.equals("")) {
javax.swing.JOptionPane.showMessageDialog(null,
"密码不能为空!");
} else if (username.equals("Admin")
&& password.equals("123")) {
javax.swing.JOptionPane.showMessageDialog(null,
"用户名密码正确 可以进入主页面!");
} else
{
javax.swing.JOptionPane.showMessageDialog(null,
"用户名密码不正确!");
usernameField.setText("");
passwordField.setText("");
}
}
});
}
return loginButton;
}
/**
* This method initializes jButton1
*
* @return javax.swing.JButton
*/
private JButton getJButton1() {
if (clolseButton == null) {
clolseButton = new JButton();
clolseButton.setBounds(new Rectangle(288, 218, 80, 32));
clolseButton.setText("关闭");
clolseButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.exit(0);
}
});
}
return clolseButton;
}
/**
* This method initializes jTextField
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField() {
if (usernameField == null) {
usernameField = new JTextField();
usernameField.setBounds(new Rectangle(191, 53, 165, 35));
}
return usernameField;
}
/**
* This method initializes jPasswordField
*
* @return javax.swing.JPasswordField
*/
private JPasswordField getJPasswordField() {
if (passwordField == null) {
passwordField = new JPasswordField();
passwordField.setBounds(new Rectangle(191, 114, 165, 35));
}
return passwordField;
}
public static void main(String args[]) {
LoginDialog dialog = new LoginDialog();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
dialog.setBounds((screenSize.width - 500) / 2,
(screenSize.height - 320) / 2, 500, 320);
dialog.show();
}
} // @jve:decl-index=0:visual-constraint="174,13"
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询