JTable之间的差距很大 [英] Large gap between JTables

查看:81
本文介绍了JTable之间的差距很大的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是提供我正在寻找的结果的方法,但不幸的是,当显示每页的表格之间存在很大差距。这样我每个标签只能在屏幕上放入其中两个(每个表只有两行)。

The below is on the way to providing the results I am looking for, but unfortunately when this displays there is a large gap between tables on each page. It is so that I can only fit two of them on the screen per tab (with just two rows per table).

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;
    }
}

我如何解决这个问题?谢谢

How would I go about fixing this? Thanks

编辑:编辑为每个请求添加完整代码...大部分内容与此处发生的事情无关。同样删除了setPreferredSize,但结果仍然相同。

Edited to add full code per request... most of it is irrelevant to what is going on here. Also removed setPreferredSize, but still same result.

编辑2:

仅供参考,抽奖中的空白点无关。 / p>

FYI, the blank spot in "Draws" is unrelated.

推荐答案

如果没有完整的例子,很难确定你所看到的是什么。鉴于应该避免 setPreferredSize() ,请注意 JTable 实现可滚动并可以计算它自己的首选大小。不幸的是,计算出的任意 Dimension 可能会导致问题。覆盖 getPreferredScrollableViewportSize() getRowHeight()的倍数.com / a / 14429388/230513>示例,可能是一个有用的替代方案。

Absent a complete example, it's hard to be sure what you're seeing. Given that one should avoid setPreferredSize(), note that JTable implements Scrollable and can calculate it's own preferred size. Unfortunately, the arbitrary Dimension calculated may be causing the problem. Overriding getPreferredScrollableViewportSize() to return a multiple of getRowHeight(), for example, may be a useful alternative.

这篇关于JTable之间的差距很大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