简体   繁体   中英

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! 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.

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)

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. 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

Add this next to your other ActionListener

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

You are missing an ActionListener for the blue JButtton.

Similar to how you added the ActionListener to your ColorButton, your ColorrButton needs to have one registered. 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.

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.

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:

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. This will add a little bit of length to your code, but as(if?) your program grows, it will reduce complexity greatly

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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