0

以下に貼り付けたコードを使用して、JTable が表示されてから 5 秒遅れて列幅のサイズを変更しました。ただし、「TestColumnResizer.java」を実行すると、エラー「java.lang.NoClassDefFoundError」が表示されます。メインが見つかりません。コードを変更して実行するにはどうすればよいですか?または、他のクラスで TestColumnResizer プログラムを呼び出すにはどうすればよいですか?

public class ColumnResizer {
    public static void adjustColumnPreferredWidths(JTable table) {
        // strategy - get max width for cells in column and
        // make that the preferred width
        TableColumnModel columnModel = table.getColumnModel();
        for (int col = 0; col < table.getColumnCount(); col++) {

            int maxwidth = 0;
            for (int row = 0; row < table.getRowCount(); row++) {
                TableCellRenderer rend = table.getCellRenderer(row, col);
                Object value = table.getValueAt(row, col);
                Component comp = rend.getTableCellRendererComponent(table,
                        value, false, false, row, col);
                maxwidth = Math.max(comp.getPreferredSize().width, maxwidth);
            } // for row
            TableColumn column = columnModel.getColumn(col);
            column.setPreferredWidth(maxwidth);
        } // for col
    }
}

// Testing automatic column sizing

public class TestColumnResizer {
    final static Object[][] TABLE_DATA = {
            { new Integer(1), "ONJava", "http://www.onjava.com/" },
            { new Integer(2), "Joshy's Site", "http://www.joshy.org/" },
            { new Integer(3), "Anime Weekend Atlanta",
                    "http://www.awa-con.com/" },
            { new Integer(4), "QTJ book",
                    "http://www.oreilly.com/catalog/quicktimejvaadn/" } };

    final static String[] COLUMN_NAMES = { "Count", "Name", "URL" };

    public static void main(String[] args) {
        // 142 mac l&f has a header bug - force metal for today
        try {

            UIManager.setLookAndFeel(UIManager
                    .getCrossPlatformLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        DefaultTableModel mod = new DefaultTableModel(TABLE_DATA, COLUMN_NAMES);
        JTable table = new JTable(mod);
        JScrollPane pane =

        new JScrollPane(table, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        JFrame frame = new JFrame("JTable Column Widths");
        frame.getContentPane().add(pane);
        frame.pack();
        frame.setVisible(true);

        try {
            Thread.sleep(5000);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // now get smart about col widths
        final JTable fTable = table;
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                ColumnResizer.adjustColumnPreferredWidths(fTable);
                fTable.revalidate();
            }
        });
    }
}
4

1 に答える 1

2

私が見る限り、クラスが定義されている方法だけで、コードの動作に問題はありません。

これがすべて 1 つのクラス ファイルに含まれていると、エラーが発生します。これを修正するには、クラス修飾子を次のように編集します。

import java.awt.Component;
import javax.swing.*;
import javax.swing.table.*;

//Testing automatic column sizing
public class TestColumnResizer {
    final static Object[][] TABLE_DATA = {
            { new Integer(1), "ONJava", "http://www.onjava.com/" },
            { new Integer(2), "Joshy's Site", "http://www.joshy.org/" },
            { new Integer(3), "Anime Weekend Atlanta",
                    "http://www.awa-con.com/" },
            { new Integer(4), "QTJ book",
                    "http://www.oreilly.com/catalog/quicktimejvaadn/" } };

    final static String[] COLUMN_NAMES = { "Count", "Name", "URL" };

    public static void main(String[] args) {
        // 142 mac l&f has a header bug - force metal for today
        try {

            UIManager.setLookAndFeel(UIManager
                    .getCrossPlatformLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        DefaultTableModel mod = new DefaultTableModel(TABLE_DATA, COLUMN_NAMES);
        JTable table = new JTable(mod);
        JScrollPane pane =

        new JScrollPane(table, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        JFrame frame = new JFrame("JTable Column Widths");
        frame.getContentPane().add(pane);
        frame.pack();
        frame.setVisible(true);

        try {
            Thread.sleep(5000);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // now get smart about col widths
        final JTable fTable = table;
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                ColumnResizer.adjustColumnPreferredWidths(fTable);
                fTable.revalidate();
            }
        });
    }
}


class ColumnResizer {
    public static void adjustColumnPreferredWidths(JTable table) {
        // strategy - get max width for cells in column and
        // make that the preferred width
        TableColumnModel columnModel = table.getColumnModel();
        for (int col = 0; col < table.getColumnCount(); col++) {

            int maxwidth = 0;
            for (int row = 0; row < table.getRowCount(); row++) {
                TableCellRenderer rend = table.getCellRenderer(row, col);
                Object value = table.getValueAt(row, col);
                Component comp = rend.getTableCellRendererComponent(table,
                        value, false, false, row, col);
                maxwidth = Math.max(comp.getPreferredSize().width, maxwidth);
            } // for row
            TableColumn column = columnModel.getColumn(col);
            column.setPreferredWidth(maxwidth);
        } // for col
    }
}

すべてが 1 つのクラス ファイルに含まれていない場合は、http ://www.tech-recipes.com/rx/826/java-exception-in-thread-main-javalangnoclassdeffounderror/ を試してください。基本的に、クラスパスに問題がある可能性があります。

コードは、指定したすべてのことを実行するように見えますが、心配する必要はありません。

于 2012-04-20T07:11:36.470 に答える