2013-06-29 8 views
2

JDialog를 모달
하지만 .. JDialog의 모달을 드릴 수 없습니다
또한 setModal (참) 등을 시도 setModalityType (ModalityType.DOCUMENT_MODAL); 그러나 운 ... (I 이클립스의 WindowBuilder 플러그인을 사용하고 있습니다)
PS : 나는 자바에 새로운 오전 ..이
여기 내 코드입니다 ...수 없습니다 나는 버튼을 클릭 "등록"을 할 때 .. JDialog를 새롭게을 열려고하고

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 

    } 
} 

답변

7

당신이 시도하기 전에 대화 상자 모달을 시도하고 그것을 보이게하십시오. 대화 상자가 표시되면 모달 상태를 변경할 수 없습니다.

JDialog jd=new JDialog(loginpage.this,"User Registration"); 
jd.setModal(true); 
jd.setLayout(null); // THIS IS A BAD IDEA // 
jd.setLocationRelativeTo(null); 
// This is somewhat pointless, you've set relative location, but know overridden it... 
// You should also be relying on the layout manager and pack to determine the size... 
jd.setBounds(400,300, 479, 329); 
jd.setResizable(false); 
setLocationRelativeTo(loginpage.this); 

// Add you other components 

jd.setVisible(true); 
+0

아 ... 모달이되었지만 이제는 JDialog가 왼쪽 상단의 작은 창으로 표시됩니다. !! ! : -/ –

+0

레이아웃 관리자가 없습니다. – MadProgrammer

+0

대체 솔루션 ?? –