1

我想我的一些事情actionListeners是如何搞砸的。我应该有两个按钮块。

第一个面板是 16 个按钮 x 16 个按钮(总共 256 个按钮)。第二个面板是 8 个按钮 x 2 个按钮(总共 16 个按钮)我可以将“蓝色”从“ColorChooser”传递到两个面板。

但是,第二个面板应该变成红色。不是蓝色的。我的第二个问题 - Panel 2 看起来是正确的。它从 1 开始到 16。但是,面板 1 从 17 开始到 256。请感谢您的帮助。

我还想知道为什么我的按钮阵列工具提示不起作用?

再次感谢!

我的意思的例子

            final JFrame MaineFrame = new JFrame("Kola Color Crapper");
            MaineFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            MaineFrame.setPreferredSize(new Dimension(400, 400));

            final JFrame PatternGeneratorFrame = new JFrame("Pattern Generator");
            PatternGeneratorFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            ActionListener PatternMakerActionListener = new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {

                    PatternGeneratorFrame.setVisible(true);

                }
            };
            JButton PatternMakerBtn = new JButton("Make Pattern");
            PatternMakerBtn.addActionListener(PatternMakerActionListener);
            PatternMakerBtn.setToolTipText("This will allow you to create your own patterns 1 frame at a time.");



            JPanel contentPane = new JPanel();



            ActionListener myActionListener = new ActionListener() {
                public void actionPerformed(ActionEvent aef) {
                    if (aef.getSource() instanceof JButton) {
                        ((JButton) aef.getSource()).setBackground(ColorChooser.PassColor);

                    }
                }
            };



            ActionListener ColorChooserActionListener = new ActionListener() {
                public void actionPerformed(ActionEvent aee) {
                    if (aee.getSource() instanceof JButton) {
                        ((JButton) aee.getSource()).setBackground(Color.red);

                    }
                }
            };
        JButton button[] = new JButton[256];
            JPanel GridPanel = new JPanel();
            GridPanel.setBorder(BorderFactory.createLineBorder(Color.blue, 2));
            GridPanel.setLayout(new GridLayout(16, 16, 10, 10));
            for (int i = 0; i < button.length; i++) {
                button[i] = new JButton(Integer.toString(i + 1));
                button[i].addActionListener(myActionListener);
                GridPanel.add(button[i]);
                GridPanel.setToolTipText("lets make noise.");
            }
            contentPane.add(GridPanel);


            JButton ColorPickerBtn[] = new JButton[16];
            JPanel ActionPanel = new JPanel();
            ActionPanel.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 6));
            ActionPanel.setLayout(new GridLayout(2, 8, 0, 10));
            for (int l = 0; l < 16; l++) {
                ColorPickerBtn[l] = new JButton(Integer.toString(l+ 1));
                ColorPickerBtn[l].addActionListener(ColorChooserActionListener);
                ColorPickerBtn[l].setToolTipText("Choose the color you would like to use. * Colors 1-8 are static. You may change colors 9-16.");
                ActionPanel.add(button[l]);
            }
            contentPane.add(ActionPanel);

            MaineFrame.getContentPane().add(PatternMakerBtn);

            PatternGeneratorFrame.setContentPane(contentPane);
            PatternGeneratorFrame.pack();
            PatternGeneratorFrame.setVisible(false);
            MaineFrame.pack();
            MaineFrame.setVisible(true);
            ;
        }
    });

}
}
}
4

1 回答 1

0

我解决这个问题的方法是只使用一个 ActionListener 来监听整个帧。

于 2013-04-29T18:25:48.617 回答