我一直在尝试几个小时,不同的事情并在各处寻找解决方案,但我无法让我的桌子出现在addScoreCardUpper()
中。我得到一个水平滚动条,但没有内容,只有2个像素的边框。
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
public class YahtzeeGUI extends JFrame{
/**
*
*/
private static final long serialVersionUID = 3255683022699487295L;
private JFrame frame;
private JPanel panel;
private JPanel dicePanel;
private JScrollPane scrollPane;
private JButton btnRoll;
private JButton[] btnDice = new JButton[5];
private JTable table;
private Yahtzee y = new Yahtzee();
public YahtzeeGUI(){
createWindow();
addButtonRoll();
addButtonDice();
addScoreCardUpper();
//addScoreCardLower();
frame.add(panel);
frame.setVisible(true);
}
public void createWindow(){
frame = new JFrame();
frame.setTitle("Yahtzee");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1000,700);
frame.setVisible(true);
panel = new JPanel(new BorderLayout());
dicePanel = new JPanel();
scrollPane = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
}
public void addButtonRoll(){
btnRoll = new JButton ("Roll the Dice");
btnRoll.addActionListener(new RollHandler());
dicePanel.add (btnRoll);
panel.add(dicePanel, BorderLayout.SOUTH);
}
public void addButtonDice(){
for (int i = 0; i < btnDice.length; i++){
btnDice[i] = new JButton(String.valueOf(y.dice[i].getFaceValue()));
btnDice[i].addActionListener(new HoldHandler());
dicePanel.add (btnDice[i]);
}
panel.add(dicePanel, BorderLayout.SOUTH);
}
public void addScoreCardUpper(){
String tableHeader[] = {"Upper Section", "How to score", "Score" };
String tableValues[][] = {
{"ONES", "Total of all Ones",""},
{"TWOS", "Total of all Twos",""},
{"THREES", "Total of all Threes",""},
{"FOURS", "Total of all Fours",""},
{"FIVES", "Total of all Fives",""},
{"SIXES", "Total of all Sixes",""},
{"TOTAL SCORE", "",""},
{"BONUS", "",""},
{"TOTAL + BONUS", "",""}
};
table = new JTable(tableValues, tableHeader);
table.setEnabled(false);
setColumnWidths();
scrollPane.add(table); // Here is the offender
panel.add(scrollPane, BorderLayout.EAST);
}
/*public void addScoreCardLower(){
String tableHeader[] = {"Lower S", "How to score", "Score" };
String tableValues[][] = {
{"ONES", "Total of all Ones",""},
{"TWOS", "Total of all Twos",""},
{"THREES", "Total of all Threes",""},
{"FOURS", "Total of all Fours",""},
{"FIVES", "Total of all Fives",""},
{"SIXES", "Total of all Sixes",""},
{"TOTAL SCORE", "",""},
{"BONUS", "",""},
{"TOTAL + BONUS", "",""}
};
table = new JTable(tableValues, tableHeader);
table.setEnabled(false);
setColumnWidths();
scoreSubPanel.add(table);
panel.add(scoreSubPanel, BorderLayout.WEST);
}*/
public void setColumnWidths(){
TableColumn a = table.getColumnModel().getColumn(0);
TableColumn b = table.getColumnModel().getColumn(1);
a.setPreferredWidth(100);
b.setPreferredWidth(100);
}
class RollHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
for(int i = 0; i < y.dice.length; i++){
if (y.dice[i].getHoldState() != true){
y.dice[i].roll();
btnDice[i].setText(String.valueOf(y.dice[i].getFaceValue()));
}
}
}
}
class HoldHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
for (int i = 0; i < btnDice.length; i++){
if (event.getSource()== btnDice[i]){ // Picks the pressed button after running through them all.
if (y.dice[i].getHoldState() == false){
y.dice[i].setHoldState(true);
} else if (y.dice[i].getHoldState() == true){
y.dice[i].setHoldState(false);
}
}
}
}
}
}
答案 0 :(得分:2)
这是你的问题:
scrollPane.add(table);
这不会将JTable添加到JScrollPane的视口中,这是您想要的视图,而是完全用JTable替换JScrollPane的视口,使JScrollPane完全不起作用。而是将表设置为JScrollPane的viewportview:
scrollPane.setViewportView(table);
执行此操作或将表传递给JScrollPane的构造函数,该构造函数几乎完全相同:
JScrollPane scrollPane = new JScrollPane(table,
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
JScrollPane API中对此进行了详细解释,您将需要查看重要的详细信息。
修改强>
在添加可能导致问题的所有组件之前,您的代码还会在JFrame上调用setVisible(true)
。你会想避免这样做。
答案 1 :(得分:2)
尝试任何一个
scrollPane = new JScrollPane(table);
或
scrollPane = new JScrollPane(table,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
或
scrollPane.getViewport().add(table);