2

Javaクラスを使用してJTableを作成するには、次のプログラムを使用します。オプションペインからwarnIcon、infoIconの画像を取得すると、正しく表示されます。ただし、システムから画像を追加すると、テーブルに表示されません。画像の代わりに空白が表示されます。そのテーブルのファイル(A.jpgなど)から画像を描画するにはどうすればよいですか?

package pointer;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.*;
import javax.swing.plaf.OptionPaneUI;
import javax.swing.table.*;
import sun.swing.ImageIconUIResource;

public class TableIcon1 extends JFrame  {
    private JTable table;
    private int pHeight = 60;
       public TableIcon1() {
           ImageIcon testIcon = new ImageIcon("A.jpg");
          // ImageIcon errorIcon = (ImageIcon) UIManager.getIcon("OptionPane.errorIcon");
           ImageIcon infoIcon = (ImageIcon) UIManager.getIcon("OptionPane.informationIcon");
           ImageIcon warnIcon = (ImageIcon) UIManager.getIcon("OptionPane.warningIcon");
           String[] columnNames = {"Picture", "Description"};
           Object[][] data = {{testIcon  , "About"}, {infoIcon, "Add"}, {warnIcon, "Copy"},};
           DefaultTableModel model = new DefaultTableModel(data, columnNames);
           table = new JTable(model) {
            @Override
            public Class getColumnClass(int column) {
                return getValueAt(2, column).getClass();
            }
          };
           table.setRowHeight(pHeight);
           table.setPreferredScrollableViewportSize(table.getPreferredSize());
           JScrollPane scrollPane = new JScrollPane(table);
           add(scrollPane, BorderLayout.CENTER);
         }

    public static void main(String[] args) {
        TableIcon1 frame = new TableIcon1();
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setLocation(150, 150);
        frame.pack();
        frame.setVisible(true);
    }
}
4

1 に答える 1

3
ImageIcon testIcon = new ImageIcon("A.jpg"); is road to nowhere

間違ったパスまたはnull値の例外を返さないアイコンは一般的です。そのためにテストする必要があります。

icons最善の方法は、名前を使用して新しいフォルダを作成しJava project、そこにコピーすることです。A.jpg Icon

その後、あなたは呼び出すことができるだけです

URL url = ClassLoader.getSystemClassLoader().getResource("icons/A.jpg");
ImageIcon testIcon = new ImageIcon(url);
于 2012-02-09T11:42:04.643 に答える