私のコードはまだかなり進行中ですが、私の問題は、拡張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);
}