Java 以3种不同的方式更改面板背景色

Java 以3种不同的方式更改面板背景色,java,swing,Java,Swing,我想显示程序底部的3个按钮,如果我们点击红色按钮,面板的颜色会变为红色,等等 import javax.swing.*; import java.awt.*; import java.awt.event.*; public class LatihanEvent2 implements ActionListener { private JButton buttonRed = new JButton ("Red"); private JButton buttonGreen = new JButt

我想显示程序底部的3个按钮,如果我们点击红色按钮,面板的颜色会变为红色,等等

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class LatihanEvent2 implements ActionListener {

private JButton buttonRed = new JButton ("Red");
private JButton buttonGreen = new JButton ("Green");
private JButton buttonBlue = new JButton ("Blue");

public LatihanEvent2() {
    JFrame frame = new JFrame("Contoh Event");
    frame.setSize(400,400);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel panel = new JPanel();

panel.add(buttonRed, BorderLayout.WEST);
panel.add(buttonGreen, BorderLayout.CENTER);
panel.add(buttonBlue, BorderLayout.EAST);

//Inner Class
ListenerRed clickListener = new ListenerRed();
buttonRed.addActionListener(clickListener);

//Anonymous Class
buttonGreen.addActionListener(new ActionListener () {
    public void actionPerformed (ActionEvent e) {
        buttonGreen.setBackground(Color.GREEN);
    }
});

//Derived Class
buttonBlue.addActionListener(this); //Step 2

frame.add(panel, BorderLayout.SOUTH);
frame.setVisible(true);
frame.show();
}

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

//Inner Class
class ListenerRed implements ActionListener {
    public void actionPerformed (ActionEvent e) {
        buttonRed.setBackground(Color.RED);
    }
}

//Derived Class
public void actionPerformed (ActionEvent e) {
    buttonBlue.setBackground(Color.BLUE);
}
}

在我的编码中有3种方法。内部类、匿名类和派生类。如何使用这种不同的方法使面板改变颜色背景?“请帮助我”

此示例添加了第二个面板(
ui
),每个操作都会在其中设置背景。有关更多提示,请参见代码中的注释

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class LatihanEvent2 implements ActionListener {

    private JButton buttonRed = new JButton("Red");
    private JButton buttonGreen = new JButton("Green");
    private JButton buttonBlue = new JButton("Blue");
    JPanel panel = new JPanel();

    public LatihanEvent2() {
        JFrame frame = new JFrame("Contoh Event");

        panel.add(buttonRed, BorderLayout.WEST);
        panel.add(buttonGreen, BorderLayout.CENTER);
        panel.add(buttonBlue, BorderLayout.EAST);

        frame.add(panel, BorderLayout.SOUTH);
        frame.setSize(400, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        buttonRed.addActionListener(this);
        buttonGreen.addActionListener(this);
        buttonBlue.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e) {
        String btnName = e.getActionCommand();
        if(btnName.equalsIgnoreCase("red")) {
            panel.setBackground(Color.red);
        }
        else if(btnName.equalsIgnoreCase("green")) {
            panel.setBackground(Color.green);
        }
        else {
            panel.setBackground(Color.blue);
        }
    }

    public static void main(String[] args) {
        new LatihanEvent2();
    }
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;

public class LatihanEvent2 implements ActionListener {

    private JButton buttonRed = new JButton("Red");
    private JButton buttonGreen = new JButton("Green");
    private JButton buttonBlue = new JButton("Blue");
    JPanel ui = new JPanel(new BorderLayout(5, 5));

    public LatihanEvent2() {
        JFrame frame = new JFrame("Contoh Event");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // quick and dirty way to provide space in a GUI
        ui.add(new JLabel(new ImageIcon(
                new BufferedImage(300, 300, BufferedImage.TYPE_INT_ARGB))));

        JPanel panel = new JPanel();
        // border layout constraints are irrelevant to a flow layout!
        //ui.add(buttonRed, BorderLayout.WEST);
        panel.add(buttonRed);
        panel.add(buttonGreen);
        panel.add(buttonBlue);

        //Inner Class
        ListenerRed clickListener = new ListenerRed();
        buttonRed.addActionListener(clickListener);

        //Anonymous Class
        buttonGreen.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ui.setBackground(Color.GREEN);
            }
        });

        //Derived Class
        buttonBlue.addActionListener(this); //Step 2

        ui.add(panel, BorderLayout.PAGE_END);

        frame.setContentPane(ui);

        frame.pack();
        //frame.show();  // Deprecated!
        frame.setVisible(true); // should be last..
    }

    public static void main(String[] args) {
        // The GUI should be created and updated on the EDT.  E.G.
        Runnable r = new Runnable() {

            @Override
            public void run() {
                new LatihanEvent2();
            }
        };
        // Here is the iomportant part of actually 
        // starting that runnable on the EDT..
        SwingUtilities.invokeLater(r);
    }

    //Inner Class
    class ListenerRed implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            ui.setBackground(Color.RED);
        }
    }

    //Derived Class
    public void actionPerformed(ActionEvent e) {
        ui.setBackground(Color.BLUE);
    }
}

