2016-04-12 47 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);; 

    } 
} 

否则该按钮不可见。 当我将鼠标放在登录区域1上时,按钮就会出现在那里。按钮在Java GUI(Swing)中使用鼠标后即可使用?

如果我在动作侦听器中添加注释部分并删除动作侦听器中的logins1,即使我没有在其上使用鼠标,也会打印输出。

+0

你需要在事件分派器线程中创建所有的GUI,看看'SwingUtilities.invokeLater()'。 –

+0

SwingUtilities.invokeLater()将使按钮稍后出现。但是我希望buttonlogins1在我点击user_login_button时立即来到 –

+0

您确实应该使用LayoutManager;使用空布局并尝试自己放置子组件非常困难。像素完美的放置几乎是不可能的,因为它取决于正在使用的字体和L&F,并且在用户调整窗口大小时不会很好地响应。 – FredK

回答

1

如果您修改了需要拨打repaint()revalidate()的内容,请参阅后面的内容。您还需要建立在你的EDT GUI,并在使用PanelJFrame(总是比依赖于默认越好):

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,选择一些更合适的,它不代表事件,但事件处理程序...

+0

谢谢@ Jean-Baptiste。这工作 –

相关问题