1

这是我用来加载图像的类,我使用构造函数调用将图像加载到 percorso 数组中的方法加载。

import java.awt.Component;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;

class Track extends Component {
   Image[] percorso;
   String path;
   int n;
   int x;
   int y;

   public Track(String path, int n) {
      int i;
      this.path = path;
      this.n = n;
      this.percorso = new Image[n];
      for (i = 0; i < n; i++) {
         this.percorso[i] = load(path, n, i);
         repaint();
      }
   }

   public final Image load(String path, int n, int i) {
      Toolkit tool = Toolkit.getDefaultToolkit();
      String dot = ".jpg";
      path = this.path;
      path = path + Integer.toString(i) + dot;
      Image img = tool.getImage(path);
      try {
         MediaTracker track = new MediaTracker(this);
         track.addImage(img, 0);
         track.waitForID(0);
      } catch (InterruptedException e) {
         System.out.println("Image not loaded");
      }
      return (img);
   }

   @Override
   public void paint(Graphics g) {
      int i;
      g.drawImage(percorso[1], x, y, this);
   }
}

这是我创建 gui 的类,我尝试将图像添加到 jlayeredpane,但它没有用。

public class Gui extends javax.swing.JFrame {

   /** Creates new form GUI */
   public Gui() {
      initComponents();
   }

   /**
    * This method is called from within the constructor to initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is always
    * regenerated by the Form Editor.
    */
   @SuppressWarnings("unchecked")
   // <editor-fold defaultstate="collapsed" desc="Generated Code">
   private void initComponents() {

      background = new javax.swing.JLayeredPane();
      testata = new javax.swing.JLabel();
      nuova = new javax.swing.JLabel();
      selectrack = new javax.swing.JLabel();
      track1 = new javax.swing.JLabel();
      track2 = new javax.swing.JLabel();
      track3 = new javax.swing.JLabel();
      track4 = new javax.swing.JLabel();
      track5 = new javax.swing.JLabel();
      track5.setVisible(false);
      track6 = new javax.swing.JLabel();
      track6.setVisible(false);
      track7 = new javax.swing.JLabel();
      track7.setVisible(false);
      track8 = new javax.swing.JLabel();
      track8.setVisible(false);
      track9 = new javax.swing.JLabel();
      track9.setVisible(false);
      track10 = new javax.swing.JLabel();
      track10.setVisible(false);
      behind = new javax.swing.JLabel();
      behind.setVisible(false);
      traccy = new javax.swing.JLabel();

      setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

      testata.setFont(new java.awt.Font("Tahoma", 0, 36));
      testata.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
      testata.setText("Thosa Racing");
      testata.setBounds(90, 0, 220, 44);
      background.add(testata, javax.swing.JLayeredPane.DEFAULT_LAYER);

      nuova.setFont(new java.awt.Font("Tahoma", 0, 18));
      nuova.setText("Nuova");
      nuova.addMouseListener(new java.awt.event.MouseAdapter() {
         public void mouseClicked(java.awt.event.MouseEvent evt) {
            nuovaMouseClicked(evt);
         }
      });
      nuova.setBounds(160, 140, 50, 22);
      background.add(nuova, javax.swing.JLayeredPane.DEFAULT_LAYER);

      selectrack.setVisible(false);
      selectrack.setFont(new java.awt.Font("Tahoma", 0, 24));
      selectrack.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
      selectrack.setText("Scegli il tracciato");
      selectrack.setBounds(110, 10, 177, 29);
      background.add(selectrack, javax.swing.JLayeredPane.DEFAULT_LAYER);

      track1.setVisible(false);
      track1.setFont(new java.awt.Font("Tahoma", 0, 24));
      track1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
      track1.setText("1");
      track1.addMouseListener(new java.awt.event.MouseAdapter() {
         public void mouseClicked(java.awt.event.MouseEvent evt) {
            track1MouseClicked(evt);
         }
      });
      track1.setBounds(50, 50, 13, 29);
      background.add(track1, javax.swing.JLayeredPane.DEFAULT_LAYER);

      track2.setVisible(false);
      track2.setFont(new java.awt.Font("Tahoma", 0, 24));
      track2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
      track2.setText("2");
      track2.setBounds(120, 50, 13, 29);
      background.add(track2, javax.swing.JLayeredPane.DEFAULT_LAYER);

      track3.setVisible(false);
      track3.setFont(new java.awt.Font("Tahoma", 0, 24));
      track3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
      track3.setText("3");
      track3.setBounds(190, 50, 13, 29);
      background.add(track3, javax.swing.JLayeredPane.DEFAULT_LAYER);

      track4.setVisible(false);
      track4.setFont(new java.awt.Font("Tahoma", 0, 24));
      track4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
      track4.setText("4");
      track4.setBounds(250, 50, 13, 29);
      background.add(track4, javax.swing.JLayeredPane.DEFAULT_LAYER);

      track5.setFont(new java.awt.Font("Tahoma", 0, 24));
      track5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
      track5.setText("5");
      track5.setBounds(320, 50, 13, 29);
      background.add(track5, javax.swing.JLayeredPane.DEFAULT_LAYER);

      track6.setFont(new java.awt.Font("Tahoma", 0, 24));
      track6.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
      track6.setText("6");
      track6.setBounds(50, 110, 13, 29);
      background.add(track6, javax.swing.JLayeredPane.DEFAULT_LAYER);

      track7.setFont(new java.awt.Font("Tahoma", 0, 24));
      track7.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
      track7.setText("7");
      track7.setBounds(120, 110, 13, 29);
      background.add(track7, javax.swing.JLayeredPane.DEFAULT_LAYER);

      track8.setFont(new java.awt.Font("Tahoma", 0, 24));
      track8.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
      track8.setText("8");
      track8.setBounds(190, 110, 13, 29);
      background.add(track8, javax.swing.JLayeredPane.DEFAULT_LAYER);

      track9.setFont(new java.awt.Font("Tahoma", 0, 24));
      track9.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
      track9.setText("9");
      track9.setBounds(250, 110, 13, 29);
      background.add(track9, javax.swing.JLayeredPane.DEFAULT_LAYER);

      track10.setFont(new java.awt.Font("Tahoma", 0, 24));
      track10.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
      track10.setText("10");
      track10.setBounds(320, 110, 26, 29);
      background.add(track10, javax.swing.JLayeredPane.DEFAULT_LAYER);

      behind.setFont(new java.awt.Font("Tahoma", 0, 18));
      behind.setText("Behind");
      behind.addMouseListener(new java.awt.event.MouseAdapter() {
         public void mouseClicked(java.awt.event.MouseEvent evt) {
            behindMouseClicked(evt);
         }
      });
      behind.setBounds(10, 260, 54, 22);
      background.add(behind, javax.swing.JLayeredPane.DEFAULT_LAYER);
      traccy.setBounds(0, 0, 400, 300);
      background.add(traccy, javax.swing.JLayeredPane.DEFAULT_LAYER);

      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
            getContentPane());
      getContentPane().setLayout(layout);
      layout.setHorizontalGroup(layout.createParallelGroup(
            javax.swing.GroupLayout.Alignment.LEADING).addComponent(background,
            javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE));
      layout.setVerticalGroup(layout.createParallelGroup(
            javax.swing.GroupLayout.Alignment.LEADING).addComponent(background,
            javax.swing.GroupLayout.Alignment.TRAILING,
            javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE));

