Swingに関して少し問題があります。とJFrame呼ばれるものがありFrameMainます。その中には とJPanel呼ばれるものがありpanelChoicesます。
がFrameMain呼び出される/作成されると、panelChoicesオブジェクトが多数のオブジェクトで埋められPanelEntriesます。これは、その中JPanelに多数のJButtons が含まれています (これは、私が作成した別のクラスです)。
私がしたいのは、PanelEntriesオブジェクト内のボタンの1つをクリックしたときに、 を破棄/削除したい 、FrameMainおよび残りのコンポーネント ( を含むPanelEntriesオブジェクトを含むJButton) です。
私は使用してみましたが、それらをすべて一緒に保持するのではなく、保持する(オブジェクト) をsuper返します。どうすればこれを達成できますか?JPanelPanelEntriesJButtonFrameMain
編集:私は十分に明確ではないようですので、ここに私の仕事からのもう少しの情報があります. 私は別のマシンを使用しているため、現在実際のコードはありませんが、これが私の質問を詳しく説明するのに役立つことを願っています.
public class FrameMain() {
private JFrame frameMain;
private JPanel panelChoices;
public FrameMain(args) {
createGUI();
loadData();
}
private void createGUI() {
JFrame frameMain = new JFrame();
JPanel panelChoices = new JPanel(new GridLayout(1,1));
frameMain.add(panel);
// removed formatting and other design codes since they are not important.
pack();
}
private void loadData() {
boolean available;
for (int i = 1; i <= 10; i++) {
// do some if/else and give value to boolean available
PanelEntries panel = new PanelEntries(i, available);
frameMain.add(panel);
// more code here to handle data.
}
}
}
public class PanelEntries() extends JPanel {
public PanelEntries(int num, boolean avb) {
JButton button = new JButton("Button Number " + num);
button.setEnabled(avb);
add(button);
// add action listener to created button so that it calls 'nextScreen()' when clicked.
// more code
pack();
}
private void nextScreen() {
// destroy/dispose MainFrame here.
// See Notes.
AnotherFrame anotherFrame = new AnotherFrame();
}
}
注:
- すべてのクラスは独自の
.javaファイル内にあります。 - JFrame を破棄するだけでなく、オブジェクト内のボタンから破棄する方法
FrameMainPanelEntriesを知る必要があります。