2013-05-01 3 views
0

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     
    } 

+0

및 재정의 paintComponent (가 JApplet에에 JPanel을 추가하는) 같은로 변경 // TODO add your handling code here:

  • setForeground(color);에() – mKorbel

  • 답변

    3
    1. 클래스의 속성으로 Color color = Color.YELLOW;를 선언 도와주세요 코드입니다. 감사의
    2. 변경 setForeground(Color.yellow); 대신 페인트의 color = Color.BLACK; repaint();
    +0

    번들의 친절 –

    +0

    을했다 당신이 할 수있는 서클 내부에 텍스트를 추가하려면 어떻게해야합니까? @Andrew –

    +0

    그건 별개의 질문입니다. –

    3
    import java.awt.*; 
    import java.awt.event.*; 
    import javax.swing.*; 
    public class NewJApplet extends JApplet { 
        private Color currentColor = Color.YELLOW; 
        @Override 
        public void paint(Graphics g) { 
         super.paint(g); 
         g.setColor(currentColor); 
         g.fillOval(100, 100, 100, 100); 
        } 
        private void jButton2ActionPerformed(ActionEvent evt){ 
         currentColor = Color.BLUE; 
         repaint(); 
        }           
    }