2015-04-05 74 views
2

所以这将是非常基本的,但我无法弄清楚。我有一个CarIcon,它实现了一个Icon接口和一个Resizable接口(我将在后面实现),并且我已经设置了一切,但我无法弄清楚如何将我的图标添加到主要的JPanel或JFrame中。我的意思是,我没有组件和图形信息来调用我的CarIcon类中的paintIcon方法,那么我该怎么做?如何将图标添加到JPanel

下面是一些相关的代码:

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(主)

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


} 

感谢您的帮助提前复活节快乐!

下将无法正常工作:

final static Icon myCar = new CarIcon(20); 
final static JLabel label = new JLabel(myCar); 

编译器说: “构造函数的JLabel(图标)是不确定的”

另一个编辑: 还能有什么毛病我的Eclipse?这里是一个屏幕截图,我把你的代码,并将其粘贴在我的程序,它抛出一个错误:

enter image description here

回答

2

图标不自然地进入JPanels,但他们自然,轻松地进入JLabels,然后JLabels可以轻松进入JPanel,我认为这正是你应该做的:

  • 用你的图标创建一个JLabel。您可以通过将图标传入JLabel的构造函数或通过在JLabel上调用setIcon(...)来完成此操作。
  • 您的JLabel添加到您的JPanel使用任何你需要适当的布局管理器
  • 说的JPanel添加到另一个JPanel要么使用一个顶层窗口或者是任何一个需要去。

如果你的图标适当执行,你会做这样的事情:

JLabel myLabel = new JLabel(myIcon); 
myPanel.add(myLabel); 

或者例如你的代码:

JLabel label = new JLabel(myCar); 
    panel.add(label); 

    JFrame frame = new JFrame(); 
    frame.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); 
    frame.add(panel); 

或者更简单地说:

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

这里是我的整个测试程序:

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

这都说明这一点:

enter image description here

+0

嗯,你能不能把制定几分?我遇到了JLabel和JPanel一样的问题。我不能简单地去label.add(myCar)。有没有一些工作,我失踪了。我的车不是图像,(它实现了界面图标)。 – aurora91 2015-04-05 19:09:11

+0

另外,panel.setIcon(myCar)将不起作用,因为它只适用于Java swing Icon并且投射它会引发异常。 – aurora91 2015-04-05 19:12:32

+0

@ aurora91:要设置JLabel的图标,您必须执行以下两件事之一:您可以通过将图标传入JLabel的构造函数或通过调用JLabel上的'setIcon(...)'来执行此操作。如果你已经正确地实现了接口,那么JLabel应该处理得很好。 – 2015-04-05 19:19:00