[英]How to add Icon to JPanel
So this is going to be very basic, but I can't figure it out. 所以这将是非常基本的,但我无法弄清楚。 I have a CarIcon, which implements an Icon interface and a Resizable interface (I'll be implementing that later) and I have everything set up, but I can't figure out how to add my icon in to the JPanel or JFrame from main. 我有一个CarIcon,它实现了一个Icon界面和一个Resizable界面(稍后我会实现),我已经设置了所有内容,但我无法弄清楚如何将我的图标添加到JPanel或JFrame中。 I mean, I don't have the Component and Graphics information in order to call paintIcon method in my CarIcon class, so what do I do? 我的意思是,我没有组件和图形信息,以便在我的CarIcon类中调用paintIcon方法,所以我该怎么办?
Here's some relevant code: 这是一些相关的代码:
CarIcon CarIcon
import java.awt.*;
import java.awt.geom.*;
public class CarIcon implements Icon, Resizable{
private int width;
/**
* Construct a car of a given width.
* @param width: the width of the car
*/
public CarIcon(int aWidth){
width = aWidth;
}
public int getIconWidth(){
return width;
}
public int getIconHeight(){
return width/2;
}
public void paintIcon(Component c, Graphics g, int x, int y){
Graphics2D g2 = (Graphics2D) g;
Rectangle2D.Double body = new Rectangle2D.Double(x, y + width / 6, width -1, width / 6);
Ellipse2D.Double frontTire = new Ellipse2D.Double(x + width / 6, y + width / 3, width / 6, width /6);
Ellipse2D.Double rearTire = new Ellipse2D.Double(x + width * 2 / 3, y + width / 3, width / 6, width / 6);
// The bottom of the front windshield
Point2D.Double r1 = new Point2D.Double(x + width / 6, y + width / 6);
// The front of the roof
Point2D.Double r2 = new Point2D.Double(x + width / 3, y);
// The rear of the roof
Point2D.Double r3 = new Point2D.Double(x + width * 2 / 3, y);
// The bottom of the rear windshield
Point2D.Double r4 = new Point2D.Double(x + width * 5 /6, y + width / 6);
Line2D.Double frontWindshield = new Line2D.Double(r1, r2);
Line2D.Double roofTop = new Line2D.Double(r2, r3);
Line2D.Double rearWindshield = new Line2D.Double(r3, r4);
g2.fill(frontTire);
g2.fill(rearTire);
g2.setColor(Color.RED);
g2.fill(body);
g2.draw(frontWindshield);
g2.draw(roofTop);
g2.draw(rearWindshield);
}
@Override
public void resize(int y) {
// TODO Auto-generated method stub
width += y;
}
@Override
public void setIconWidth(int x) {
// TODO Auto-generated method stub
width = x;
}
}
SliderTester (main) SliderTester(主)
import javax.swing.JFrame;
import javax.swing.JPanel;
public class SliderTester extends JPanel{
private static final int DEFAULT_WIDTH = 400;
private static final int DEFAULT_HEIGHT = 200;
final static Icon myCar = new CarIcon(20);
final static JPanel panel = new JPanel();
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame = new JFrame();
frame.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Thanks for your help in advance and Happy Easter! 感谢您提前帮助和复活节快乐!
The following won't work: 以下内容不起作用:
final static Icon myCar = new CarIcon(20);
final static JLabel label = new JLabel(myCar);
Compiler says "The constructor JLabel(Icon) is undefined" 编译器说“构造函数JLabel(Icon)未定义”
Another edit: Can there be something wrong with my Eclipse? 另一个编辑:我的Eclipse可能有问题吗? Here is a screen shot where I take your code and paste it in my program and it throws an error: 这是一个屏幕截图,我将你的代码粘贴到我的程序中,它会抛出一个错误:
Icons don't naturally go into JPanels, but they do naturally and easily go into JLabels, and then JLabels can easily go into a JPanel, and I think that this is exactly what you should do: 图标不自然地进入JPanels,但他们做自然和容易进入的JLabel,然后Jlabel之下可以轻松地进入一个JPanel,我认为这是你应该怎么做:
setIcon(...)
on the JLabel. 您可以通过将Icon传递给JLabel的构造函数或通过在JLabel上调用setIcon(...)
来完成此操作。 If you've implemented your Icon appropriately, you'd do something like: 如果您已经适当地实施了Icon,那么您可以执行以下操作:
JLabel myLabel = new JLabel(myIcon);
myPanel.add(myLabel);
Or for example with your code: 或者例如使用您的代码:
JLabel label = new JLabel(myCar);
panel.add(label);
JFrame frame = new JFrame();
frame.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
frame.add(panel);
Or more simply: 或者更简单:
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JLabel label = new JLabel(new CarIcon(40));
JPanel panel = new JPanel();
panel.add(label);
JOptionPane.showMessageDialog(null, panel);
}
});
}
Here's my whole test program: 这是我的整个测试程序:
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
public class TestCarIcon {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JLabel label = new JLabel(new CarIcon(80));
JPanel panel = new JPanel();
panel.add(label);
JOptionPane.showMessageDialog(null, panel);
}
});
}
}
class CarIcon implements Icon, Resizable {
private int width;
/**
* Construct a car of a given width.
*
* @param width
* : the width of the car
*/
public CarIcon(int aWidth) {
width = aWidth;
}
public int getIconWidth() {
return width;
}
public int getIconHeight() {
return width / 2;
}
public void paintIcon(Component c, Graphics g, int x, int y) {
Graphics2D g2 = (Graphics2D) g;
// !! Added to smooth images
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Rectangle2D.Double body = new Rectangle2D.Double(x, y + width / 6,
width - 1, width / 6);
Ellipse2D.Double frontTire = new Ellipse2D.Double(x + width / 6, y
+ width / 3, width / 6, width / 6);
Ellipse2D.Double rearTire = new Ellipse2D.Double(x + width * 2 / 3, y
+ width / 3, width / 6, width / 6);
// The bottom of the front windshield
Point2D.Double r1 = new Point2D.Double(x + width / 6, y + width / 6);
// The front of the roof
Point2D.Double r2 = new Point2D.Double(x + width / 3, y);
// The rear of the roof
Point2D.Double r3 = new Point2D.Double(x + width * 2 / 3, y);
// The bottom of the rear windshield
Point2D.Double r4 = new Point2D.Double(x + width * 5 / 6, y + width / 6);
Line2D.Double frontWindshield = new Line2D.Double(r1, r2);
Line2D.Double roofTop = new Line2D.Double(r2, r3);
Line2D.Double rearWindshield = new Line2D.Double(r3, r4);
g2.fill(frontTire);
g2.fill(rearTire);
g2.setColor(Color.RED);
g2.fill(body);
g2.draw(frontWindshield);
g2.draw(roofTop);
g2.draw(rearWindshield);
}
@Override
public void resize(int y) {
width += y;
}
@Override
public void setIconWidth(int x) {
width = x;
}
}
interface Resizable {
void resize(int y);
void setIconWidth(int x);
}
Which shows this: 这显示了这个:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.