Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当在另一个类中创建我的Gui对象时,框架会加载,但内部不会显示任何内容_Java_Eclipse_User Interface_Windowbuilder - Fatal编程技术网

Java 当在另一个类中创建我的Gui对象时,框架会加载,但内部不会显示任何内容

Java 当在另一个类中创建我的Gui对象时,框架会加载,但内部不会显示任何内容,java,eclipse,user-interface,windowbuilder,Java,Eclipse,User Interface,Windowbuilder,我已经公开了initialize方法,没有帮助,并且我已经将visible设置为true,包括下面所示的外部类,任何帮助都将不胜感激。我使用eclipse中的WindowBuilder工具创建了gui GeneralWindow frame = new GeneralWindow(); frame.setVisible(true); 软件包图形用户界面 import java.awt.EventQueue; import javax.swing

我已经公开了initialize方法,没有帮助,并且我已经将visible设置为true,包括下面所示的外部类,任何帮助都将不胜感激。我使用eclipse中的WindowBuilder工具创建了gui

GeneralWindow frame = new GeneralWindow();
                        frame.setVisible(true);
软件包图形用户界面

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import java.awt.Font;

public class GeneralWindow extends JFrame{

    private JFrame frame;
    private JTextField textField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {

        try {
            for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception e) {
            // If Nimbus is not available, you can set the GUI to another look and feel.
        }

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    GeneralWindow window = new GeneralWindow();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

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

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JButton btnNewButton = new JButton("Order");
        btnNewButton.setBounds(309, 12, 115, 23);
        frame.getContentPane().add(btnNewButton);

        JButton btnNewButton_1 = new JButton("Search");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        btnNewButton_1.setBounds(309, 46, 115, 23);
        frame.getContentPane().add(btnNewButton_1);

        JButton btnNewButton_2 = new JButton("Stock");
        btnNewButton_2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
            }
        });
        btnNewButton_2.setBounds(309, 80, 115, 23);
        frame.getContentPane().add(btnNewButton_2);

        JButton btnNewButton_3 = new JButton("Emplyoees");
        btnNewButton_3.setBounds(309, 114, 115, 23);
        frame.getContentPane().add(btnNewButton_3);

        JButton btnNewButton_4 = new JButton("Price Amend");
        btnNewButton_4.setBounds(309, 148, 115, 23);
        frame.getContentPane().add(btnNewButton_4);

        JButton btnNewButton_5 = new JButton("Total");
        btnNewButton_5.setBounds(309, 182, 115, 23);
        frame.getContentPane().add(btnNewButton_5);

        textField = new JTextField();
        textField.setBounds(10, 228, 178, 20);
        frame.getContentPane().add(textField);
        textField.setColumns(10);

        JLabel lblProductcodeBar = new JLabel("Productcode Bar");
        lblProductcodeBar.setFont(new Font("Tahoma", Font.BOLD, 11));
        lblProductcodeBar.setBounds(10, 209, 125, 14);
        frame.getContentPane().add(lblProductcodeBar);

        JButton btnEnter = new JButton("Enter");
        btnEnter.setBounds(198, 227, 89, 23);
        frame.getContentPane().add(btnEnter);

        JTextArea textArea = new JTextArea();
        textArea.setBounds(10, 11, 277, 196);
        frame.getContentPane().add(textArea);
    }


}

您正在使GeneralWindow类型的框架可见。但您从未向该框架添加任何组件。相反,initialize方法会创建另一个帧,并向该帧添加许多组件。不要创建另一个框架,而是将组件添加到此。

您正在使GeneralWindow类型的框架可见。但您从未向该框架添加任何组件。相反,initialize方法会创建另一个帧,并向该帧添加许多组件。不要创建另一个框架,而是将组件添加到

在initialize()中,只需将框架更改为该关键字即可 e、 g

在你的initialize()中,它已经完成了…

,只需将帧更改为该关键字即可 e、 g


虽然这里提供的两个答案给出了正确的解决方案,但它们都强化了您应该扩展JFrame的观点。一般来说,这不是一个好建议,因为您应该更喜欢组合而不是继承。实际上,您编写代码以将JFrame作为私有成员包含的方式是正确的

我已经包含了一个代码的精简版本,它不扩展
JFrame
,而是在调用
createAndDisplayFrame()
方法时创建
JFrame
的实例

public class GeneralWindow {

    private JFrame frame;
    private JButton orderButton;

    public static void main(final String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                GeneralWindow window = new GeneralWindow();
                window.crateAndDisplayFrame();
            }
        });
    }

    public void crateAndDisplayFrame() {
        initialize();
        frame.setVisible(true);
    }

    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        orderButton = new JButton("Order");
        orderButton.setBounds(309, 12, 115, 23);
        frame.getContentPane().add(orderButton);
    }

}

虽然这里提供的两个答案给出了正确的解决方案,但它们都强化了您应该扩展JFrame的观点。一般来说,这不是一个好建议,因为您应该更喜欢组合而不是继承。实际上,您编写代码以将JFrame作为私有成员包含的方式是正确的

我已经包含了一个代码的精简版本,它不扩展
JFrame
,而是在调用
createAndDisplayFrame()
方法时创建
JFrame
的实例

public class GeneralWindow {

    private JFrame frame;
    private JButton orderButton;

    public static void main(final String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                GeneralWindow window = new GeneralWindow();
                window.crateAndDisplayFrame();
            }
        });
    }

    public void crateAndDisplayFrame() {
        initialize();
        frame.setVisible(true);
    }

    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        orderButton = new JButton("Order");
        orderButton.setBounds(309, 12, 115, 23);
        frame.getContentPane().add(orderButton);
    }

}

你能不能写一个例子,因为我在理解如何做时有点困难从常规窗口。然后将常规窗口中出现的所有
替换为
。哦,不要使用空布局,也不要设置组件的边界。使用布局管理器,正如Swing教程中所解释的。你能写一个例子吗?因为我在理解如何做时有点困难从常规窗口。然后将常规窗口中出现的所有
替换为
。哦,不要使用空布局,也不要设置组件的边界。使用布局管理器,如Swing教程中所述。