我正在尝试创建一个GUI,我可以在其中创建自定义颜色。我有一个功能,让用户在提交颜色之前预览颜色。我根本无法显示图标。实际问题出在我的ColorFrame类中。颜色图标(JLabel,颜色)在actionperformed()方法中创建。
创建Icon(ButtonListener类)的代码:
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class ColorFrame extends JFrame implements ActionListener {
private JPanel colorPanel;
private JPanel labelPanel;
private JPanel buttonPanel;
private JLabel labelRed;
private JLabel labelGreen;
private JLabel labelBlue;
private JLabel color;
private JTextField redField;
private JTextField greenField;
private JTextField blueField;
private JButton preview;
private JButton submit;
private int redInt;
private int blueInt;
private int greenInt;
private int width = 30;
private int height = 30;
public ColorFrame(int x, int y)
{
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //closes all frames for some reason
this.pack();
this.setLocation(x, y);
buttonPanel = new JPanel();
this.getContentPane().add(buttonPanel,BorderLayout.SOUTH);
preview = new JButton("Preview Color");
preview.addActionListener(new ButtonListener());
buttonPanel.add(preview);
createColorPanel();
createLabelPanel();
this.setVisible(true);
}
class ButtonListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent arg0) {
//in the gui, there are three jtextfieilds that represent color values. the first is red, the second is green, and the last is blue.
redInt = Integer.parseInt(redField.getText());
greenInt = Integer.parseInt(greenField.getText());
blueInt = Integer.parseInt(blueField.getText());
color = new JLabel(new ColorIcon(redInt,greenInt,blueInt));
buttonPanel.add(color,BorderLayout.SOUTH);
print(redInt,greenInt, blueInt);
}
}
private void createColorPanel()
{
colorPanel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
//creates the three textfields to input rgb values.
//the first is r, second, b third g
redField = new JTextField(2);
c.fill = GridBagConstraints.VERTICAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 0;
c.insets = new Insets(10,0,0,10);
colorPanel.add(redField,c);
greenField = new JTextField(2);
c.fill = GridBagConstraints.VERTICAL;
c.weightx = 0.0;
c.gridx = 1;
c.gridy = 1;
colorPanel.add(greenField,c);
blueField = new JTextField(2);
c.fill = GridBagConstraints.VERTICAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 2;
colorPanel.add(blueField,c);
redField.addActionListener(this);
greenField.addActionListener(this);
blueField.addActionListener(this);
this.add(colorPanel, BorderLayout.EAST);
}
public void actionPerformed(ActionEvent arg0) {
redInt = Integer.parseInt(redField.getText());
greenInt = Integer.parseInt(greenField.getText());
blueInt = Integer.parseInt(blueField.getText());
// creates the color icon. It works sometimes, but not every time.
color = new JLabel(new ColorIcon(redInt,greenInt,blueInt));
buttonPanel.add(color);
print(redInt,greenInt, blueInt);
}
private void print(int a, int b, int c)
{
// just to see if the actionperformed() method works.
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
public Dimension getPreferredSize() {
return new Dimension(300,300);
}
public static void main(String[] args)
{
ColorFrame frame = new ColorFrame(200,200);
}
}
Color Icon类:
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import javax.swing.Icon;
public class ColorIcon implements Icon{
private final int size = 30;
private int red;
private int green;
private int blue;
public ColorIcon(int r, int g, int b)
{
this.red = r;
this.green = g;
this.blue = b;
}
@Override
public int getIconHeight() {
// TODO Auto-generated method stub
return size;
}
@Override
public int getIconWidth() {
// TODO Auto-generated method stub
return size;
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
// TODO Auto-generated method stub
Graphics2D g2 = (Graphics2D) g;
Rectangle2D.Double square = new Rectangle2D.Double(50, 50, size, size);
g2.setColor(new Color(red,green,blue));
g2.fill(square);
}
}
答案 0 :(得分:2)
您可以通过多种方式处理此问题。一种是创建一个面板,您可以在其中设置颜色并绘制整个面板。像
这样的东西private class ColorPanel extends JPanel {
private Color color = Color.BLUE;
public void setColor(Color color) {
this.color = color;
repaint();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(color);
g.fillRect(0, 0, getWidth(), getHeight());
}
public Dimension getPreferredSize() {
return new Dimension(150, 150);
}
}
您可以设置颜色。我注意到的另一件事是你试图将新标签添加到按钮面板,但按钮面板位于框架的南边。我想你想要标签在框架的中心。因此,您可以将新标签单独添加到CENTER,将不添加到按钮面板。
这是您的代码的重构。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class ColorFrame extends JFrame implements ActionListener {
private JPanel colorPanel;
private JPanel buttonPanel;
private JLabel color;
private JTextField redField;
private JTextField greenField;
private JTextField blueField;
private JButton preview;
private int redInt;
private int blueInt;
private int greenInt;
private ColorPanel cPanel = new ColorPanel();
public ColorFrame(int x, int y) {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buttonPanel = new JPanel();
JPanel preferredSizeWrapper = new JPanel(new GridBagLayout());
preferredSizeWrapper.add(cPanel);
this.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
preview = new JButton("Preview Color");
preview.addActionListener(new ButtonListener());
buttonPanel.add(preview);
createColorPanel();
this.add(preferredSizeWrapper);
this.pack();
this.setLocation(x, y);
this.setVisible(true);
}
class ButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent arg0) {
// in the gui, there are three jtextfieilds that represent color
// values. the first is red, the second is green, and the last is
// blue.
redInt = Integer.parseInt(redField.getText());
greenInt = Integer.parseInt(greenField.getText());
blueInt = Integer.parseInt(blueField.getText());
// new ColorIcon(redInt, greenInt, blueInt)
cPanel.setColor(new Color(redInt, greenInt, blueInt));
}
}
private class ColorPanel extends JPanel {
private Color color = Color.BLUE;
public void setColor(Color color) {
this.color = color;
repaint();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(color);
g.fillRect(0, 0, getWidth(), getHeight());
}
public Dimension getPreferredSize() {
return new Dimension(150, 150);
}
}
private void createColorPanel() {
colorPanel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
// creates the three textfields to input rgb values.
// the first is r, second, b third g
redField = new JTextField(2);
c.fill = GridBagConstraints.VERTICAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 0;
c.insets = new Insets(10, 0, 0, 10);
colorPanel.add(redField, c);
greenField = new JTextField(2);
c.fill = GridBagConstraints.VERTICAL;
c.weightx = 0.0;
c.gridx = 1;
c.gridy = 1;
colorPanel.add(greenField, c);
blueField = new JTextField(2);
c.fill = GridBagConstraints.VERTICAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 2;
colorPanel.add(blueField, c);
redField.addActionListener(this);
greenField.addActionListener(this);
blueField.addActionListener(this);
this.add(colorPanel, BorderLayout.EAST);
}
public void actionPerformed(ActionEvent arg0) {
redInt = Integer.parseInt(redField.getText());
greenInt = Integer.parseInt(greenField.getText());
blueInt = Integer.parseInt(blueField.getText());
colorPanel.add(color);
}
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
ColorFrame frame = new ColorFrame(200, 200);
}
});
}
}
另一种选择就是使用常规的JPanel
private JPanel createCPanel() {
return new JPanel() {
{
setBackground(Color.BLUE);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(150, 150);
}
};
}
然后只需在面板上调用setBackground(Color)
即可。不过,您还需要记住上述变化。
答案 1 :(得分:1)
不要创建颜色图标,而是尝试将背景颜色设置为您想要的颜色,然后设置标签的大小。