单击按钮时,不会调用下一个活动

时间:2016-02-20 09:29:57

标签: java swing jframe jbutton

请帮帮我。我是java的初学者。这是我的 StartUp_page 按钮代码。单击ok按钮后,应调用 AdminLogin 。它显示1,但AdminLogin没有被调用。

StartUp_page

btnOk.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                String input = textField.getText();
                if (input.equals("1"))
                {
                    AdminLogin login=new AdminLogin();
                    login.setVisible(true);
                    setVisible(false);
                    System.out.println(input);
                    //dispose();
                }
                else
                {
                    System.out.println("2");                }
            }

后台管理

  package GUI;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class AdminLogin extends JFrame{


    private JTextField textField;
    private JTextField textField_1;
    private JPanel contentPanel;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    AdminLogin frame = new AdminLogin();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public AdminLogin() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100,100,350,300);
        contentPanel= new JPanel();
        contentPanel.setBorder(new EmptyBorder(5,5,5,5));
        setContentPane(contentPanel);
        contentPanel.setLayout(null);


        JLabel lblName = new JLabel("Name");
        lblName.setBounds(53, 69, 46, 14);
        contentPanel.add(lblName);

        textField = new JTextField();
        textField.setBounds(119, 66, 121, 20);
        contentPanel.add(textField);
        textField.setColumns(10);

        JLabel lblPassword = new JLabel("Password");
        lblPassword.setBounds(53, 128, 46, 14);
        contentPanel.add(lblPassword);

        textField_1 = new JTextField();
        textField_1.setBounds(119, 125, 121, 20);
        contentPanel.add(textField_1);
        textField_1.setColumns(10);

        JButton btnNewButton = new JButton("OK");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
            }
        });
        btnNewButton.setBounds(211, 183, 89, 23);
        contentPanel.add(btnNewButton);
    }

    public void setVisible(boolean b) {
        // TODO Auto-generated method stub

    }

}

1 个答案:

答案 0 :(得分:1)

基本问题是,你AdminLogin类是从JFrame扩展而是从类中创建第二个JFrame实例,所以实际显示了哪个帧?!就个人而言,我首先将extends JFrame替换为extends JPanel,然后根据需要将面板添加到JFrameJDialog的实例

你真的应该避免使用null布局,像素完美布局是现代ui设计中的错觉。影响组件个体大小的因素太多,您无法控制。 Swing旨在与布局管理器一起工作,放弃这些将导致问题和问题的终结,您将花费越来越多的时间来纠正

查看Laying Out Components Within a Container了解一些细节。

您可能还想查看The Use of Multiple JFrames, Good/Bad Practice?How to Make Dialogs以及How to Use CardLayout,了解有关替代方案的一些想法