java - 单击按钮时更改圆圈的颜色

标签 java swing awt jbutton actionlistener

如何改变Java中点击按钮上圆圈的颜色? 这是代码

package circle;

import java.awt.Color;
import java.awt.Graphics;

/**
 *
 * @author Akmal
 */
public class NewJApplet extends javax.swing.JApplet {

    /**
     * Initializes the applet NewJApplet
     */
    @Override
    public void init() {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(NewJApplet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
              java.util.logging.Logger.getLogger(NewJApplet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(NewJApplet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NewJApplet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the applet */
        try {
            java.awt.EventQueue.invokeAndWait(new Runnable() {
                public void run() {
                    initComponents();
                }
            });
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void paint(Graphics g)
    {

     super.paint( g );

     setForeground(Color.yellow);

     g.fillOval(100, 100, 100, 100);

    }

    /**
     * This method is called from within the init() method to initialize the
     * form. WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jButton2 = new javax.swing.JButton();

        jButton2.setText("Blue");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 113,   Short.MAX_VALUE)
                .addGap(277, 277, 277))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,    layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE,       javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(137, 137, 137))
        );
    }// </editor-fold>                        

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
      // TODO add your handling code here:

    }                                        

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton2;
    // End of variables declaration                   
    }

请帮忙

最佳答案

  1. Color color = Color.YELLOW; 声明为类属性。
  2. setForeground(Color.yellow); 更改为 setForeground(color);
  3. //TODO 在此处添加您的处理代码: 更改为 color = Color.BLACK;重绘();

关于java - 单击按钮时更改圆圈的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16319834/

相关文章:

java - 需要可靠/持久的出站套接字,选项?

java - 从 Activity 中设置偏好

Java DAO模式多表原子事务

java - BufferedImage 未在 JPanel 内呈现

java - 如何返回到 Java 中的 JFrames

java - 怎么画一个长方形,中间挖出一个圆圈?

java - 如何解决不兼容类型错误?

java - 单击按钮时在 JLabel 中显示图像时遇到问题 - Java

java - 我无法让碰撞在 Java 中运行

java - 如果数据从 cellrenderer 更改,如何在列调整后计算 jTable 宽度和高度