import javaawt;
import javaawtevent;
import javaapplet;
public class ColorChange extends Applet implements MouseListener
{Button btn=new Button("变色");
public void init()
{btnaddMouseListener(this);
thisadd(btn);}//将鼠标事件注册
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
setBackground(Colorwhite);}
public void mousePressed(MouseEvent e)
{}
public void mouseReleased(MouseEvent e)
{}
public void mouseClicked(MouseEvent e){
setBackground(Colorgreen);
repaint();
}
}
声明是指对象,定义才是指类比较好理解。
java里如何声明一个颜色类对象。
java里如何定义一个颜色类。
如果是第一种的话: javaawtColor color;
如果是 第二种就是自定义类,需要看具体需求来,或者可以参考javaawtColor的源码。
import javaawt;
import javaawtevent;
public class adjustcolor implements AdjustmentListener, WindowListener {
Frame f=new Frame("调整颜色");
Label l1=new Label("调整滚动条,会改变初始颜色",LabelCENTER);
Label l2=new Label("此处显示颜色值",LabelCENTER);
Label l3=new Label("红",LabelCENTER);
Label l4=new Label("绿",LabelCENTER);
Label l5=new Label("蓝",LabelCENTER);
Scrollbar scr1=new Scrollbar(ScrollbarHORIZONTAL,0,10,0,265);
Scrollbar scr2=new Scrollbar(ScrollbarHORIZONTAL,0,10,0,265);
Scrollbar scr3=new Scrollbar(ScrollbarHORIZONTAL,0,10,0,265);
public adjustcolor(){
fadd(l1);
fadd(l2);
fadd(l3);
fadd(l4);
fadd(l5);
fadd(scr1);
fadd(scr2);
fadd(scr3);
fsetSize(400,350);
fsetVisible(true);
faddWindowListener(this);
fsetResizable(false);
l1setBackground(ColorGREEN);
scr1setBounds(35,225,360,25);
scr2setBounds(35,255,360,25);
scr3setBounds(35,285,360,25);
l1setBounds(0,0,400,200);
l2setBounds(0,310,400,30);
l3setBounds(0,225,30,30);
l4setBounds(0,255,30,30);
l5setBounds(0,285,30,30);
scr1addAdjustmentListener(this);
scr2addAdjustmentListener(this);
scr3addAdjustmentListener(this);
l1setBackground(ColorGREEN);
scr1setBackground(ColorRED);
scr2setBackground(ColorGREEN);
scr3setBackground(Colorblue);
}
public void adjustmentValueChanged(AdjustmentEvent e){
int a=scr1getValue();
int b=scr2getValue();
int c=scr3getValue();
l1setBackground(new Color(a,b,c)) ;
l2setText("红"+" "+"绿"+" "+"蓝"+" "+a+" "+b+" "+c);
l1setText(null);
}
public static void main(String[] args){
new adjustcolor();
}
public void windowActivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowClosed(WindowEvent arg0) {
}
public void windowClosing(WindowEvent arg0) {
Systemexit(0);
}
public void windowDeactivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowDeiconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowIconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowOpened(WindowEvent arg0) {
// TODO Auto-generated method stub
}
}
这是源代码 应该是你想要的
主类不需要更改,只需改一下Circlejava我呢见
import javaawtColor;
import javaawtGraphics;
import javaawtGridLayout;
import javaxswingJPanel;
public class Circle extends JPanel
{
public void paintComponent( Graphics g )
{
superpaintComponent( g );
int numFlag=0;
for ( int topLeft = 0; topLeft < 80; topLeft += 10 )
{
numFlag++;
if(numFlag%2==0)
gsetColor(Colorblue);//改变颜色
else
gsetColor(Colorwhite);//改变颜色
int radius = 160 - ( topLeft 2 );
gfillOval(topLeft + 10, topLeft + 25, radius, radius);//这是画圆
//gfillArc( topLeft + 10, topLeft + 25, radius, radius, 0, 360 );
}
thissetBackground(Colorred);
}
}
代码如下:
import javaawtColor;import javaawtDimension;
import javaawtFlowLayout;
import javautilHashMap;
import javautilMap;
import javaxswingJComboBox;
import javaxswingJFrame;
import javaxswingJTextField;
public class App extends JFrame {
private JComboBox<String> cbxColors;
private JTextField txtName;
private Map<String, Color> colorMap;
public App() {
thissetSize(300, 100);
thissetLocationRelativeTo(null);
thissetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
thissetLayout(new FlowLayout());
colorMap = new HashMap<>();
colorMapput("红", ColorRED);
colorMapput("绿", ColorGREEN);
colorMapput("蓝", ColorBLUE);
colorMapput("黄", ColorYELLOW);
cbxColors = new JComboBox(colorMapkeySet()toArray());
cbxColorssetPreferredSize(new Dimension(50, 23));
cbxColorsaddItemListener(e -> {
Color color = colorMapget(egetItem());
txtNamesetBackground(color);
});
thisadd(cbxColors);
txtName = new JTextField();
txtNamesetPreferredSize(new Dimension(100, 23));
txtNamesetBackground(ColorRED);
thisadd(txtName);
}
public static void main(String[] args) {
new App()setVisible(true);
}
}
运行结果:
参考。JColorChooser
可以用Java Swing里面的颜色选择器:JColorChooser
例子如下:
public class Color extends javaxswingJFrame {
public Color() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jColorChooser1 = new javaxswingJColorChooser();
setDefaultCloseOperation(javaxswingWindowConstantsEXIT_ON_CLOSE);
orgjdesktoplayoutGroupLayout layout = new orgjdesktoplayoutGroupLayout(getContentPane());
getContentPane()setLayout(layout);
layoutsetHorizontalGroup(
layoutcreateParallelGroup(orgjdesktoplayoutGroupLayoutLEADING)
add(layoutcreateSequentialGroup()
addContainerGap()
add(jColorChooser1, orgjdesktoplayoutGroupLayoutPREFERRED_SIZE, orgjdesktoplayoutGroupLayoutDEFAULT_SIZE, orgjdesktoplayoutGroupLayoutPREFERRED_SIZE)
addContainerGap(orgjdesktoplayoutGroupLayoutDEFAULT_SIZE, ShortMAX_VALUE))
);
layoutsetVerticalGroup(
layoutcreateParallelGroup(orgjdesktoplayoutGroupLayoutLEADING)
add(layoutcreateSequentialGroup()
add(jColorChooser1, orgjdesktoplayoutGroupLayoutPREFERRED_SIZE, orgjdesktoplayoutGroupLayoutDEFAULT_SIZE, orgjdesktoplayoutGroupLayoutPREFERRED_SIZE)
addContainerGap(46, ShortMAX_VALUE))
);
pack();
}// </editor-fold>
public static void main(String args[]) {
javaawtEventQueueinvokeLater(new Runnable() {
public void run() {
new Color()setVisible(true);
}
});
}
// Variables declaration - do not modify
private javaxswingJColorChooser jColorChooser1;
// End of variables declaration
}
本来是在drawcomponent这个里边使用setBackground,你想啊drawcomponent是继承JComponent的所以它是一个容器,所以它同样有setBackground这个方法来设置它的背景颜色
但是因为你在设置它本身为一个画布,因为你用了paintComponent(Graphics g)
这个方法,所以setBackground这个方法即使你用了也看不到很大的效果。但是有一种取代的方法就是在paintComponent(Graphics g)方法中首先就用Graphics 所含有的方法gsetColor(Colorblack);来设置背景颜色再用gfillRect(0, 0, thisgetWidth(), thisgetHeight());来填满整个容器,这就达到了设置背景目的。然后你再gsetColor(其他颜色);来绘制其它图形
具体代码:(在你以上的代码上修改了点)
public void paintComponent(Graphics g)
{
Graphics2D g2=(Graphics2D)g;
gsetColor(Colorblack);//这里设置背景颜色
gfillRect(0, 0, thisgetWidth(), thisgetHeight());//这里填充背景颜色
double x=100;
double y=100;
double w=200;
double h=150;
Rectangle2D rect=new Rectangle2DDouble(x,y,w,h);
g2setPaint(Colorwhite);//这里是你设置其他笔触颜色
g2draw(rect);
Ellipse2D ellipse=new Ellipse2DDouble();
ellipsesetFrame(rect);
g2draw(ellipse);
Point2D p1=new Point2DDouble(x-40,y-30);
Point2D p2=new Point2DDouble(x+w+40,y+h+30);
g2draw(new Line2DDouble(p1,p2));
double centerx=rectgetCenterX();
double centery=rectgetCenterY();
double radius=150;
Ellipse2D circle=new Ellipse2DDouble();
circlesetFrameFromCenter(centerx,centery,centerx+125,centery+125);
g2draw(circle);
}
测试结果图
以上就是关于用JAVA编写一个简单的屏幕变色程序,当用户单击“变色”按钮时,窗口颜色就自动地变成另外一种颜色。全部的内容,包括:用JAVA编写一个简单的屏幕变色程序,当用户单击“变色”按钮时,窗口颜色就自动地变成另外一种颜色。、java里如何声明一个颜色类、用java声明一个颜色类Color等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)