无法显示图像图标

时间:2016-03-29 15:20:08

标签: java swing

我必须使用飞行重量模式显示图像,我无法将图像打印到屏幕,这是演示问题的代码。

    public void draw(Graphics g, int tx, int ty, String name) {
       grem.paintIcon(null, g, tx, ty);
        g.drawString(name, tx, ty + H + 15 );
         ImageIcon grem = new ImageIcon("../images/grem.png");
    }

/// next class that calls the above class

 public void paint(Graphics g) {
        Folder folderIcon;
        String name;

        int j = 0;      //count number in row
        int row = Top;  //start in upper left
        int x = Left;

        //go through all the names and folders
        for (int i = 0; i< names.size(); i++) {
            name = (String)names.elementAt(i);
            if (name.equals(selectedName))
                folderIcon = fact.getFolder(true);
            else
                folderIcon = fact.getFolder(false);
            //have that folder draw itself at this spot
            folderIcon.paint(g);

            x = x + HSpace;    //change to next posn
            j++;
            if (j >= HCount) { //reset for next row
                j = 0;
                row += VSpace;
                x = Left;
            }
        }
    }

3 个答案:

答案 0 :(得分:1)

  1. 不要覆盖paint()。通过重写paintComponent()来完成自定义绘制。

  2. 不要在绘画方法中进行I / O操作。您无法控制Swing何时重新绘制组件,因此您不想在绘画方法中阅读图像。应该在班级的构造函数中读取图像。

  3. 重写getPreferredSize(...)方法以返回组件的大小,否则组件的大小将为(0,0),因此可能无需绘制任何内容(具体取决于所使用的布局管理器)

  4. 如果您需要更多帮助,请发布一个显示问题的正确SSCCE,因为我们不了解代码的使用情况,也没有时间花在猜测你可能做什么或不做什么。

  5. 阅读Custom Painting上Swing教程中的部分以获取更多信息。此外,您还可以使用JList以网格模式显示图标,而不是进行自定义绘制。查看教程链接的目录,查找How to Use Lists部分以获取更多信息。

答案 1 :(得分:0)

也许null就是问题:如果我没弄错的话,它应该是这样的

  class MyComponent extends JComponent {
  public void paint(Graphics g) {

    ImageIcon icon = new ImageIcon("a.png");
    int x = 0;
    int y = 100;
    icon.paintIcon(this, g, x, y);
  }

答案 2 :(得分:0)

公共类Gremlin扩展JFrame实现了ActionListener {

String names[] = {"Andy","Bill","Bob","Dan","Eugene","Frank","Gary","Harry","Ian","Jack",
        "Killlian","Liam","Mark","Nial","Obi","Phil","Richard","Stephan","Terry","Viny",};   // 20 names

public Icon img = new ImageIcon("grem1.jpg");
public JLabel grem = new JLabel(img);
JLabel bigLabel = new JLabel();
JLabel grem2 = new JLabel("New Gremlin");

public JPanel panel2 = new JPanel();
JPanel panel = new JPanel();

public Gremlin() {



    JButton button = new JButton("Add Gremlin");

    this.add(panel);



    panel.setLayout(new GridLayout(9,6));

    panel.add(panel2);
    panel2.add(button);
    for(int i = 0; i<20; i++){
        bigLabel.add(grem = new JLabel(names[i]), panel.add(grem = new JLabel(img)));
        panel.add(bigLabel);    

    }
    button.addActionListener(this);

    setSize(550,600);
    setLocationRelativeTo(null);
    setVisible(true);
}

public static void main(String[] args) {
    Gremlin frame = new Gremlin();
}

@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    if(e.getSource() != null ){
        System.out.println("add a Gremlin");

        panel.add(grem = new JLabel("NEW GREMLIN"), panel.add(grem = new JLabel(img))); 
        revalidate();
    }
}

}