2017-02-28 38 views
1

我已经将initialize方法设为public,并没有帮助,并且我已经将this和external类设置为true,如下所示, 任何帮助,将不胜感激。我使用eclipse的window builder工具创建了gui在另一个类中创建我的Gui对象时,框架加载但内部没有任何内容

GeneralWindow frame = new GeneralWindow(); 
         frame.setVisible(true); 

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


} 

回答

1

在你的初始化()只是改变帧这个关键字 如

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

那它的乌尔做...

+0

工作,谢谢你向我解释。 –

+1

不是一个真正的解释,只是解决方案[: - | –

2

您正在使类型为GeneralWindow的框架frame可见。但是你绝不会向该框架添加任何组件。相反,您的初始化方法会创建另一个框架,并为该框架添加许多组件。不要创建其他框架,而是将组件添加到this

+0

你能否写一个例子,因为我在理解如何做这件事时有点困难。 –

+0

从GeneralWindow中删除'private JFrame frame;'。然后用'this'替换GeneralWindow中'frame'的所有出现。哦,不要使用空布局,也不要设置组件的边界。使用布局管理器,如Swing教程中所述。 –

1

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

}