java JTextPane 问题
1个窗口中有一个JTextPane,里面有一些字符串有几个按钮,分别为红黄绿,点击改变JTextPane里的字符颜色那位高手帮我写下不甚感激!!!不好意思是我没说清楚,谢...
1个 窗口中有一个 JTextPane ,里面有一些字符串
有几个 按钮,分别为红 黄 绿,点击改变 JTextPane里的字符颜色
那位高手帮我写下 不甚感激!!!
不好意思 是我没说清楚,谢谢 网友的热心回答
我的意思是控制 部分的文字变色,就是说在一个JTextPane
中 有一窜字符(黑色),有一个按钮 每点击一下 就有一个字符变成红色。
实现的是部分的变色,谢谢可不可以 帮个忙 帮我改改, 好的我再追加分数。! 展开
有几个 按钮,分别为红 黄 绿,点击改变 JTextPane里的字符颜色
那位高手帮我写下 不甚感激!!!
不好意思 是我没说清楚,谢谢 网友的热心回答
我的意思是控制 部分的文字变色,就是说在一个JTextPane
中 有一窜字符(黑色),有一个按钮 每点击一下 就有一个字符变成红色。
实现的是部分的变色,谢谢可不可以 帮个忙 帮我改改, 好的我再追加分数。! 展开
5个回答
展开全部
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.WindowConstants;
public class ChangColor extends javax.swing.JFrame {
private JPanel jPanel1;
private JButton bntblue;
private JButton bntred;
private JTextPane txtpane;
private JScrollPane jScrollPane1;
public static void main(String[] args) {
ChangColor inst = new ChangColor();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
public ChangColor() {
super();
initGUI();
}
private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
{
jPanel1 = new JPanel();
getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.setLayout(null);
{
jScrollPane1 = new JScrollPane();
jPanel1.add(jScrollPane1);
jScrollPane1.setBounds(54, 30, 274, 111);
{
txtpane = new JTextPane();
jScrollPane1.setViewportView(txtpane);
txtpane.setText("你好呀");
txtpane.setPreferredSize(new java.awt.Dimension(254,
100));
}
}
{
bntred = new JButton();
jPanel1.add(bntred);
bntred.setText("红色");
bntred.setBounds(35, 194, 70, 30);
bntred.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
txtpane.setForeground(Color.red);
}
});
}
{
bntblue = new JButton();
jPanel1.add(bntblue);
bntblue.setText("\u84dd\u8272");
bntblue.setBounds(121, 194, 70, 30);
bntblue.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
txtpane.setForeground(Color.blue);
}
});
}
}
pack();
setSize(400, 300);
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.WindowConstants;
public class ChangColor extends javax.swing.JFrame {
private JPanel jPanel1;
private JButton bntblue;
private JButton bntred;
private JTextPane txtpane;
private JScrollPane jScrollPane1;
public static void main(String[] args) {
ChangColor inst = new ChangColor();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
public ChangColor() {
super();
initGUI();
}
private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
{
jPanel1 = new JPanel();
getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.setLayout(null);
{
jScrollPane1 = new JScrollPane();
jPanel1.add(jScrollPane1);
jScrollPane1.setBounds(54, 30, 274, 111);
{
txtpane = new JTextPane();
jScrollPane1.setViewportView(txtpane);
txtpane.setText("你好呀");
txtpane.setPreferredSize(new java.awt.Dimension(254,
100));
}
}
{
bntred = new JButton();
jPanel1.add(bntred);
bntred.setText("红色");
bntred.setBounds(35, 194, 70, 30);
bntred.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
txtpane.setForeground(Color.red);
}
});
}
{
bntblue = new JButton();
jPanel1.add(bntblue);
bntblue.setText("\u84dd\u8272");
bntblue.setBounds(121, 194, 70, 30);
bntblue.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
txtpane.setForeground(Color.blue);
}
});
}
}
pack();
setSize(400, 300);
} catch (Exception e) {
e.printStackTrace();
}
}
}
展开全部
import .....;
public class ChangeColorTest{
public static void main(String[] args)
{
TextFrame frame = new TextFrame();
frame.setDefaultCloseOperation (EXIT_ON_CLOSE);
frame.show();
}
class TextFrame extends JFrame{
public TextFrame()
{
setSize(width,height);
Container panel = getContentPane();
JTextPane textPanel = new JTextPane();
panel.add(textPanel);
}
private final int width=300;
private final int height=200;
}
class JTextPane extends JPanel{
JButton button = null;
JButton button1 = null;
JButton button2 = null;
int flag=0;
public JTextPane(){
if(flag==0)
{
repaint();
}
Icon iconOld = new ImageIcon("F:/.../.../old.jpg");
button = new JButton("红",iconOld);
button1 = new JButton("黄",iconOld);
button2 = new JButton("绿",iconOld);
button.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent e1){
flag=1;
repaint();
}
});
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e1){
flag=2;
repaint();
}
});
button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e1){
flag=3;
repaint();
}
});
}
public void print(Graphics g)
{
if(flag=0)
{
g.setColor(new Color(0,255,0)); // set 其他颜色代码
g.drawString("点击按扭改变颜色",20,40);
}else if(flag==1)
{
g.setColor(new Color(0,255,0)); // set 红颜色代码
g.drawString("点击按扭改变颜色",20,40);
}else if(flag==2)
{
g.setColor(new Color(0,255,0)); // set 黄颜色代码
g.drawString("点击按扭改变颜色",20,40);
}else{
g.setColor(new Color(0,255,0)); // set 绿颜色代码
g.drawString("点击按扭改变颜色",20,40);
}
}
}
}
}
public class ChangeColorTest{
public static void main(String[] args)
{
TextFrame frame = new TextFrame();
frame.setDefaultCloseOperation (EXIT_ON_CLOSE);
frame.show();
}
class TextFrame extends JFrame{
public TextFrame()
{
setSize(width,height);
Container panel = getContentPane();
JTextPane textPanel = new JTextPane();
panel.add(textPanel);
}
private final int width=300;
private final int height=200;
}
class JTextPane extends JPanel{
JButton button = null;
JButton button1 = null;
JButton button2 = null;
int flag=0;
public JTextPane(){
if(flag==0)
{
repaint();
}
Icon iconOld = new ImageIcon("F:/.../.../old.jpg");
button = new JButton("红",iconOld);
button1 = new JButton("黄",iconOld);
button2 = new JButton("绿",iconOld);
button.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent e1){
flag=1;
repaint();
}
});
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e1){
flag=2;
repaint();
}
});
button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e1){
flag=3;
repaint();
}
});
}
public void print(Graphics g)
{
if(flag=0)
{
g.setColor(new Color(0,255,0)); // set 其他颜色代码
g.drawString("点击按扭改变颜色",20,40);
}else if(flag==1)
{
g.setColor(new Color(0,255,0)); // set 红颜色代码
g.drawString("点击按扭改变颜色",20,40);
}else if(flag==2)
{
g.setColor(new Color(0,255,0)); // set 黄颜色代码
g.drawString("点击按扭改变颜色",20,40);
}else{
g.setColor(new Color(0,255,0)); // set 绿颜色代码
g.drawString("点击按扭改变颜色",20,40);
}
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
//直接编译》运行》ok的
貌似题目看错了!!!
呵呵呵……
你自己改改吧。我还以为设置背景色(⊙o⊙)?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonTest {
public static void main(String[] args) {
ButtonFrame frame = new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class ButtonFrame extends JFrame {
public ButtonFrame() {
setTitle("ButtonTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
ButtonPanel panel = new ButtonPanel();
add(panel);
}
public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 200;
}
class ButtonPanel extends JPanel {
public ButtonPanel() {
JButton yellowButton = new JButton("Yellow");
JButton blueButton = new JButton("Blue");
JButton redButton = new JButton("Red");
add(yellowButton);
add(blueButton);
add(redButton);
ColorAction yellowAction = new ColorAction(Color.YELLOW);
ColorAction blueAction = new ColorAction(Color.BLUE);
ColorAction redAction = new ColorAction(Color.RED);
yellowButton.addActionListener(yellowAction);
blueButton.addActionListener(blueAction);
redButton.addActionListener(redAction);
}
private class ColorAction implements ActionListener {
public ColorAction(Color c) {
backgroundColor = c;
}
public void actionPerformed(ActionEvent event) {
setBackground(backgroundColor);
}
private Color backgroundColor;
}
}
貌似题目看错了!!!
呵呵呵……
你自己改改吧。我还以为设置背景色(⊙o⊙)?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonTest {
public static void main(String[] args) {
ButtonFrame frame = new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class ButtonFrame extends JFrame {
public ButtonFrame() {
setTitle("ButtonTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
ButtonPanel panel = new ButtonPanel();
add(panel);
}
public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 200;
}
class ButtonPanel extends JPanel {
public ButtonPanel() {
JButton yellowButton = new JButton("Yellow");
JButton blueButton = new JButton("Blue");
JButton redButton = new JButton("Red");
add(yellowButton);
add(blueButton);
add(redButton);
ColorAction yellowAction = new ColorAction(Color.YELLOW);
ColorAction blueAction = new ColorAction(Color.BLUE);
ColorAction redAction = new ColorAction(Color.RED);
yellowButton.addActionListener(yellowAction);
blueButton.addActionListener(blueAction);
redButton.addActionListener(redAction);
}
private class ColorAction implements ActionListener {
public ColorAction(Color c) {
backgroundColor = c;
}
public void actionPerformed(ActionEvent event) {
setBackground(backgroundColor);
}
private Color backgroundColor;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
等我写完给你发过去。
才子_辉祝您愉快!
才子_辉祝您愉快!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
给我个邮箱 我发给你
不懂问我
不懂问我
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询