      pack();
   }// </editor-fold>

   private void nuovaMouseClicked(java.awt.event.MouseEvent evt) {
      // Disabilita menu principale
      byte d = 1;
      disable(d);
   }

   private void behindMouseClicked(java.awt.event.MouseEvent evt) {
      // Torna al menu principale
      byte d = 2;
      disable(d);
   }

   private void track1MouseClicked(java.awt.event.MouseEvent evt) {
      trackMDisable(false);
      Track tracciato1 = new Track("src/track/m", 5);
      background.add(tracciato1);

   }

   // Metodo dis/able menu
   public void disable(byte i) {
      boolean d = false;
      switch (i) {
      case 1: {
         // Dal menu principale alla lista tracciati
         d = true;
         principalMDisable(d);
         trackMDisable(d);
      }
      case 2: {
         // Dalla lista tracciati al menu principale
         trackMDisable(d);
         principalMDisable(d);
      }
      }
   }

   public void principalMDisable(Boolean b) {
      if (b == true) {
         // se vero disable il menu principale
         testata.setVisible(false);
         nuova.setVisible(false);
      } else {
         // se falso able il menu principale
         testata.setVisible(true);
         nuova.setVisible(true);
      }
   }

   public void trackMDisable(Boolean b) {
      if (b == true) {
         // se vero able la lista dei tracciati
         selectrack.setVisible(true);
         track1.setVisible(true);
         track2.setVisible(true);
         track3.setVisible(true);
         track4.setVisible(true);
         track5.setVisible(true);
         track6.setVisible(true);
         track7.setVisible(true);
         track8.setVisible(true);
         track9.setVisible(true);
         track10.setVisible(true);
         behind.setVisible(true);
      } else {
         // se falso disable la lista dei tracciati
         selectrack.setVisible(false);
         track1.setVisible(false);
         track2.setVisible(false);
         track3.setVisible(false);
         track4.setVisible(false);
         track5.setVisible(false);
         track6.setVisible(false);
         track7.setVisible(false);
         track8.setVisible(false);
         track9.setVisible(false);
         track10.setVisible(false);
         behind.setVisible(false);
      }
   }

   // Variables declaration - do not modify
   private javax.swing.JLayeredPane background;
   private javax.swing.JLabel behind;
   private javax.swing.JLabel nuova;
   private javax.swing.JLabel selectrack;
   private javax.swing.JLabel testata;
   private javax.swing.JLabel traccy;
   private javax.swing.JLabel track1;
   private javax.swing.JLabel track10;
   private javax.swing.JLabel track2;
   private javax.swing.JLabel track3;
   private javax.swing.JLabel track4;
   private javax.swing.JLabel track5;
   private javax.swing.JLabel track6;
   private javax.swing.JLabel track7;
   private javax.swing.JLabel track8;
   private javax.swing.JLabel track9;
   // End of variables declaration}
}

我没有收到任何编译器错误,我认为错误出现在我添加图像的 Gui 类中,在 metod track1MouseClicked 中,我使用 NetBeans 7.01 的 JFrom 插件来创建框架和公司。

4

1 回答 1

3

我没有浏览你所有的代码,但这会导致问题:

public final Image load(String path, int n, int i) {
  Toolkit tool = Toolkit.getDefaultToolkit();
  String dot = ".jpg";

  path = this.path; // *****

因为您并没有在此处真正设置路径字段。你会想要反转那条线。

更重要的是,您将首先尝试在一个更小、更简单的程序中解决您的问题,这是一个测试程序,它除了读取一堆图像并显示它们之外什么都不做。然后,一旦你解决了这个问题,就使用你更大的程序中的代码。另一个好处是,如果您的小程序无法运行,您可以将其作为更合理大小的程序发布在这里,以便更多人愿意阅读、分析、测试和纠正。

此外,除非绝对必要,否则不要将 AWT 组件(例如ComponentSwing 组件)混合在一起——您的程序并非如此。

于 2011-11-06T12:24:37.090 回答