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中将十六进制代码转换为颜色。我遇到了这个问题。
I want to know how to paint the background based on the color of the hex value inputed by the user. I have this:
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);
}
}
But I want to know how to fix it such that incase the user decides to put a 0x in front of the hexadecimal code or not it will work. I also want to know how to convert the hex code into a color in java. I am having trouble with this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个 JUnit 测试可能会帮助您理解:
您可以使用
Integer.parseInt
将十六进制字符串转换为以 16 为基数的数字。请注意,如果字符串无效(包含数字以外的字符),这将引发异常或 af)。然后可以使用
Integer
创建Color
实例。我添加了一个断言来表明所创建的就是我们期望的那样。
This JUnit test may help you understand:
You can use
Integer.parseInt
to turn the hexadecimal string into a number of base 16. Note this will throw an exception if the string is invalid (contains characters other than digits or a-f).Color
instances can then be created using theInteger
.I have included an assertion to show that the created is what we expect it to be.
好吧,将十六进制字符串转换为颜色:
或者您可以这样做:
但是输入不能是字符串,否则您将得到 NumberFormatException。
判断输入是否以 0x 开头应该很容易
Well, converting a hex-string into a Color:
Or you could do:
But then the input can't be a String, or else you'll get a NumberFormatException.
Figuring out if the input starts with an 0x should be easy