Java 单击按钮更改圆的颜色
如何在Java中更改单击按钮时圆圈的颜色? 这是密码Java 单击按钮更改圆的颜色,java,swing,awt,jbutton,actionlistener,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
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
}
包装圈;
导入java.awt.Color;
导入java.awt.Graphics;
/**
*
*@作者阿克马尔
*/
公共类NewJApplet扩展了javax.swing.JApplet{
/**
*初始化小程序NewJApplet
*/
@凌驾
公共void init(){
/*设置Nimbus的外观和感觉*/
//
/*如果Nimbus(在JavaSE6中引入)不可用,请使用默认的外观。
*详情请参阅http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
试一试{
for(javax.swing.UIManager.LookAndFeelInfo:javax.swing.UIManager.getInstalledLookAndFeels()){
if(“Nimbus”.equals(info.getName())){
setLookAndFeel(info.getClassName());
打破
}
}
}捕获(ClassNotFoundException ex){
getLogger(NewJApplet.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}catch(实例化异常){
getLogger(NewJApplet.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}捕获(非法访问例外){
getLogger(NewJApplet.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}catch(javax.swing.UnsupportedLookAndFeelException ex){
getLogger(NewJApplet.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}
//
/*创建并显示小程序*/
试一试{
java.awt.EventQueue.invokeAndWait(new Runnable()){
公开募捐{
初始化组件();
}
});
}捕获(例外情况除外){
例如printStackTrace();
}
}
公共空间涂料(图g)
{
超级油漆(g);
设置前景(颜色:黄色);
g、 圆形(100100100100);
}
/**
*从init()方法中调用此方法来初始化
*表单.警告:请勿修改此代码。此方法的内容为
*始终由表单编辑器重新生成。
*/
@抑制警告(“未选中”)
//
私有组件(){
jButton2=newjavax.swing.JButton();
jButton2.setText(“蓝色”);
jButton2.addActionListener(新java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout=newjavax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(布局);
layout.setHorizontalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButton2,javax.swing.GroupLayout.DEFAULT_SIZE,113,Short.MAX_值)
.addGap(277277277))
);
layout.setVerticalGroup(
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))
);
}//
私有void jButton2ActionPerformed(java.awt.event.ActionEvent evt){
//TODO在此处添加您的处理代码:
}
//变量声明-不修改
私有javax.swing.JButton jButton2;
//变量结束声明
}
请帮忙
Color Color=Color.YELLOW代码>作为类属性
设置前景(颜色为黄色)代码>至设置前景(颜色)代码>
//TODO在此处添加处理代码:
类似于color=color.BLACK;重新油漆()代码>
Color Color=Color.YELLOW代码>作为类属性
设置前景(颜色为黄色)代码>至设置前景(颜色)代码>
//TODO在此处添加处理代码:
类似于color=color.BLACK;重新油漆()代码>
要通过覆盖paintComponent()而不是paint()将Jpanel添加到JApplet,以及通过覆盖paintComponent()而不是paint()将Jpanel添加到JApplet,请告诉我如何在圆圈内添加文本@这是另一个问题。你能告诉我如何在圆圈内添加文字吗@这是另一个问题。
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();
}
}