2012-03-18 85 views
0

我有点卡住了。我应该显示2个按钮和1个标签,但我的程序没有显示它们。如果你有任何想法,这将是伟大的。我正在使用eclipse,代码正在编译和运行。这是代码。不显示按钮和标签

/** This is the driver class of the program. 
* Here is the main method with the JFrame. 
* class name RunFurniture.class 
* @author Kiril Anastasov 
* @date 18/03/2012 
*/ 
import java.awt.*; 
import javax.swing.*; 
public class RunRurniture 
{ 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) 
    { 

     JFrame application = new JFrame(); 
     PanelFurniture panel = new PanelFurniture(); 
     application.add(panel); 
     application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     application.setSize(600,450); 
     application.setLocationByPlatform(true); 
     application.setVisible(true); 

    } 

} 

/** Here is the GUI of the program. 
* class name PanelFurniture.class 
* @author Kiril Anastasov 
* @date 18/03/2012 
*/ 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.*; 

public class PanelFurniture extends JPanel implements ActionListener 
{ 

    JLabel label; 
    JButton a,b; 
    JPanel centerPanel, eastPanel; 

    public void actionPerformed(ActionEvent ae) 
    { 
     this.setLayout(new BorderLayout()); 

     centerPanel = new JPanel(); 
     //centerPanel.setLayout(); 
     this.add(centerPanel, BorderLayout.CENTER); 

     eastPanel = new JPanel(new GridLayout(1,3)); 
     label = new JLabel(); 
     b = new JButton("Move"); 
     eastPanel.add(b); 
     eastPanel.add(label); 
     a = new JButton("ee"); 
     eastPanel.add(a); 

     //this.add(eastPanel); 
     this.add(eastPanel, BorderLayout.EAST); 
    } 
} 

回答

2

您只在actionPerformed时将组件添加到面板,并且您从不将其注册为侦听器,因此它将永远不会显示。取而代之的是,将它们添加在构造函数/ init方法:

public class PanelFurniture extends JPanel 
{ 

    JLabel label; 
    JButton a,b; 
    JPanel centerPanel, eastPanel; 

    public void init() 
    { 
     this.setLayout(new BorderLayout()); 

     centerPanel = new JPanel(); 
     //centerPanel.setLayout(); 

     this.add(centerPanel, BorderLayout.CENTER); 


     eastPanel = new JPanel(new GridLayout(1,3)); 

     label = new JLabel(); 
     b = new JButton("Move"); 
     eastPanel.add(b); 
     eastPanel.add(label); 
     a = new JButton("ee"); 
     eastPanel.add(a); 

     this.add(eastPanel, BorderLayout.EAST); 
    } 
} 

和创建面板之后调用init()

PanelFurniture panel = new PanelFurniture(); 
panel.init(); 
application.add(panel); 

此外,还要确保用户界面是在美国东部时间构造:

SwingUtils.invokeLater(new Runnable() { 
    @Override 
    public void run() { 
     JFrame application = new JFrame(); 
     PanelFurniture panel = new PanelFurniture(); 
     application.add(panel); 
     application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     application.setSize(600,450); 
     application.setLocationByPlatform(true); 
     application.setVisible(true); 
    } 
}); 
+0

ooo我现在看到,我觉得很愚蠢,因为我看不到我的问题。队友的欢呼声! – Kiril 2012-03-18 08:53:17

+0

我可以问一下,将jlabel/jbuttons放入数组或图像中是否是一种好的做法 – Kiril 2012-03-18 09:04:05

+0

当你有很多这样的东西,并且需要经历所有这些东西时,这是一个很好的练习:)它让事情变得更简单。 – MByD 2012-03-18 09:05:15