0

我有一个 Main 类,它有一个 public static void main(String[] args) {}。我还有一个名为 appGUI 的类。当我运行 Main 类时,我一直试图让 GUI 加载,但什么也没发生,甚至没有任何错误...... :(

这是主类:

    public class Main {

    /**
     * @param args
     */

    public static void main(String[] args) throws Exception {

        appGUI gui = new appGUI();
    }

}

这是 appGUI 类:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*
 * Created by JFormDesigner on Wed Apr 03 19:24:35 BST 2013
 */



/**
 * @author Hrach Ghapantsyan
 */
public class appGUI extends JFrame {
    public appGUI() {
        initComponents();
    }

    private void loginButtonActionPerformed(ActionEvent e) {
        // TODO add your code here
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        // Generated using JFormDesigner Evaluation license - Hrach Ghapantsyan
        loginPasswordField = new JPasswordField();
        loginUsernameField = new JTextField();
        usernameLabel = new JLabel();
        passwordLabel = new JLabel();
        loginButton = new JButton();
        titleLabel = new JLabel();

        //======== this ========
        setTitle("Experimental X | Administrator Login");
        Container contentPane = getContentPane();
        contentPane.setLayout(null);
        contentPane.add(loginPasswordField);
        loginPasswordField.setBounds(80, 65, 100, loginPasswordField.getPreferredSize().height);
        contentPane.add(loginUsernameField);
        loginUsernameField.setBounds(80, 35, 100, loginUsernameField.getPreferredSize().height);

        //---- usernameLabel ----
        usernameLabel.setText("Username:");
        contentPane.add(usernameLabel);
        usernameLabel.setBounds(20, 40, 55, usernameLabel.getPreferredSize().height);

        //---- passwordLabel ----
        passwordLabel.setText("Password:");
        contentPane.add(passwordLabel);
        passwordLabel.setBounds(20, 70, 55, passwordLabel.getPreferredSize().height);

        //---- loginButton ----
        loginButton.setText("Login");
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                loginButtonActionPerformed(e);
            }
        });
        contentPane.add(loginButton);
        loginButton.setBounds(80, 95, 100, loginButton.getPreferredSize().height);

        //---- titleLabel ----
        titleLabel.setText("Experimental X | Administrator Login");
        contentPane.add(titleLabel);
        titleLabel.setBounds(45, 10, 190, titleLabel.getPreferredSize().height);

        { // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        setSize(270, 170);
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    // Generated using JFormDesigner Evaluation license - Hrach Ghapantsyan
    private JPasswordField loginPasswordField;
    private JTextField loginUsernameField;
    private JLabel usernameLabel;
    private JLabel passwordLabel;
    private JButton loginButton;
    private JLabel titleLabel;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}

我曾尝试在 Eclipse 和 netbeans 上运行主类,但它会在几秒钟后运行然后停止。我没有收到任何错误。大家有什么建议吗?谢谢。

4

4 回答 4

1

你还没有打电话JFrame#setVisible

gui.setVisible(true);

一些注意事项:

  • 避免使用null布局。始终使用布局管理器
  • 不要使用setXXXSize方法。getPreferredSize确定组件大小的覆盖方法
  • 您通常不想扩展 a JFrame,而是希望创建一个并直接使用。
  • 考虑JFrame使用初始线程EDT创建
于 2013-04-03T14:51:42.033 回答
0

确保在 GUI 上调用 setVisible(true)。

于 2013-04-03T14:52:38.783 回答
0
public class appGUI extends JFrame {

    setVisible(true);

   // ----
}

放置此代码以使其可见。

于 2013-04-03T14:52:46.203 回答
0

只需添加:

this.setVisible(true);
于 2013-04-03T14:53:27.647 回答