java – JTable之间的差距很大
作者:互联网
以下是提供我正在寻找的结果的方法,但不幸的是,当显示每页的表格之间存在很大差距.这样我每个标签只能在屏幕上放入其中两个(每个表只有两行).
private void display() {
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new JLabel("Test"));
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
class ResultsPanel extends JPanel implements ActionListener {
private ArrayList<Venue> venueList;
private Timer timer;
QueueItem[] queue;
Reporter reporter;
private JPanel resultsPane1;
private JPanel resultsPane2;
private JTable[] table1;
private JTable[] table2;
private int totalItems;
public String[] page1ColumnNames = {
"Name",
"Wins",
"Losses",
"Draws",
"Win %",
"Avg Turns"};
public String[] page2ColumnNames = {
"Name",
"H%Total",
"H%inRange",
"KO/F rcvd",
"KO/F Infl",
"HChances",
"HitsTotal",
"Wounds Inflicted",
"Wounds Received"};
public String[][] page1Data = new String[2][6];
public String[][] page2Data = new String[2][9];
public void initResults() {
timer = new Timer(500, this);
timer.start();
}
@Override
public void actionPerformed(ActionEvent e) {
Object src = e.getSource();
try {
Thread.sleep(1);
} catch (InterruptedException d) {
System.err.println("Caught : InterruptedException" + d.getMessage());
}
repaint();
}
public ResultsPanel(QueueItem[] queueItems) {
super();
totalItems = 0;
queue = queueItems;
for (QueueItem a : queue) {
if (a != null) {
totalItems++;
}
}
JTabbedPane tabbedPane = new JTabbedPane();
resultsPane1 = new JPanel();
resultsPane2 = new JPanel();
resultsPane1.setLayout(new BoxLayout(resultsPane1, BoxLayout.PAGE_AXIS));
resultsPane2.setLayout(new BoxLayout(resultsPane2, BoxLayout.PAGE_AXIS));
table1 = new JTable[totalItems];
table2 = new JTable[totalItems];
for (int b = 0; b < totalItems; b++) {
reporter = queue[b].reporter;
generateData();
table1[b] = new JTable(page1Data, page1ColumnNames);
table2[b] = new JTable(page2Data, page2ColumnNames);
resultsPane1.add(new JScrollPane(table1[b]));
resultsPane2.add(new JScrollPane(table2[b]));
}
JScrollPane scrollPane1 = new JScrollPane(resultsPane1);
JScrollPane scrollPane2 = new JScrollPane(resultsPane2);
tabbedPane.addTab("Win/Loss", scrollPane1);
tabbedPane.addTab("Overall", scrollPane2);
add(tabbedPane);
}
public void generateData() {
double temp = 0;
double avgTurns;
double total;
for (Integer a : reporter.numberOfTurns) {
temp = temp + a;
}
avgTurns = 1.0 * temp / reporter.numberOfTurns.size();
page1Data[0][0] = reporter.glad1.name;
page1Data[0][1] = "" + reporter.gladTotalWins1;
page1Data[0][2] = "" + reporter.gladTotalLosses1;
page1Data[0][3] = "" + reporter.gladTotalDraws1;
temp = (int) ((reporter.gladTotalWins1 * 100.0) / (reporter.gladTotalWins1 + reporter.gladTotalLosses1 + reporter.gladTotalDraws1));
page1Data[0][4] = "" + temp + "%";
page1Data[0][5] = "" + avgTurns;
page1Data[1][0] = reporter.glad2.name;
page1Data[1][1] = "" + reporter.gladTotalWins2;
page1Data[1][2] = "" + reporter.gladTotalLosses2;
page1Data[0][3] = "" + reporter.gladTotalDraws2;
temp = (int) ((reporter.gladTotalWins2 * 100.0) / (reporter.gladTotalWins2 + reporter.gladTotalLosses2 + reporter.gladTotalDraws2));
page1Data[1][4] = "" + temp + "%";
page1Data[1][5] = "" + avgTurns;
page2Data[0][0] = reporter.glad1.name;
total = 0;
for (double a : reporter.hitChancePercentOut1) {
total = total + a;
}
temp = 1.0 * total / reporter.hitChancePercentOut1.size();
page2Data[0][1] = "" + temp;
total = 0;
for (double a : reporter.hitChancePercentIn1) {
total = total + a;
}
temp = 1.0 * total / reporter.hitChancePercentIn1.size();
page2Data[0][2] = "" + temp;
page2Data[1][0] = reporter.glad2.name;
total = 0;
for (double a : reporter.hitChancePercentOut2) {
total = total + a;
}
temp = 1.0 * total / reporter.hitChancePercentOut2.size();
page2Data[1][1] = "" + temp;
total = 0;
for (double a : reporter.hitChancePercentIn2) {
total = total + a;
}
temp = 1.0 * total / reporter.hitChancePercentIn2.size();
page2Data[1][2] = "" + temp;
int deadCount2 = 0;
int KOCount2 = 0;
for (String a : reporter.endStatus2) {
if (a.equals("DEAD")) {
deadCount2++;
} else if (a.equals("KO")) {
KOCount2++;
}
}
int deadCount1 = 0;
int KOCount1 = 0;
for (String a : reporter.endStatus1) {
if (a.equals("DEAD")) {
deadCount1++;
} else if (a.equals("KO")) {
KOCount1++;
}
}
page2Data[0][3] = "" + KOCount1 + "/" + deadCount1;
page2Data[1][3] = "" + KOCount2 + "/" + deadCount2;
page2Data[0][4] = "" + KOCount2 + "/" + deadCount2;
page2Data[1][4] = "" + KOCount1 + "/" + deadCount1;
total = 0;
for (Integer a : reporter.hitChances1) {
total = total + a;
}
page2Data[0][5] = "" + total;
total = 0;
for (Integer a : reporter.hitTotals1) {
total = total + a;
}
page2Data[0][6] = "" + total;
total = 0;
for (Integer a : reporter.woundsInflicted1) {
total = total + a;
}
page2Data[0][7] = "" + total;
total = 0;
for (Integer a : reporter.woundsReceived1) {
total = total + a;
}
page2Data[0][8] = "" + total;
total = 0;
total = 0;
for (Integer a : reporter.hitChances2) {
total = total + a;
}
page2Data[1][5] = "" + total;
total = 0;
for (Integer a : reporter.hitTotals2) {
total = total + a;
}
page2Data[1][6] = "" + total;
total = 0;
for (Integer a : reporter.woundsInflicted2) {
total = total + a;
}
page2Data[1][7] = "" + total;
total = 0;
for (Integer a : reporter.woundsReceived2) {
total = total + a;
}
page2Data[1][8] = "" + total;
total = 0;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
}
}
我该如何解决这个问题?谢谢
编辑:编辑为每个请求添加完整代码…其中大部分与此处发生的事情无关.还删除了setPreferredSize,但结果仍然相同.
编辑2:
仅供参考,“抽奖”中的空白点无关紧要.
解决方法:
如果没有完整的例子,很难确定你所看到的是什么.鉴于应该避免使用setPreferredSize()
,请注意JTable实现Scrollable并且可以计算它自己的首选大小.不幸的是,计算的任意维度可能导致问题.对于example,重写getPreferredScrollableViewportSize()以返回getRowHeight()的倍数,可能是一个有用的替代方法.
标签:jtabbedpane,java,swing,jtable,preferredsize 来源: https://codeday.me/bug/20190928/1825877.html