如何向 JPanel 添加图标

aur*_*a91 5 java swing

所以这将是非常基本的,但我无法弄清楚。我有一个 CarIcon,它实现了一个 Icon 接口和一个可调整大小的接口(我将在稍后实现),并且我已设置好所有内容,但我不知道如何从 main 将我的图标添加到 JPanel 或 JFrame 。我的意思是,我没有组件和图形信息来调用 CarIcon 类中的 PaintIcon 方法,那么我该怎么办?

这是一些相关代码:

汽车图标

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

}
Run Code Online (Sandbox Code Playgroud)

滑块测试仪(主要)

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


}
Run Code Online (Sandbox Code Playgroud)

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

以下内容将不起作用:

final static Icon myCar = new CarIcon(20);
final static JLabel label = new JLabel(myCar);
Run Code Online (Sandbox Code Playgroud)

编译器显示“构造函数 JLabel(Icon) 未定义”

另一个编辑:我的 Eclipse 有问题吗?这是一个屏幕截图,我将您的代码粘贴到我的程序中,它抛出一个错误:

在此输入图像描述

Hov*_*els 6

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

  • 使用您的 Icon 创建一个 JLabel。您可以通过将 Icon 传递到 JLabel 的构造函数或调用setIcon(...)JLabel 来完成此操作。
  • 使用您需要的任何适当的布局管理器将 JLabel 添加到 JPanel
  • 将该 JPanel 添加到另一个 JPanel 或顶级窗口或任何需要的位置。

如果你已经适当地实现了你的图标,你会做类似的事情:

JLabel myLabel = new JLabel(myIcon);
myPanel.add(myLabel);
Run Code Online (Sandbox Code Playgroud)

或者例如使用您的代码:

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

  JFrame frame = new JFrame();
  frame.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
  frame.add(panel);
Run Code Online (Sandbox Code Playgroud)

或者更简单地说:

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);
     }
  });
}
Run Code Online (Sandbox Code Playgroud)

这是我的整个测试程序:

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);
}
Run Code Online (Sandbox Code Playgroud)

这表明:

在此输入图像描述