0
如何使用JColorChooser更改java swing中的字體顏色?JColorChooser更改Java中的字體顏色
如何使用JColorChooser更改java swing中的字體顏色?JColorChooser更改Java中的字體顏色
下面是從例子中的代碼,創建一個JColorChooser的實例,並把它添加到一個容器中:
public class ColorChooserDemo extends JPanel ... {
public ColorChooserDemo() {
super(new BorderLayout());
banner = new JLabel("Welcome to the Tutorial Zone!",
JLabel.CENTER);
banner.setForeground(Color.yellow);
. . .
tcc = new JColorChooser(banner.getForeground());
. . .
add(tcc, BorderLayout.PAGE_END);
}
以下碼寄存器,並實現了變化監聽器:
tcc.getSelectionModel().addChangeListener(this);
. . .
public void stateChanged(ChangeEvent e) {
Color newColor = tcc.getColor();
banner.setForeground(newColor);
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JColorChooserExample extends JFrame
implements ActionListener
{
private JButton b;
private Container c;
private Color color;
public JColorChooserExample(String title)
{
super(title);
color = Color.pink;
c = getContentPane();
c.setLayout(new FlowLayout());
c.setBackground(color);
c.add(new JLabel(
"Click button to select a new background color"));
b = new JButton("Color");
b.setToolTipText("Click here to change color");
b.addActionListener(this);
c.add(b);
addWindowListener(new MyWindowCloser());
}
public void actionPerformed(ActionEvent e)
{
color = JColorChooser.showDialog(
this, "Select a Background Color", color);
if (color != null)
{
c.setBackground(color);
}
}
public static void main(String[] args)
{
JColorChooserExample jcce =
new JColorChooserExample("JColorChooser Example");
jcce.pack();
jcce.setVisible(true);
}
}
並閱讀這tutorial也
非常簡單的任務後,你通過[如何使用顏色選擇器]( http://docs.oracle.com/javase/tutorial/uiswing/components/colorchooser.html) –