我在做一个在框架内放入三个按钮,每当单击一个按钮时显示相应的颜色
我查阅了HTML帮助文档,但由于是初学者,关于actionPerformed的东西又
很多,所以还是不明白。
public void actionPerformed(ActionEvent e){ }
请教一下,我应在actionPerformed(应该是用这个方法吧)
方法中添加什么代码,才能实现预定功能呢???
谢谢

解决方案 »

  1.   

    /*
     * NewJFrame.java
     *
     * Created on 2006年4月7日, 下午6:45
     */package testwnb;
    import java.awt.*;
    /**
     *
     * @author  Administrator
     */
    public class NewJFrame extends javax.swing.JFrame {
        
        /** Creates new form NewJFrame */
        public NewJFrame() {
            initComponents();
        }
        
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        // <editor-fold defaultstate="collapsed" desc=" 生成的代码 ">
        private void initComponents() {
            jPanel1 = new javax.swing.JPanel();
            jButton1 = new javax.swing.JButton();
            jButton2 = new javax.swing.JButton();
            jButton3 = new javax.swing.JButton();
            jLabel1 = new javax.swing.JLabel();        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            jButton1.setText("\u7ea2");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });        jPanel1.add(jButton1);        jButton2.setText("\u7eff");
            jButton2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton2ActionPerformed(evt);
                }
            });        jPanel1.add(jButton2);        jButton3.setText("\u84dd");
            jButton3.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton3ActionPerformed(evt);
                }
            });        jPanel1.add(jButton3);        jLabel1.setFont(new java.awt.Font("宋体", 0, 24));
            jLabel1.setText("\u663e\u793a\u989c\u8272");
            jLabel1.setOpaque(true);
            jPanel1.add(jLabel1);        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);        pack();
        }
        // </editor-fold>    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO 将在此处添加您的处理代码:
           jLabel1.setBackground(Color.BLUE);
        }    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO 将在此处添加您的处理代码:
           jLabel1.setBackground(Color.GREEN);
            
        }    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO 将在此处添加您的处理代码:
           jLabel1.setBackground(Color.RED);
        }
        
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new NewJFrame().setVisible(true);
                }
            });
        }
        
        // 变量声明 - 不进行修改
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JButton jButton3;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JPanel jPanel1;
        // 变量声明结束
        
    }