①.、我的世界颜色代码大全.
①我的世界颜色代码是amp.
①.0.b=蓝绿、amp.
本来是在drawcomponent这个里边使用setBackground,你想啊drawcomponent是继承JComponent的所以它是一个容器,所以它同样有setBackground这个方法来设置它的背景颜色
但是因为你在设置它本身为一个画布,因为你用了paintComponent(Graphics?g)
这个方法,所以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;
ellipse.setFrame(rect);
double?centerx=rect.getCenterX();
double?centery=rect.getCenterY();
}
测试结果图
①.、首先打开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("蓝色");
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);