1

我有一些代码:

button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            JFrame mainFrame = new JFrame();
            JPanel windowPanel = new JPanel(new FlowLayout());
//          windowPanel.setPreferredSize(new Dimension(200,200));
            windowPanel.add(colorChooser);
            windowPanel.add(button);
            windowPanel.setVisible(true);
            mainFrame.add(windowPanel);
        }
    });

问题是单击按钮后如何显示新FlowLayout的(在新窗口中)?

4

2 回答 2

2

首先交换windowPanel.setVisible(true);mainFrame.add(windowPanel);

mainFrame.add(windowPanel);
windowPanel.setVisible(true);

mainFrame.pack()在通话之前添加setVisible也不会受到伤害。

您可能希望看看The Use of Multiple JFrames: Good or Bad Practice?不过,在致力于特定设计之前。

于 2013-10-09T08:25:17.150 回答
0
        public void actionPerformed(ActionEvent e) {
            frame2 = new JFrame("Meine Frame");
            frame2.setSize(500,400);
            frame2.setLocationRelativeTo(null);
            JPanel windowPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
            windowPanel.add(okButton);

            okButton.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    color = colorChooser.getColor();
                    System.out.println("The selected color was:" + color);
                    panel.setBackground(color);
                    frame2.dispose();

                }
            });{

            };
            windowPanel.add(colorChooser);

            windowPanel.setVisible(true);
            frame2.add(windowPanel);
            frame2.setVisible(true);

        }

这样我就解决了我的问题。

于 2013-10-10T08:32:49.583 回答