简体   繁体   English

两个按钮将面板颜色更改为红色或蓝色

[英]Two buttons changing panel color to red or blue

my first post here. 我的第一篇文章在这里。 I'm currently in school and usually spend my time here on Stackoverflow looking for answers to homework, this time i'd thought that perhaps i'll put my code here and maybe i'll get help more precis and quicker! 我目前在学校,通常在这里花时间在Stackoverflow上寻找作业的答案,这一次我本以为我可能会将代码放在这里,也许我会更快,更准确地获得帮助! Anyways, my problem is that i've written a code which you can see below, and I'm new to swing, studied it for a few hours only. 无论如何,我的问题是我写了一个代码,您可以在下面看到,我是新手,只研究了几个小时。 My problem is that I'm not quite sure how to proceed with my problem, I have 2 buttons, what i want is when you click on first button the panel will change to Red, second button the panel changes to blue, so far only Red works and I don't know how to implement it so that blue works aswell. 我的问题是我不太确定如何解决此问题,我有2个按钮,我要的是单击第一个按钮时面板将变为红色,第二个按钮时面板将变为蓝色,仅红色有效,我不知道如何实现它,所以蓝色也有效。

Would greatly appreciate your help! 非常感谢您的帮助! (Don't be shy about pointing out a few errors or help along the way that doesn't have with the buttons to do, as I said, I'm new :P) (不要害羞地指出一些错误或没有按钮需要的帮助,就像我说的,我是新来的:P)

public class FirstProgram extends JFrame {

    public FirstProgram() {

        initUI();
    }

    private void initUI() {

        JPanel panel = new JPanel();
        panel.setBackground(Color.yellow);
        getContentPane().add(panel);
        panel.setLayout(null);

        JButton Colorbutton = new JButton("Red");
        Colorbutton.setBounds(50, 60, 80, 30);
        Colorbutton.setToolTipText("Panel changes to red");
        Colorbutton.setBackground(Color.green);

        JButton Colorrbutton = new JButton("Blue");
        Colorrbutton.setBounds(1, 30, 90, 30);
        Colorrbutton.setToolTipText("Panel changes to blue");
        Colorrbutton.setBackground(Color.orange);

        Colorbutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                panel.setBackground(Color.red);

            }
        });

        panel.add(Colorbutton);
        panel.add(Colorrbutton);
        setTitle("Time to change colors");
        setSize(300, 200);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                FirstProgram ex = new FirstProgram();
                ex.setVisible(true);
            }
        });
    }
}

you need another ActionListener. 您需要另一个ActionListener。 right now you just have one and it has just one behavior. 现在,您只有一种,而且只有一种行为。 create another one and tie to the 'Blue" button 创建另一个并绑定到“蓝色”按钮

JButton RedColorbutton = new JButton("Red");
 RedColorbutton .setBounds(50, 60, 80, 30);
 RedColorbutton.setToolTipText("Panel changes to red");
 RedColorbutton.setBackground(Color.green);

 JButton BlueColorbutton = new JButton("Blue");
 BlueColorrbutton.setBounds(1,30,90,30);
 BlueColorrbutton.setToolTipText("Panel changes to blue");
 BlueColorrbutton.setBackground(Color.orange);

 RedColorbutton.addActionListener(new ActionListener() {
 @Override
 public void actionPerformed(ActionEvent event) {
 panel.setBackground(Color.red);
 }
 });

BlueColorbutton.addActionListener(new ActionListener() {
 @Override
 public void actionPerformed(ActionEvent event) {
 panel.setBackground(Color.blue);
 }
 });

You've set an action listener for your Colorbutton , but not for Colorrbutton 您已经为Colorbutton设置了一个动作监听Colorbutton ,但没有为Colorrbutton设置一个动作监听Colorrbutton

Add this next to your other ActionListener 将此添加到其他ActionListener旁边

Colorrbutton.addActionListener(new ActionListener()
{
    public void actionPerformed(ActionEvent event)
    {
        panel.setBackground(Color.blue);
    }
});

You are missing an ActionListener for the blue JButtton. 您缺少蓝色JButtton的ActionListener。

Similar to how you added the ActionListener to your ColorButton, your ColorrButton needs to have one registered. 类似于将ActionListener添加到ColorButton的方法,ColorrButton需要注册一个。 By the way, you may wish to change the ColorButton to redButton and the ColorrButton to blueButton or something like that to make things stick out better. 顺便说一句,您可能希望将ColorButton更改为redButton,将ColorrButton更改为blueButton或类似的东西,以使效果更好。

example: 例:

    Colorrbutton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {
            panel.setBackground(Color.blue);
        }
    });

For the sake of reducing duplicate code, you can simplify the logic even further by having your class implement ActionListener. 为了减少重复的代码,可以通过让类实现ActionListener来进一步简化逻辑。

public class FirstProgram extends JFrame implements ActionListener {

Then, when you are instantiating your buttons, add a listener like so: 然后,在实例化按钮时,添加一个监听器,如下所示:

redButton.addActionListener(this);
blueButton.addActionListener(this);

And then in your implementation of actionPerformed you could do something like: 然后在actionPerformed的实现中,您可以执行以下操作:

public void actionPerformed(ActionEvent e) {
    switch (e.getSource()) {
        case redButton:
            panel.setBackground(Color.red);
            break;
        case blueButton:
            panel.setBackground(Color.blue);
            break;
    }
}

Anytime the red or blue buttons performs an action, the actionPerformed will be triggered, and then the logic to determine which button was the source will take over from there. 每当红色或蓝色按钮执行操作时,都会触发actionPerformed,然后逻辑确定源将从那里接管哪个按钮。 This will add a little bit of length to your code, but as(if?) your program grows, it will reduce complexity greatly 这将增加代码的长度,但是随着程序的增长,它将大大降低复杂性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM