在另一个类中创建我的GUI对象时,帧加载,但内部没有任何内容



我已经公开了初始化方法,没有帮助,我将在此处和外部类都可以看到,如下所示,任何帮助都将不胜感激。我使用eclipse

的窗口构建器工具创建了GUI
GeneralWindow frame = new GeneralWindow();
                        frame.setVisible(true);

包gui;

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);
    }

}

您正在制作gerledwindow类型的框架frame,可见。但是您永远不会在该框架中添加任何组件。相反,您的初始化方法会创建另一个帧,并在该框架中添加了许多组件。不要创建另一个帧,而是将组件添加到this,而是。

在您的初始化()中只需将框架更改为此关键字例如

 //Remove this line
 frame = new JFrame();
 //change frame to this keyword
 this.setBounds(100, 100, 450, 300);
 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 this.getContentPane().setLayout(null);

它完成了...

虽然这里提供的两个答案提供了正确的解决方案,但它们都强化了您应该扩展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);
    }
}

最新更新