JFrameAのサブクラスがあります。Aのサブクラスである別のクラスBがあります。JButtonのようにフレームBに新しいコンポーネントを追加したいと思います。私のコードは次のとおりです。
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);
}
}