网站首页 > 文章中心 > 其它

java颜色代码简单

作者:小编 更新时间:2023-10-19 13:27:14 浏览量:359人看过

我的世界颜色代码大全(我的世界颜色代码表)

①.、我的世界颜色代码大全.

①我的世界颜色代码是amp.

①.0.b=蓝绿、amp.

java设定背景颜色

本来是在drawcomponent这个里边使用setBackground,你想啊drawcomponent是继承JComponent的所以它是一个容器,所以它同样有setBackground这个方法来设置它的背景颜色

但是因为你在设置它本身为一个画布,因为你用了paintComponent(Graphics?g)

java颜色代码简单-图1

这个方法,所以setBackground这个方法即使你用了也看不到很大的效果.但是有一种取代的方法就是在paintComponent(Graphics?g)方法中首先就用Graphics?所含有的方法g.setColor(Color.black);来设置背景颜色再用g.fillRect(0,?0,?this.getWidth(),?this.getHeight());来填满整个容器,这就达到了设置背景目的.然后你再g.setColor(其他颜色);来绘制其它图形.

具体代码:(在你以上的代码上修改了点)

public?void?paintComponent(Graphics?g)

{

g.setColor(Color.black);//这里设置背景颜色

g.fillRect(0,?0,?this.getWidth(),?this.getHeight());//这里填充背景颜色

double?x=100;

double?y=100;

java颜色代码简单-图2

ellipse.setFrame(rect);

double?centerx=rect.getCenterX();

double?centery=rect.getCenterY();

}

测试结果图

爱心代码编程java怎样加颜色

①.、首先打开java编译软件,引入爱心代码编程.

java通过按钮改变文本框内字体颜色,在原代码上面改,尽量简单一点?

import java.awt.BorderLayout;

import java.awt.Button;

import java.awt.Color;

import java.awt.FlowLayout;

import java.awt.Frame;

import java.awt.Insets;

import java.awt.TextArea;

import java.awt.TextField;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import javax.swing.BorderFactory;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JTextPane;

import javax.swing.border.EmptyBorder;

import javax.swing.text.AttributeSet;

import javax.swing.text.BadLocationException;

import javax.swing.text.SimpleAttributeSet;

import javax.swing.text.StyleConstants;

import javax.swing.text.StyleContext;

import javax.swing.text.StyledDocument;

public class main {

static String tf_str=null;

public static void main(String[] args) {

Frame a = new Frame("打印");

a.setLayout(new FlowLayout());

Button c = new Button("确定");

Button e = new Button("红色");

Button f = new Button("蓝色");

java颜色代码简单-图3

JTextPane d=new JTextPane();

d.setMargin(new Insets(100,100, 100, 100));

a.add(b);

a.add(c);

a.add(d);

a.add(e);

a.add(f);

a.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

});

c.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

d.setText("");

tf_str = b.getText().trim();

b.setText("");

appendToPane(d, tf_str, Color.black);

b.requestFocus();

e.addActionListener(new ActionListener() {

appendToPane(d, tf_str, Color.RED);

f.addActionListener(new ActionListener() {

appendToPane(d, tf_str, Color.BLUE);

a.setVisible(true);

private static void appendToPane(JTextPane tp, String msg, Color c) {

StyleContext sc = StyleContext.getDefaultStyleContext();

AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);

aset = sc.addAttribute(aset, StyleConstants.FontFamily, "宋体");

aset = sc.addAttribute(aset, StyleConstants.Alignment, StyleConstants.ALIGN_JUSTIFIED);

int len = tp.getDocument().getLength();

tp.setCaretPosition(len);

tp.setCharacterAttributes(aset, false);

tp.replaceSelection(msg);

版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章