请问有没有人用过JColorChooser类?能不能给个简单的例子,说明怎样用这个类

重炮军团 2009-05-20 01:59:28
请各位大侠帮帮忙
小生多谢~~
...全文
76 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
没有昵称阿 2009-05-20
  • 打赏
  • 举报
回复
//ShowColors2.java
//Demonstrating JColorChooser.

//Java core packages
import java.awt.*;
import java.awt.event.*;

//Java extension packages
import javax.swing.*;

public class ShowColors2 extends JFrame
{
private JButton changeColorButton;
private Color color = Color.lightGray;
private Container container;

//set up GUI
public ShowColors2()
{
super("Using JColorChooser");

container = getContentPane();
container.setLayout(new FlowLayout());

//set up changeColorButton and register its event handler
changeColorButton = new JButton( "Change Color" );

changeColorButton.addActionListener(
//anonymous inner class
new ActionListener()
{
public void actionPerformed(ActionEvent event )
{
color = JColorChooser.showDialog(ShowColors2.this,"Choose a color",color);

//set default color,if no color is returned
if(color == null )
color = Color.lightGray;

//container.setBackground( color );
}//end actionPerformd

}//end of ActionListioner
);//end call to addActionListener
container.add( changeColorButton );

setSize( 400,130 );
setVisible( true );
}

//execute application
public static void main(String arges[] )
{
ShowColors2 application = new ShowColors2();

application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
}

没有昵称阿 2009-05-20
  • 打赏
  • 举报
回复
JTextArea can display and edit multiple lines of text. Although a text area can display text in any font, all of the text is in the same font. Use a text area to allow the user to enter unformatted text of any length or to display unformatted help information.
你试一下:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;

public class TestForYou{
public static void main(String[] args){
final JFrame f = new JFrame();
final JButton b = new JButton("button");
final JTextPane t = new JTextPane();

b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
JColorChooser jc=new JColorChooser();
Color selectedColor=jc.showDialog(f,"zb",t.getSelectedTextColor());
new StyledEditorKit.ForegroundAction("no-name", selectedColor).actionPerformed(e);
}
});

f.getContentPane().setLayout(new BorderLayout());
f.getContentPane().add(b,BorderLayout.NORTH);
f.getContentPane().add(t,BorderLayout.CENTER);
f.setSize(200,200);
f.setVisible(true);


}
}
先输入一段文本,选一部分,点Button,返回后选中文本已改色,按tab切换至textpane仍为原选中状态
一头头 2009-05-20
  • 打赏
  • 举报
回复
xlongbuilder 2009-05-20
  • 打赏
  • 举报
回复
建议多看看sun 的列子
最好的入门文档
jxplus 2009-05-20
  • 打赏
  • 举报
回复
这是我学习时用的例子:ColorChooserTest.java

/**
@version 1.02 2009-05-20
@author jxplus
*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class ColorChooserTest
{
public static void main(String[] args)
{
ColorChooserFrame frame = new ColorChooserFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

/**
A frame with a color chooser panel
*/
class ColorChooserFrame extends JFrame
{
public ColorChooserFrame()
{
setTitle("ColorChooserTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

// add color chooser panel to frame

ColorChooserPanel panel = new ColorChooserPanel();
add(panel);
}

public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 200;
}

/**
A panel with buttons to pop up three types of color choosers
*/
class ColorChooserPanel extends JPanel
{
public ColorChooserPanel()
{
JButton modalButton = new JButton("Modal");
modalButton.addActionListener(new ModalListener());
add(modalButton);

JButton modelessButton = new JButton("Modeless");
modelessButton.addActionListener(new ModelessListener());
add(modelessButton);

JButton immediateButton = new JButton("Immediate");
immediateButton.addActionListener(new ImmediateListener());
add(immediateButton);
}

/**
This listener pops up a modal color chooser
*/
private class ModalListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
Color defaultColor = getBackground();
Color selected = JColorChooser.showDialog(
ColorChooserPanel.this,
"Set background",
defaultColor);
if (selected != null) setBackground(selected);
}
}

/**
This listener pops up a modeless color chooser.
The panel color is changed when the user clicks the Ok
button.
*/
private class ModelessListener implements ActionListener
{
public ModelessListener()
{
chooser = new JColorChooser();
dialog = JColorChooser.createDialog(
ColorChooserPanel.this,
"Background Color",
false /* not modal */,
chooser,
new ActionListener() // OK button listener
{
public void actionPerformed(ActionEvent event)
{
setBackground(chooser.getColor());
}
},
null /* no Cancel button listener */);
}

public void actionPerformed(ActionEvent event)
{
chooser.setColor(getBackground());
dialog.setVisible(true);
}

private JDialog dialog;
private JColorChooser chooser;
}

/**
This listener pops up a modeless color chooser.
The panel color is changed immediately when the
user picks a new color.
*/
private class ImmediateListener implements ActionListener
{
public ImmediateListener()
{
chooser = new JColorChooser();
chooser.getSelectionModel().addChangeListener(new
ChangeListener()
{
public void stateChanged(ChangeEvent event)
{
setBackground(chooser.getColor());
}
});

dialog = new JDialog(
(Frame) null,
false /* not modal */);
dialog.add(chooser);
dialog.pack();
}

public void actionPerformed(ActionEvent event)
{
chooser.setColor(getBackground());
dialog.setVisible(true);
}

private JDialog dialog;
private JColorChooser chooser;
}
}

可直接运行,希望对你有用。
horizonlyhw 2009-05-20
  • 打赏
  • 举报
回复
百度搜嘛 這個 好多
http://cache.baidu.com/c?m=9f65cb4a8c8507ed4fece763104c8c711923d030678197027fa3c215cc790007506197fe747e4519839b21321cf40f0bb5ed6070360627e7999b8b19d8ecc12a2ed87736711b851710d70eafbc1c639e7e841fb4ef4fe8e2a16fcdf3928285090e8b0445&p=882a954387881bc300adc7710c50&user=baidu 這個就是~

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