我有一個JFrame A的子類。我有另一個B類,它是A的子類。我想將新組件添加到框架B(如JButton)。我的代碼如下:如何將組件添加到JFrame子類的子類中
public B() extends A {
//Calling super class constructor
super();
//Creating and adding a button
JButton btn = new JButton();
this.add(btn);
//other codes
}
當我顯示框架時,不添加按鈕,只顯示超類框架及其組件。我怎樣才能將這些按鈕添加到子類B的框架中?
更新:這裏是我的代碼的精簡版。我在超類ListObjects中使用了BorderLayout。
package assignment2;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ListAndModifyCustomer extends ListObjects {
public ListAndModifyCustomer() {
//Calling super class constructor
super("Customers");
//Adding listener to the ok button
super.selectBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//codes to create another JFrame
dispose(); //Closing the frame
}
});
//Adding button to the panel
super.panel.add(new JButton("NO"));
JPanel jp = new JPanel();
jp.add(super.selectBtn);
super.add(jp, BorderLayout.SOUTH);
}
}
我們需要一點點信息。你可以發佈一個[SSCCE](http://www.sscce.org)來說明你在做什麼?特別是,我想知道你使用的是什麼'LayoutManager'? –