我想调用StoreManager中名为 test 的方法。 我称之为的课程名为 Gui
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);
}
现在在班级 Gui ,我称之为
的班级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 :(得分: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);
您在代码中缺少此内容