我想知道如何根據用戶輸入的十六進制值的顏色來繪製背景。我有這樣的:Java中的顏色計算器幫助
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class SimpleColorCalc extends JFrame implements Runnable
{
ColorPanel cp;
JButton show;
JTextField hexCode;
public SimpleColorCalc()
{
super("Simple Color Calculator");
cp = new ColorPanel();
show = new JButton("Show the Color");
hexCode = new JTextField("ffffff");
show.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
String text = hexCode.getText();
try
{
int colorCode = Integer.parseInt(text, 16);
Color enteredColor = new Color(colorCode);
cp.setColor(enteredColor);
}
catch(NumberFormatException ex)
{
hexCode.setText("ffffff");
cp.setColor(Color.white);
}
finally
{
repaint();
}
}
});
}
public static void main(String[] args)
{
SimpleColorCalc scc = new SimpleColorCalc();
javax.swing.SwingUtilities.invokeLater(scc);
}
public void run()
{
setSize(400,300);
Container c = getContentPane();
JPanel top = new JPanel();
c.add(BorderLayout.NORTH, top);
top.setLayout(new GridLayout(1,2));
top.add(show);
top.add(hexCode);
setVisible(true);
}
}
但我想知道如何解決它,從而櫃面用戶決定把0X的十六進制代碼的前面或不是會工作。我也想知道如何將十六進制代碼轉換爲java中的顏色。我遇到了麻煩。
計算器淋浴? – Seth 2010-10-01 21:00:10
Woops ...輸入錯誤,我會解決這個問題 – Salazar 2010-10-01 21:02:56