gpt4 book ai didi

java - 如何让我的彩色图标显示在我的 GUI 上?

转载 作者:行者123 更新时间:2023-11-29 05:21:09 25 4
gpt4 key购买 nike

我正在尝试创建一个 GUI,我可以在其中创建自定义颜色。我有一个功能可以让用户在提交颜色之前预览颜色。我根本无法显示图标。实际问题出在我的 ColorFrame 类中。颜色 Icon(a JLabel, color) 在 actionperformed() 方法中创建。

创建图标的代码(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);
}


}

颜色图标类:

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);
}

}

最佳答案

有多种可能的方法可以处理此问题。一种是创建一个面板,您可以在其中设置颜色并绘制整个面板。有点像

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)。不过,您还需要牢记上述更改。

关于java - 如何让我的彩色图标显示在我的 GUI 上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24682824/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com