【问题标题】:GUI not appearing图形界面没有出现
【发布时间】:2013-03-25 07:33:19
【问题描述】:

我有一个 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 上运行主类,但它会在几秒钟后运行然后停止。我没有收到任何错误。大家有什么建议吗?谢谢。

【问题讨论】:

    标签: java swing jframe visible


    【解决方案1】:

    你还没有打电话给JFrame#setVisible

    gui.setVisible(true);
    

    一些注意事项:

    • 避免使用null 布局。始终使用layout manager
    • 不要使用 setXXXSize 方法。覆盖 getPreferredSize 方法以确定组件大小
    • 您通常希望创建一个并直接使用,而不是扩展 JFrame
    • 考虑使用initial threadsEDT 中创建JFrame

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:
        public class appGUI extends JFrame {
        
            setVisible(true);
        
           // ----
        }
        

        放置此代码以使其可见。

        【讨论】:

        • 非常感谢,但我采用了不同的方式:appGUI gui = new appGUI(); gui.setVisible(true);
        【解决方案4】:

        只需添加:

        this.setVisible(true);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-03-25
          • 1970-01-01
          • 2010-10-11
          • 1970-01-01
          • 2010-12-10
          • 1970-01-01
          • 2016-01-30
          • 2014-11-04
          相关资源
          最近更新 更多