更简单地说,我们可以这样做: (在看到您的要求后)


问题陈述意味着要更改面板的颜色,但侦听器中的操作正在设置它们所连接的按钮的BG颜色。这是故意的吗?还要注意的是,如果需要更改面板的颜色,则意味着需要让用户看到部分面板。最简单的方法是添加一个
EmptyBorder
@androw如何添加
EmptyBorder
请?\<代码>面板.设置顺序(新的清空顺序(…)刚刚注意到,您没有回答我的问题,这是为了让我(可能还有其他人)更好地理解需求,以便我们能够提供最好的帮助。请尽你所能回答任何问题。如果您不理解问题,请要求澄清。@AndrewThompson抱歉。哪一个我失败了?我已经尝试添加一个类似于您的
EmptyBorder
,但什么也没有发生:(是的,这显示了对面板的更改,正如我所期望的,这是这里的要求。抱歉,但我的意思是使用3种方法,您的方法只是使用1种方法。呵呵。@enjeru修改您的代码以使用此代码中的思想很简单。我已经使用了您的代码并进行了更改(使用3种不同的方法)并看到了您在此代码中可能看到的结果,因为它更改了面板的颜色,而不是按钮。请发布您的最新代码,显示(尝试)使用
panel.setBackground(…)
。我已经明白了Bhaskar的意思,但我的老师想要这样,对不起,我只是用photoshop-->@andreThompsonah编辑它!这就是我的意思。你的编码使用了新材料(BuffereImage),所以我仍然很困惑,但我会学习它的^^^非常感谢。很抱歉让你困惑了“你的编码使用了新材料(BuffereImage)”注释掉添加的行,看看会发生什么。;)这种编码像bhaskar,对吗?只需使用1方法hehe><我的老师弄糊涂了,我们必须使用3方法。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class LatihanEvent2 implements ActionListener {

    private JButton buttonRed = new JButton("Red");
    private JButton buttonGreen = new JButton("Green");
    private JButton buttonBlue = new JButton("Blue");
    JFrame frame = new JFrame("Contoh Event");
    JPanel panelDown = new JPanel();
    JPanel panelUp = new JPanel();

    public LatihanEvent2() {

        panelDown.add(buttonRed, BorderLayout.WEST);
        panelDown.add(buttonGreen, BorderLayout.CENTER);
        panelDown.add(buttonBlue, BorderLayout.EAST);

        frame.add(panelDown, BorderLayout.SOUTH);
        frame.add(panelUp, BorderLayout.CENTER);
        frame.setSize(400, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        buttonRed.addActionListener(this);
        buttonGreen.addActionListener(this);
        buttonBlue.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e) {
        String btnName = e.getActionCommand();
        if(btnName.equalsIgnoreCase("red")) {
            panelUp.setBackground(Color.red);
        }
        else if(btnName.equalsIgnoreCase("green")) {
            panelUp.setBackground(Color.green);
        }
        else {
            panelUp.setBackground(Color.blue);
        }
    }

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