gpt4 book ai didi

java - Jframe 上不显示标签

转载 作者:行者123 更新时间:2023-12-01 16:55:56 25 4
gpt4 key购买 nike

public class Iconshape implements Icon {


private Color color = Color.RED;

public Iconshape (Color c)
{
this.color = c;
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
Graphics2D g1 = (Graphics2D) g;
Ellipse2D.Double circle = new Ellipse2D.Double (0, 0, 20, 20);
g1.setColor(color);
g1.fill(circle);

}

@Override
public int getIconWidth() {
// TODO Auto-generated method stub
return 0;
}

@Override
public int getIconHeight() {
// TODO Auto-generated method stub
return 0;
}
public Color setColor(Color c)
{
return this.color = c;

}
}


public class Button {

private static JLabel label;
private static Iconshape icon = new Iconshape(Color.RED);

public static void main(String[] args)
{
JFrame frame = new JFrame();
JButton red = new JButton("Red");
JButton green = new JButton("Green");
JButton blue = new JButton("Blue");
label = new JLabel(icon);
final int FRAME_WIDTH = 600;
final int FRAME_HEIGHT = 400;

red.addActionListener(createRedButtonListener(Color.RED));
blue.addActionListener(createRedButtonListener(Color.BLUE));
green.addActionListener(createRedButtonListener(Color.GREEN));

frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setLayout(new FlowLayout());
frame.add(red);
frame.add(blue);
frame.add(green);
frame.add(label);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.pack();
frame.setVisible(true);



}

private static ActionListener createRedButtonListener(Color color) {

return new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
if (color == Color.RED)
{ icon.setColor(Color.RED);
label.repaint();
}
if (color == Color.BLUE)
{ icon.setColor(Color.BLUE);
label.repaint();
}
if (color == Color.GREEN)
{ icon.setColor(Color.GREEN);
label.repaint();
}
}
};
}
}

嗨,我要实现一个在单击按钮时更改标签颜色的程序,但是,Jframe 上唯一显示的是 3 个按钮。我可以得到一些帮助吗,因为我刚刚开始学习,所以对 GUI 的东西不太了解。

这是我目前所拥有的屏幕截图

enter image description here

最佳答案

您的图标宽度和高度均为 0,因此没有任何内容可绘制。

它们的 getIconHeight()getIconWidth() 方法应该返回 20,因为这是椭圆形的大小。

关于java - Jframe 上不显示标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32977249/

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