2014-06-23 59 views

回答

0

這只是WindowBuilder生成的默認範圍。如果您要訪問的,例如,您的類中的另一種方法,你可以隨時從代碼的組件:

public Test_class() { 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setBounds(100, 100, 450, 300); 
    contentPane = new JPanel(); 
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
    contentPane.setLayout(new BorderLayout(0, 0)); 
    setContentPane(contentPane); 

    JPanel panel = new JPanel(); //Object created within the constructor 
    contentPane.add(panel, BorderLayout.CENTER); 
} 

要更多的東西一樣:

private JPanel panel; 
public Test() { 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setBounds(100, 100, 450, 300); 
    contentPane = new JPanel(); 
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
    contentPane.setLayout(new BorderLayout(0, 0)); 
    setContentPane(contentPane); 

    panel = new JPanel(); 
    contentPane.add(panel, BorderLayout.CENTER); 
} 

完成該操作後,您將能夠從代碼中稍後調用的任何方法調用方法,如panel.add(Component)。您也可以將修改器更改爲public,然後通過使用Test_class().panel.method()

相關問題