单击“注册”按钮时,我试图打开一个新的 JDialog。
但我无法使 JDialog 成为模态。
我尝试了 setModal(true) 和 setModalityType(ModalityType.DOCUMENT_MODAL); 但没有运气..(我正在使用 Eclipse 的 WindowBuilder 插件)
PS:我是 JAVA 的新手..
这是我的代码...
import java.awt.BorderLayout;
import java.awt.Dialog.ModalityType;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Frame;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.xml.ws.handler.MessageContext;
public class loginpage extends JFrame {
private JPanel contentPane;
private JTextField userid;
private JPasswordField password;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
loginpage frame = new loginpage();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public loginpage() {
super("User Login");
setResizable(false);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(400, 200, 550, 370);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(null);
setContentPane(contentPane);
JLabel label = new JLabel("Please enter your login credentials...");
label.setFont(new Font("Times New Roman", Font.PLAIN, 25));
label.setForeground(Color.BLUE);
label.setBounds(79, 11, 400, 61);
contentPane.add(label);
JLabel lblLoginId = new JLabel("User ID :");
lblLoginId.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblLoginId.setBounds(159, 142, 71, 17);
contentPane.add(lblLoginId);
JLabel lblUserType = new JLabel("User Type :");
lblUserType.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblUserType.setBounds(159, 102, 85, 17);
contentPane.add(lblUserType);
JLabel lblPassword = new JLabel("Password :");
lblPassword.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblPassword.setBounds(159, 188, 96, 14);
contentPane.add(lblPassword);
userid = new JTextField();
userid.setBounds(254, 141, 114, 20);
contentPane.add(userid);
userid.setColumns(10);
password = new JPasswordField();
password.setBounds(254, 186, 114, 20);
contentPane.add(password);
JComboBox usertype = new JComboBox();
usertype.setBounds(254, 101, 89, 20);
contentPane.add(usertype);
usertype.addItem("Admin");
usertype.addItem("Employee");
JButton btnLogin = new JButton("Login");
btnLogin.setBounds(254, 228, 71, 23);
contentPane.add(btnLogin);
JLabel lblNewEmployeeRegister = new JLabel("New employee? ");
lblNewEmployeeRegister.setForeground(Color.RED);
lblNewEmployeeRegister.setBounds(296, 304, 114, 14);
contentPane.add(lblNewEmployeeRegister);
JButton btnRegister = new JButton("Register Here");
btnRegister.setBounds(398, 301, 126, 20);
contentPane.add(btnRegister);
ImageIcon myicon = new ImageIcon("src\\logo.gif");
JLabel iconlabel = new JLabel(myicon);
iconlabel.setBounds(0, 233, 103, 99 );
iconlabel.setIcon(myicon);
contentPane.add(iconlabel);
//Adding Listeners to buttons
//Login Button Listener
btnLogin.addActionListener(new ActionListener()
{
//Making the Connection and registering the Drivers
@Override
public void actionPerformed(ActionEvent arg0) {
}//actionPerformed ends here
});//Login Button Listener ends here
//Register Button Listener
btnRegister.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JDialog jd=new JDialog(loginpage.this,"User Registration");
jd.setLayout(null);
jd.setVisible(true);
jd.setBounds(400,300, 479, 329);
jd.setResizable(false);
setLocationRelativeTo(loginpage.this);
jd.setModal(true);
JLabel lblFillUpThe = new JLabel("Fill up the form to register");
lblFillUpThe.setForeground(Color.BLUE);
lblFillUpThe.setFont(new Font("Times New Roman", Font.PLAIN, 25));
lblFillUpThe.setBounds(93, 11, 400, 61);
jd.add(lblFillUpThe);
JLabel lblUserId = new JLabel("Type in a User ID :");
lblUserId.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblUserId.setBounds(77, 96, 104, 14);
jd.add(lblUserId);
JTextField newid = new JTextField();
newid.setBounds(236, 94, 122, 20);
jd.add(newid);
newid.setColumns(10);
JLabel lblTypeA = new JLabel("Type in a new Password :");
lblTypeA.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblTypeA.setBounds(77, 142, 142, 14);
jd.add(lblTypeA);
JPasswordField newpass = new JPasswordField();
newpass.setBounds(236, 140, 122, 20);
jd.add(newpass);
JLabel lblConfirmThePassword = new JLabel("Confirm the Password :");
lblConfirmThePassword.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblConfirmThePassword.setBounds(77, 189, 130, 14);
jd.add(lblConfirmThePassword);
JPasswordField confpass = new JPasswordField();
confpass.setBounds(236, 187, 122, 20);
jd.add(confpass);
JButton reg = new JButton("Register");
reg.setBounds(239, 237, 89, 23);
jd.add(reg);
reg.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(getParent(), "User Registered Successfully..!!", "Successful",JOptionPane.INFORMATION_MESSAGE);
}
});
}
});//Register Button Listener ends here
}
}