0

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();
    }
}

:

  1. すべてのクラスは独自の.javaファイル内にあります。
  2. JFrame を破棄するだけでなく、オブジェクト内のボタンから破棄する方法FrameMainPanelEntriesを知る必要があります。
4

1 に答える 1

2

与えられた情報によると、

  1. アプリケーションを終了したい場合は、大したことではありませんSystem.exit(0);:)

  2. フレームを処分する場合は、jframe.dispose();

  3. コンポーネントを削除したい場合/使用できるすべてのコンポーネント.remove(Component)/.removeAll()など

これで問題が解決しない場合は、より詳しい情報を記載して質問を書き直してください。

于 2011-12-16T07:15:06.073 に答える