请问在java中如何改变JTextArea指定位置的背景色?谢谢!
3个回答
展开全部
希望这个例子能帮你,不过好像不是背景色,成前景色了,我们一起想想吧
import javax.swing.*;
import java.awt.*;
public class Test extends JFrame{
MyTextArea area;
public Test() {
super();
area=new MyTextArea();
this.setLayout(new BorderLayout());
add(area,BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setBounds(100,100,300,400);
}
public static void main (String[] args) {
new Test();
}
}
class MyTextArea extends JTextArea{
public MyTextArea(){
super();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.BLUE);
g.fillRect(20,20,30,30);
}
}
import javax.swing.*;
import java.awt.*;
public class Test extends JFrame{
MyTextArea area;
public Test() {
super();
area=new MyTextArea();
this.setLayout(new BorderLayout());
add(area,BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setBounds(100,100,300,400);
}
public static void main (String[] args) {
new Test();
}
}
class MyTextArea extends JTextArea{
public MyTextArea(){
super();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.BLUE);
g.fillRect(20,20,30,30);
}
}
追问
通过这个g.fillRect(20,20,30,30);被填充区域的大小,
那我该如何通过一个具体字符串的起始位置和长度来来改变背景色呢?
追答
这个说实话我也不会
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
应该是改变文本背景色:(下面是我改了一下的代码,应该是你的效果了如果我没理解错的话)
package gui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Menueg extends JFrame implements ActionListener{
private JMenuBar menubar;
private JMenuItem menuItem,a,b;
private JMenu menu,submenu;
private JTextArea text;
private JScrollPane scrollPane;
public Menueg(){
super("Menueg");
setSize(400,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container cp = getContentPane();
text = new JTextArea(5,30);
scrollPane = new JScrollPane(text);
cp.add(scrollPane,BorderLayout.CENTER);
menubar = new JMenuBar();
setJMenuBar(menubar);
menu = new JMenu("文件"); //文件菜单
menubar.add(menu);
submenu = new JMenu("change color");
submenu.addActionListener(this);
submenu.setActionCommand("change color");
menu.add(submenu);
a = new JMenuItem("red");
a.addActionListener(this);
a.setActionCommand("red");
submenu.add(a);
b = new JMenuItem("blue");
b.addActionListener(this);
b.setActionCommand("blue");
submenu.add(b);
// ButtonGroup group = new ButtonGroup();
/* ButtonGroup group = new ButtonGroup();
button1 = new JRadioButtonMenuItem("red");
button1.addActionListener(this);
button1.setActionCommand("red");
button1.setSelected(true);
button2 = new JRadioButtonMenuItem("blue");
button2.addActionListener(this);
button2.setActionCommand("blue");
button2.setSelected(true);
group.add(button1);
submenu.add(button1);
group.add(button2);
submenu.add(button2);
*/
menuItem = new JMenuItem("标签");
menuItem.addActionListener(this);
menuItem.setActionCommand("标签");
menu.add(menuItem);
menu = new JMenu("编辑"); //编辑菜单
menubar.add(menu);
}
public void actionPerformed(ActionEvent e) {
String a = e.getActionCommand();
if(a.equals("red")){
// this.getContentPane().setBackground(Color.RED);
this.text.setBackground(Color.RED);
}
if(a.equals("blue")){
// this.getContentPane().setBackground(Color.BLUE);
this.text.setBackground(Color.BLUE);
}
}
public static void main(String []args){
Menueg eg = new Menueg();
eg.setVisible(true);
}
}
package gui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Menueg extends JFrame implements ActionListener{
private JMenuBar menubar;
private JMenuItem menuItem,a,b;
private JMenu menu,submenu;
private JTextArea text;
private JScrollPane scrollPane;
public Menueg(){
super("Menueg");
setSize(400,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container cp = getContentPane();
text = new JTextArea(5,30);
scrollPane = new JScrollPane(text);
cp.add(scrollPane,BorderLayout.CENTER);
menubar = new JMenuBar();
setJMenuBar(menubar);
menu = new JMenu("文件"); //文件菜单
menubar.add(menu);
submenu = new JMenu("change color");
submenu.addActionListener(this);
submenu.setActionCommand("change color");
menu.add(submenu);
a = new JMenuItem("red");
a.addActionListener(this);
a.setActionCommand("red");
submenu.add(a);
b = new JMenuItem("blue");
b.addActionListener(this);
b.setActionCommand("blue");
submenu.add(b);
// ButtonGroup group = new ButtonGroup();
/* ButtonGroup group = new ButtonGroup();
button1 = new JRadioButtonMenuItem("red");
button1.addActionListener(this);
button1.setActionCommand("red");
button1.setSelected(true);
button2 = new JRadioButtonMenuItem("blue");
button2.addActionListener(this);
button2.setActionCommand("blue");
button2.setSelected(true);
group.add(button1);
submenu.add(button1);
group.add(button2);
submenu.add(button2);
*/
menuItem = new JMenuItem("标签");
menuItem.addActionListener(this);
menuItem.setActionCommand("标签");
menu.add(menuItem);
menu = new JMenu("编辑"); //编辑菜单
menubar.add(menu);
}
public void actionPerformed(ActionEvent e) {
String a = e.getActionCommand();
if(a.equals("red")){
// this.getContentPane().setBackground(Color.RED);
this.text.setBackground(Color.RED);
}
if(a.equals("blue")){
// this.getContentPane().setBackground(Color.BLUE);
this.text.setBackground(Color.BLUE);
}
}
public static void main(String []args){
Menueg eg = new Menueg();
eg.setVisible(true);
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |