我在擺動方面有點問題。我有一個叫做FrameMain
的JFrame
。它內部是一個JPanel
,被稱爲panelChoices
。控制另一個班級的另一個班級
當FrameMain
被調用/創建的,它填充了一個數PanelEntries
對象,這是一種JPanel
與一些在它JButton
S(它是一個不同的類,我寫)的panelChoices
對象。
我想要做的是,當我點擊PanelEntries
對象的內部,我要摧毀/刪除FrameMain
,它的部件(包括包含JButton
的PanelEntries
對象)的其餘部分一起的一個按鈕。
我一直在使用super
嘗試,但它返回的JPanel
(在PanelEntries
對象),它保存JButton
而不是FrameMain
持有它們放在一起。我怎樣才能做到這一點?
編輯:看來我還不夠清楚,所以這裏有更多的信息來自我的工作。我現在沒有實際的代碼,因爲我在不同的機器上,但我希望這將有助於詳細說明我的問題。
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
文件中。 - 我需要知道的如何處理
PanelEntries
對象中的按鈕,而不僅僅配置JFrame的FrameMain
。
您可以提供一段代碼摘錄。我認爲這會比文字牆更容易理解 – fyr 2011-12-16 07:06:15