package color2;import java.awt.BorderLayout;import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Color;
public class ColorFrame extends JFrame {
    BorderLayout borderLayout1 = new BorderLayout();
    JPanel jPanel1 = new JPanel();
    JButton jButton1 = new JButton();
    JButton jButton2 = new JButton();
    JButton jButton3 = new JButton();    public ColorFrame() {
        try {            jbInit();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }    private void jbInit() throws Exception {
        getContentPane().setLayout(borderLayout1);
        jButton2.addActionListener(new ColorFrame_jButton1_actionAdapter(this));
        jButton3.addActionListener(new ColorFrame_jButton1_actionAdapter(this));
        this.getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
        jButton2.setBounds(new Rectangle(62, 130, 83, 25));
        jButton2.setText("绿");
        jButton3.setBounds(new Rectangle(99, 215, 83, 25));
        jButton3.setText("蓝");
        jButton1.addActionListener(new ColorFrame_jButton1_actionAdapter(this));
        jPanel1.add(jButton1);
        jPanel1.add(jButton2);
        jPanel1.add(jButton3);
        jButton1.setBounds(new Rectangle(142, 76, 83, 25));
        jButton1.setText("红");
        jPanel1.setLayout(null);
        this.setSize(500, 400);
        this.setDefaultCloseOperation(this.DISPOSE_ON_CLOSE);
        this.setVisible(true);
    }    public void jButton1_actionPerformed(ActionEvent e) {
        ColorFrame frm = new ColorFrame();
        String command = e.getActionCommand();
        if (command.equals("红")) {
            frm.getContentPane().setBackground(new Color(255, 0, 0));
        } else if (command.equals("绿")) {
            frm.getContentPane().setBackground(new Color(0, 255, 0));
        } else {
            frm.getContentPane().setBackground(new Color(0, 0, 255));
        }    }
    class ColorFrame_jButton1_actionAdapter implements ActionListener {
        private ColorFrame adaptee;
        ColorFrame_jButton1_actionAdapter(ColorFrame adaptee) {
            this.adaptee = adaptee;
        }
        public void actionPerformed(ActionEvent e) {
            adaptee.jButton1_actionPerformed(e);
        }    }}
不知道怎么回事,这个监听根本不起作用~~~
我是一开始做了三个
可是想合成一个用,然后在那里面判断一下就可以了,谁知道怎么都做不好,请大家帮帮我吧,太感谢了~

解决方案 »

  1.   

    package test;import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Rectangle;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;public class ColorFrame extends JFrame
    {
        BorderLayout borderLayout1 = new BorderLayout();
        JPanel       jPanel1       = new JPanel();
        JButton      jButton1      = new JButton();
        JButton      jButton2      = new JButton();
        JButton      jButton3      = new JButton();    public ColorFrame()
        {
            try
            {            jbInit();
            }
            catch (Exception exception)
            {
                exception.printStackTrace();
            }
        }    private void jbInit() throws Exception
        {
            getContentPane().setLayout(borderLayout1);
            jButton2.addActionListener(new ColorFrame_jButton1_actionAdapter(this));
            jButton3.addActionListener(new ColorFrame_jButton1_actionAdapter(this));
            this.getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
            jButton2.setBounds(new Rectangle(62, 130, 83, 25));
            jButton2.setText("绿");
            jButton3.setBounds(new Rectangle(99, 215, 83, 25));
            jButton3.setText("蓝");
            jButton1.addActionListener(new ColorFrame_jButton1_actionAdapter(this));
            jPanel1.add(jButton1);
            jPanel1.add(jButton2);
            jPanel1.add(jButton3);
            jButton1.setBounds(new Rectangle(142, 76, 83, 25));
            jButton1.setText("红");
            jPanel1.setLayout(null);
            this.setSize(500, 400);
            this.setDefaultCloseOperation(this.DISPOSE_ON_CLOSE);
            this.setVisible(true);
        }    public void jButton1_actionPerformed(ActionEvent e)
        {
    //        ColorFrame frm = new ColorFrame();
            String command = e.getActionCommand();
            if (command.equals("红"))
            {
                jPanel1.setBackground(new Color(255, 0, 0));
            }
            else if (command.equals("绿"))
            {
                jPanel1.setBackground(new Color(0, 255, 0));
            }
            else
            {
                jPanel1.setBackground(new Color(0, 0, 255));
            }    }    class ColorFrame_jButton1_actionAdapter implements ActionListener
        {
            private ColorFrame adaptee;        ColorFrame_jButton1_actionAdapter(ColorFrame adaptee)
            {
                this.adaptee = adaptee;
            }        public void actionPerformed(ActionEvent e)
            {
                adaptee.jButton1_actionPerformed(e);
            }    }
        
        public static void main(String[] args)
        {
            new ColorFrame();
        }}
      

  2.   

    jPanel1 盖在 frm.getContentPane()  上面
    你设置frm.getContentPane()  
    当然看不到颜色变化