JAR文件混乱GUI?

时间:2014-05-30 20:25:02

标签: java eclipse swing user-interface jar

我做了一些功课来制作一个简单的登录窗口。 完成编码后,我尝试将文件导出到一个可运行的jar文件中,运行jar文件后,我看到它搞砸了GUI。 由于某种原因,JPasswordField填满了我的整个JFrame。我不知道可能是什么问题,因为它从IDE运行良好。有什么帮助吗?

What I want it to look like

What it looks like when I run the JAR

这里是代码(在eclipse IDE中正常工作):`

public class Login extends JFrame {

private static final long serialVersionUID = 1L;
private static JTextField user = new JTextField();
private static JPasswordField pass = new JPasswordField();
private static JButton Loginbtn = new JButton("Login");
protected String[] args;
private static JFrame frame = new JFrame("Log In");


public Login(){

    Loginbtn.addActionListener(new ActionListener(){
        @SuppressWarnings("deprecation")
        public void actionPerformed(ActionEvent e){
            if(user.getText().equals("Admin") && pass.getText().equals("Nimda")){

                //System.out.println("Hello ADMIN!");
                JOptionPane.showMessageDialog(null, "Logged In!");
                MyLog.main(args);
                frame.setVisible(false);
            }
            else{

                //System.out.println("Login error!");
                JOptionPane.showMessageDialog(null, "Login Error!");
            }
        }
    });

}

public static void main(String[] args) {

    @SuppressWarnings("unused")
    Login login = new Login();
    JLabel username = new JLabel("Username");
    JLabel password = new JLabel("Password");

    frame.setSize(260, 200);
    frame.setResizable(false);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

    username.setBounds(20, 60, 100, 20);
    password.setBounds(20, 85, 100, 20);
    pass.setBounds(85, 85, 150, 20);
    user.setBounds(85, 60, 150, 20);
    Loginbtn.setBounds(88, 110, 50, 20);

    frame.add(username);
    frame.add(password);
    frame.add(Loginbtn);
    frame.add(user);
    frame.add(pass);

}          
}

先谢谢你们! 干杯!

1 个答案:

答案 0 :(得分:2)

在将组件添加到JFrame之前调用setVisible会导致最有可能出现此问题。您应该在调用setVisible之前将所需的所有组件添加到JFrame中,因为setVisible会验证这些组件(将它们排出)。

What setVisible does from the API, you'll see it talks about validating the components

What validate does from API