2014-11-24 17 views
0

我想调用类StoreManager中名为test的方法。 我是从调用它的类名为来自不同类的多窗口应用程序调用方法

public class StoreManager extends JFrame 
{ 
    JFrame jf = new JFrame(); 

    public void table() 
    { 
    JPanel p = new JPanel(); 
     JButton b; 
     jf.setSize(200, 100); 
     jf.setTitle("test"); 
     jf.setLayout(new FlowLayout()); 
     jf.setLocationRelativeTo(null); 
     jf.setVisible(true); 

     b = new JButton("button"); 
     b.setBounds(20, 20, 125, 25); 

     p.add(b); 
    } 

而现在类桂,我调用类的我有

public class Gui 
{ 
private StoreManager sm = new StoreManager(); //calling instance of the class with test window 
//code to create its own window 

//some button to call this table 
sm.table(); //calling the method form StoreManager 

当我编译从该表的方法商店经理我得到空白窗口。

有没有人有想法?

回答

0

你不需要table()方法可言,打造你的构造函数需要和使用SwingUtilities.invokeLater(new Runnable()使用JFrame像这样:

public class StoreManager extends JFrame 
    { 
    public StoreManager(){ 
    JFrame jf = new JFrame(); 
    JPanel p = new JPanel(); 
     JButton b; 
     jf.setSize(200, 100); 
     jf.setTitle("test"); 
     jf.setLayout(new FlowLayout()); 
     jf.setLocationRelativeTo(null); 
     jf.setVisible(true); 

     b = new JButton("button"); 
     b.setBounds(20, 20, 125, 25); 

     p.add(b); 
     jf.add(p); 
     } 

    } 


public class Gui 
{ 
public static void main(String[]args){ 
    SwingUtilities.invokeLater(new Runnable(){ 
     public void run(){ 
      new StoreManager(); 
     } 
    }); 
} 
} 

而且不要忘记加上面板的JFrame:jf.add(p);你在代码中遗漏了这个

+0

p.add(jf);窗口出现,但空白和错误java.lang.IllegalArgumentException – Alex 2014-11-24 23:16:15

+0

有人可以帮助我呢? – Alex 2014-11-25 10:51:58