2
  JFrame frame = new JFrame("Picture");
  frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  display = new JPanel();      
if(event.getSource().equals(birthday)){
    background = new JLabel(bday);
    display.add(background);

  }
  else if(event.getSource().equals(cake)){
    picture = new JLabel(pastry, SwingConstants.LEFT);
    display.add(picture);
  }
  else if(event.getSource().equals(input)){
    word = new JLabel(text);
    word.setHorizontalTextPosition(SwingConstants.RIGHT);
    word.setVerticalTextPosition(SwingConstants.CENTER);
    display.add(word);
  }
  frame.setPreferredSize (new Dimension(450, 350));
  frame.getContentPane().add(display);
  frame.pack();
  frame.setVisible(true);

これは、スタンドアロン クラス内の ActionListener クラスの一部です。私はコンボボックス/カードレイアウトを持っていました。そのため、あるコンボ ボックス ラベルのボタン (ケーキ) をクリックし、別のコンボ ボックス ラベルの別のボタン (誕生日) をクリックすると、2 つのフレームが表示されました。それらを同じフレームに配置したいのですが、その方法がわかりませんでした。

4

1 に答える 1

4

アクション実行メソッドが呼び出されるたびにフレームの新しいインスタンスを作成するのではなく、単一の共有インスタンスを作成する必要があります...

ここに画像の説明を入力してください

public class OneFrameToRuleThemAll {

    public static void main(String[] args) {
        new OneFrameToRuleThemAll();
    }

    public OneFrameToRuleThemAll() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });
    }

    public static class TestPane extends JPanel {

        private JFrame frame;

        private static final String FRUIT[] = new String[] {
            "Banana",
            "Apple",
            "Manga",
            "Pear"
        };

        public TestPane() {

            JButton button = new JButton("Fruit");
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {

                    if (frame == null) {
                        frame = new JFrame("Fruits basket");
                        frame.setSize(100, 200);
                        frame.setLayout(new GridLayout(0, 1));
                        frame.setLocationRelativeTo(TestPane.this);
                        frame.setVisible(true);
                    }

                    int index = (int)Math.round(Math.random() * (FRUIT.length - 1));
                    frame.add(new JLabel(FRUIT[index]));
                    frame.getContentPane().validate();

                }
            });

            setLayout(new GridBagLayout());
            add(button);

        }

    }

}
于 2013-01-13T03:47:24.960 に答える