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

importjava.awt.Button;importjava.awt.FlowLayout;importjava.awt.Frame;importjava.awt.T... import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
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;
public class main {
public static void main(String[] args) {
Frame a = new Frame("打印");
a.setBounds(400, 300, 400, 300);
a.setLayout(new FlowLayout());
TextField b = new TextField(20);
Button c = new Button("确定");
Button e = new Button("红色");
Button f = new Button("蓝色");
TextArea d = new TextArea(10, 40);
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) {
String tf_str = b.getText().trim();
b.setText("");
d.append(tf_str+"\n");
b.requestFocus();
}
});
a.setVisible(true);
}
}
展开
 我来答
xvxvnmnm
2019-12-23 · TA获得超过223个赞
知道小有建树答主
回答量:189
采纳率:100%
帮助的人:97.7万
展开全部
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.setBounds(400, 300, 400, 300);
a.setLayout(new FlowLayout());
TextField b = new TextField(20);
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() {
public void actionPerformed(ActionEvent e) {
d.setText("");
appendToPane(d, tf_str, Color.RED);

}
});
f.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
d.setText("");
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);
}

}
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式