这是我学习时用的例子: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; } }可直接运行,希望对你有用。
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. 你试一下:
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); } });
//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();
http://cache.baidu.com/c?m=9f65cb4a8c8507ed4fece763104c8c711923d030678197027fa3c215cc790007506197fe747e4519839b21321cf40f0bb5ed6070360627e7999b8b19d8ecc12a2ed87736711b851710d70eafbc1c639e7e841fb4ef4fe8e2a16fcdf3928285090e8b0445&p=882a954387881bc300adc7710c50&user=baidu 這個就是~
@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;
}
}可直接运行,希望对你有用。
最好的入门文档
你试一下:
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仍为原选中状态
//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 );
}
}