改变面板背景颜色3种不同的方式

时间:2015-04-12 04:31:09

标签: 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 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种方法。内部类,匿名类和派生类。如何使用这种不同的方法使面板改变颜色背景?请帮帮我

3 个答案:

答案 0 :(得分:1)

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();
    }
}

答案 1 :(得分:1)

此示例添加第二个面板(ui),每个操作设置背景。请参阅代码中的注释以获取更多提示。

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);
    }
}

答案 2 :(得分:0)

以更简单的方式,我们可以这样做: (看到你的要求后)

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();
    }
}