新建按钮窗口和UI对齐方式



我创建了一个主窗口,如果用户是系统管理员、员工或财务成员,他将在其中单击,我的问题之一是他们没有集中在屏幕上,我该怎么做?其次,我希望它能像这样工作,当我点击财务按钮时,主窗口将关闭,它将把我带到我的登录屏幕,我该怎么做??这是我的主窗口代码

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.CardLayout;
import javax.swing.JEditorPane;
import javax.swing.SpringLayout;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JFormattedTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

    public class MainWindow extends JFrame {
        private JPanel contentPane;
        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        MainWindow frame = new MainWindow();
                        frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
        /**
         * Create the frame.
         */
        public MainWindow() {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(100, 100, 333, 191);
            contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
            setContentPane(contentPane);
            contentPane.setLayout(new BorderLayout(0, 0));
            JButton btnNewButton = new JButton("Employee");
            contentPane.add(btnNewButton, BorderLayout.WEST);
            JButton btnNewButton_1 = new JButton("Finance");
            btnNewButton_1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    Login login = new Login();
                }
            });
            contentPane.add(btnNewButton_1, BorderLayout.CENTER);
            JButton btnNewButton_2 = new JButton("System Admin");
            contentPane.add(btnNewButton_2, BorderLayout.EAST);
            JLabel lblNewLabel = new JLabel("Welcome");
            contentPane.add(lblNewLabel, BorderLayout.NORTH);
        }
    }

这是我的登录表单的代码

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Login extends JFrame {
    private JLabel label1, label2;
    private JButton submit;
    private JTextField textfield1;
    private JPasswordField passfield;
    private JPanel panel;
    public Login() {
        setSize(300, 100);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        label1 = new JLabel("User ID:");
        textfield1 = new JTextField(15);
        label2 = new JLabel("Password:");
        passfield = new JPasswordField(15);
        submit = new JButton("Submit");
        panel = new JPanel(new GridLayout(3, 1));
        panel.add(label1);
        panel.add(textfield1);
        panel.add(label2);
        panel.add(passfield);
        panel.add(submit);
        add(panel, BorderLayout.CENTER);
        ButtonHandler handler = new ButtonHandler();
        submit.addActionListener(handler);
    }// end login constructor
    private class ButtonHandler implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            String user = textfield1.getText();
            char[] passChars = passfield.getPassword();
            Connection conn = Jdbc.dbConn();
            PreparedStatement ps = null;
            ResultSet rs = null;
            String pass = new String(passChars);
            if (passChars != null) {
                String sql = "SELECT employee_ID, employee_password FROM  user WHERE employee_ID = ? AND employee_password = ?";
                try {
                    ps = conn.prepareStatement(sql);
                    ps.setString(1, user);
                    ps.setString(2, pass);
                    rs = ps.executeQuery();
                    if (rs.next()) {
                        JOptionPane.showMessageDialog(null,"Welcome! "+user);
                    } else {
                        JOptionPane.showMessageDialog(null, "Wrong Input");
                    }
                } catch (Exception e) {
                        e.printStackTrace();
                } finally {
                    try {
                        rs.close();
                        ps.close();
                        conn.close();
                    } catch (Exception ee) {
                            ee.printStackTrace();
                    }
                }
            }// end actionPerformed
        }// End ButtonHandler
    }// End of class
}

一些建议:

  1. 不要将setBounds()用于MainWindow(JFrame(。使用一些布局,最后使用pack()。如果您想手动设置大小,那么您也可以使用setSize()
  2. 要关闭当前窗口并打开Login框架,请添加setVisible(false)dispose(),然后创建Login对象并使其可见。

  3. 要使框架居中,请尝试setLocationRelativeTo(null);

  4. 不要使用label1textFiled2btnNewButton等变量名。请为反映其用法的适当变量使用适当的名称

第2点示例:

    btnNewButton_1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            setVisible(false);
            Login login = new Login();
        }
    });

您需要仔细选择适合您需求的布局管理器。您当前使用的是BorderLayout,它似乎不能满足您的要求。

尝试将您的三个按钮添加到JPanel,然后将该面板设置为框架的内容窗格。JPanel在默认情况下使用FlowLayout,这应该起到作用。

最新更新