2016-04-12 7 views
0
import javax.swing.*; 

import java.awt.*; 
import java.awt.event.*; 

public class null_login_type extends JFrame{ 

    private JLabel admin_password_label,heading,login_label,password_label,id_label; 
    private JButton user_login_button,admin_login_button,enquiry_button,logins1,signup; 
    private JTextField user_field,password_field,admin_field,admin_password_field; 
    private ButtonGroup bg; 
    null_login_type() 
    { 
     this.setLayout(null); 
     user_login_button = new JButton("Login as User"); 
     logins1 = new JButton("Login"); 
     user_field = new JTextField("User_field"); 
     id_label = new JLabel("Id_label"); 


     user_login_button.setBounds(0,100, 150, 30); 
     logins1.setBounds(250,200,100,30); 
     user_field.setBounds(200,60,150,30); 

     add(user_login_button); 

     event e = new event(); 
     user_login_button.addActionListener(e); 
    } 
    public class event implements ActionListener 
    { 
     public void actionPerformed(ActionEvent e){ 

      logins1.setBounds(250,60,150,30);; 
      // user_field.setBounds(250,60,150,30);; 
      // add(user_field); 
      add(logins1); 
      logins1.setVisible(true); 
     } 
    } 


    public static void main(String args[]) 
    { 
     null_login_type gui = new null_login_type(); 

     gui.setSize(420,300); 
     gui.setLocation(530,200); 
     gui.setVisible(true); 
     gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);; 

    } 
} 

ボタンは表示されません。 私はボタンが来るlogins1領域にマウスを取ると、それはそこです。ボタンJava GUI(Swing)でマウスを動かした後に来る?エルス

私はアクションリスナーにコメントセクションを追加し、アクションリスナーにlogins1を削除した場合、出力は私がそれの上でマウスを取らない場合でも、印刷されます。

+0

イベントディスパッチャスレッドですべてのGUIを作成する必要があります。 'SwingUtilities.invokeLater()'を見てください。 –

+0

SwingUtilities.invokeLater()は後でボタンを表示します。しかし、私はbuttonlogins1が、すぐに私は –

+0

あなたは本当にのLayoutManagerの使用をしなければならないuser_login_buttonクリックとして来てほしいです。ヌルレイアウトを使用し、子コンポーネントを自分で配置しようとするのは非常に困難です。ピクセル完全配置は、使用されているフォントとL&Fに依存し、ユーザーがウィンドウのサイズを変更するとうまく反応しないため、ほぼ不可能です。 – FredK

答えて

1

あなたは直後参照、repaint()revalidate()を呼び出す必要が何かの内容を変更する場合。また、(デフォルトに頼るよりも常により良い)EDTであなたのGUIを作成し、あなたのJFramePanelを使用する必要があります。

import java.awt.event.*; 

public class null_login_type extends JFrame{  
    private JLabel admin_password_label,heading,login_label,password_label,id_label; 
    private JButton user_login_button,admin_login_button,enquiry_button,logins1,signup; 
    private JTextField user_field,password_field,admin_field,admin_password_field; 
    private ButtonGroup bg; 
    private JPanel panel; 
    null_login_type() 
    { 
    panel = new JPanel(); 
    this.setContentPane(panel); 
    this.setLayout(null); 
    user_login_button = new JButton("Login as User"); 
    logins1 = new JButton("Login"); 
    user_field = new JTextField("User_field"); 
    id_label = new JLabel("Id_label");   
    user_login_button.setBounds(0,100, 150, 30); 
    logins1.setBounds(250,200,100,30); 
    user_field.setBounds(200,60,150,30);   
    panel.add(user_login_button);  
    event e = new event(); 
    user_login_button.addActionListener(e); 
    } 
    public class event implements ActionListener 
    { 
    public void actionPerformed(ActionEvent e){   
     logins1.setBounds(250,60,150,30);; 
     // user_field.setBounds(250,60,150,30);; 
     // add(user_field); 
     panel.add(logins1); 
     panel.repaint(); 
     panel.revalidate(); 
    } 
    } 

    public static void main(String args[]) 
    { 
    SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      null_login_type gui = new null_login_type();   
      gui.setSize(420,300); 
      gui.setLocation(530,200); 
      gui.setVisible(true); 
      gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);; 
     } 
     }); 
    } 
} 

もすなわちNullLoginType、あなたのクラスに名前を付けるために、標準的な規則を使用してください。あなたの内部クラスeventを呼び出さないで、Eventクラスが存在し、より適切なものを選んでください。イベントを表すのではなくイベントハンドラを表します。

+0

ありがとう@ Jean-Baptiste。それは働いた –

関連する問題