java - 扩展 JTable 并在 JFrame 中显示

标签 java swing jtable

我的代码仍在进行中,但我的问题是我扩展 JTable 的类将不会显示在 JFrame 中。这是我扩展 JTable 的类。其次,我想知道使我的 LotteryTable 成为一个全新类的设计选项是否是一个好主意。

起初,我的 LotteryDisplay 包含一个 JTable,然后通过方法 tableSetup() 对其进行了调整,该方法现在位于 LotteryTable 中

public class LotteryTable extends JTable
{
    private final String[] columnNames = {"Powerball Combos", "Odds of Winning", "Payout", "Number of Wins", "Win Frequency"};
    JTable table;

    public LotteryTable()
    {
        DefaultTableModel model = new DefaultTableModel(columnNames, 9);
        table = new JTable(model)
        {
            public Class getColumnClass(int column)
            {
                if(column == 0)
                    return ImageIcon.class;
                else
                    return String.class;
            }
        };

        tableSetup();
        setVisible(true);
    }

    public void tableSetup()
    {
        DefaultTableCellRenderer dtcr = new DefaultTableCellRenderer();
        dtcr.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
        table.setDefaultRenderer(String.class, dtcr);

        ImageIcon p = new ImageIcon("Icons/Powerball.png"); //Not important to my problem
        ImageIcon w1p = new ImageIcon("Icons/WhiteplusPowerball.png");
        ImageIcon w2P = new ImageIcon("Icons/2WhitePlusPowerball.png");
        ImageIcon w3P = new ImageIcon("Icons/3WhitePlusPowerball.png");
        ImageIcon w4P = new ImageIcon("Icons/4WhitePlusPowerball.png");
        ImageIcon w5P = new ImageIcon("Icons/5WhitePlusPowerball.png");
        ImageIcon w3 = new ImageIcon("Icons/3White.png");
        ImageIcon w4 = new ImageIcon("Icons/4White.png");
        ImageIcon w5 = new ImageIcon("Icons/5White.png");

        table.setValueAt(p, 0, 0);
        table.setValueAt(w1p, 1, 0);
        table.setValueAt(w2P, 2, 0);
        table.setValueAt(w3, 3, 0);
        table.setValueAt(w3P, 4, 0);
        table.setValueAt(w4, 5, 0);
        table.setValueAt(w4P, 6, 0);
        table.setValueAt(w5, 7, 0);
        table.setValueAt(w5P, 8, 0);

        table.setValueAt("1 in 55", 0, 1);
        table.setValueAt("1 in 111", 1, 1);
        table.setValueAt("1 in 706", 2, 1);
        table.setValueAt("1 in 360", 3, 1);
        table.setValueAt("1 in 12,245", 4, 1);
        table.setValueAt("1 in 19,088", 5, 1);
        table.setValueAt("1 in 648,976", 6,1);
        table.setValueAt("1 in 5,153,633", 7, 1);
        table.setValueAt("1 in 175,223,510", 8, 1);

        for(int i = 0; i < 10; i++)
        {
            table.setRowHeight(i, table.getRowHeight(i) + 10);
        }

        for(int i = 0; i < columnNames.length; i++)
        {
            TableColumn column = table.getColumnModel().getColumn(i);
            column.setPreferredWidth(column.getPreferredWidth() + 80);
        }
    }

这是带有试图显示我的 LotteryTable 的 JPanel 的主类。

public class LotteryDisplay
{
private JFrame display;
private JPanel panel;
private LotteryTable table;
private static JCheckBox powerPlay;

public LotteryDisplay()
{
    display = new JFrame("Powerball");
    panel = new JPanel();
    table = new LotteryTable();
    powerPlay = new JCheckBox("Power Play");
    panel.setPreferredSize(new Dimension(800, 300));

    panel.add(powerPlay);

    //JTableHeader header = table.getTableHeader(); //Not important to problem
    //panel.add(header);

    panel.add(table);

    display.getContentPane().add(panel, BorderLayout.CENTER);
    display.pack();
    display.setVisible(true);
}

最佳答案

LotteryTable 扩展了 JTable,但随后在其自身内部创建了另一个 JTable,它永远不会添加到能够显示它的任何组件中。

关于你的第二个问题……这个有争议。我看不到为您要添加的功能扩展 JTable 有什么特别的好处。这可以使用传递给 JTable

static 配置方法轻松实现

工作示例

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;

public class LotteryTable {

    public static void main(String[] args) {
        new LotteryTable();
    }

    public LotteryTable() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JTable table = new JTable();
                configureLotteryTable(table);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new JScrollPane(table));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public static final String[] COLUMN_NAMES = {"Powerball Combos", "Odds of Winning", "Payout", "Number of Wins", "Win Frequency"};

    public static void configureLotteryTable(JTable table) {
        DefaultTableModel model = new DefaultTableModel(COLUMN_NAMES, 0) {
            @Override
            public Class<?> getColumnClass(int columnIndex) {
                Class clazz = String.class;
                switch (columnIndex) {
                    case 0:
                        clazz = ImageIcon.class;
                        break;
                    default:
                        clazz = String.class;
                        break;
                }
                return clazz;
            }            
        };
        table.setModel(model);

        model.addRow(new Object[]{new ImageIcon(LotteryTable.class.getResource("/icons/Powerball.png")), "1 in 55"});
        model.addRow(new Object[]{new ImageIcon(LotteryTable.class.getResource("/icons/Powerball.png")), "1 in 111"});
        model.addRow(new Object[]{new ImageIcon(LotteryTable.class.getResource("/icons/Powerball.png")), "1 in 706"});
        model.addRow(new Object[]{new ImageIcon(LotteryTable.class.getResource("/icons/Powerball.png")), "1 in 360"});
        model.addRow(new Object[]{new ImageIcon(LotteryTable.class.getResource("/icons/Powerball.png")), "1 in 12,245"});
        model.addRow(new Object[]{new ImageIcon(LotteryTable.class.getResource("/icons/Powerball.png")), "1 in 19,088"});
        model.addRow(new Object[]{new ImageIcon(LotteryTable.class.getResource("/icons/Powerball.png")), "1 in 648,976"});
        model.addRow(new Object[]{new ImageIcon(LotteryTable.class.getResource("/icons/Powerball.png")), "1 in 5,153,633"});
        model.addRow(new Object[]{new ImageIcon(LotteryTable.class.getResource("/icons/Powerball.png")), "1 in 175,223,510"});
    }
}

我会花更多时间通读 How to use Tables复习一些概念;)

关于java - 扩展 JTable 并在 JFrame 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16704468/

相关文章:

java - 如何在hql上使用 'group by'?

java - HBase代码无法在Intellij中成功运行

java - 创建一个 List<A> 作为 GraphQLObjectType

java JMenuBar 不可见?为什么?

java - 单击鼠标重新绘制框架?

java - JTable 编辑器中的键绑定(bind)

java - JScrollPane 内的 JEditorPane 不显示滚动条

java - 为什么我的样板 Java 桌面应用程序 JFrame 在 main 方法中使用 EventQueue.invokeLater?

java - 未报告的异常SQLException;必须捕获或声明抛出错误

java - JTable - 复合编辑器焦点