getContentPane
方法在JFrame class
中有什麼用法?我GOOGLE了它,但我無法找到適當的答案。JFrame類中的getcontentpane方法
class MainFrame extends JFrame {
public MainFrame(String title) {
super(title);
// Set layout manager
setLayout(new BorderLayout());
// Create Swing component
JTextArea textArea = new JTextArea();
JButton button = new JButton("Click me!");
// Add Swing components to content pane
Container c = getContentPane();
c.add(textArea, BorderLayout.CENTER);
c.add(button, BorderLayout.SOUTH);
JButton button1 = new JButton("Click me again!");
add(button1,BorderLayout.NORTH);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
textArea.append("Hello\n");
}
});
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
textArea.append("Hello\n");
}
});
}
}
那麼爲什麼我們有這樣的方法?它有什麼用處。請給我適當的例子。 –