JTable的大小问题(JTable sizing issue)

2019-08-31 09:47发布

我有JTable中的一个问题,我知道我的代码是有点难以遵循,它也是一个有点混乱周围,因为它是从一个相当大的程序来。 是的,我刚刚了解了Java命名约定在您不使用大写字母开头的变量。

final JFrame Menu = new JFrame("Crime Database 2013");
    Dimension screenSize0 = Menu.getToolkit().getScreenSize();
    Menu.setBounds(screenSize0.width / 4, screenSize0.height / 4,
            screenSize0.width / 2, screenSize0.height / 2);
    Menu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Menu.setVisible(true);
    JPanel options = new JPanel(new GridBagLayout());
    GridBagConstraints a = new GridBagConstraints();
    Menu.add(options);
    JButton show = new JButton("Show all records");
    a.gridx = 0;
    a.gridy = 1;
    options.add(show, a);

    final JFrame Show = new JFrame("Crime Database 2013 - Show Records");
        Dimension screenSize3 = Show.getToolkit().getScreenSize();
        Show.setBounds(screenSize3.width/3 - 250, screenSize3.height/7,
                screenSize3.width - 150, screenSize3.height-200);
        Show.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Show.setLayout(new GridBagLayout());
        GridBagConstraints g = new GridBagConstraints();
        final JPanel data = new JPanel(new FlowLayout());
        JPanel sortselect = new JPanel(new GridBagLayout());
        GridBagConstraints h = new GridBagConstraints();
        g.gridx = 0;
        g.gridy = 2;
        Show.add(sortselect, g);
        g.gridx = 0;
        g.gridy = 0;
        g.gridheight = 2;
        g.gridwidth = 5;
        Show.add(data, g);
        JLabel label = new JLabel("Sort options");
        JRadioButton none = new JRadioButton("No Sort",true);
        JLabel frname = new JLabel("By First Name");
        JRadioButton frnameup = new JRadioButton("First name - Alphabetical");
        JRadioButton frnamedn = new JRadioButton("First name - Reverse-Alphabetical");
        JLabel lsname = new JLabel("By Last Name");
        JRadioButton lsnameup = new JRadioButton("Last name - Alphabetical");
        JRadioButton lsnamedn = new JRadioButton("Last name - Reverse-Alphabetical");
        JLabel byage = new JLabel("By Age");
        JRadioButton ageup = new JRadioButton("Age - Increasing");
        JRadioButton agedn = new JRadioButton("Age - Decreasing");
        JLabel bycrime = new JLabel("By Crime");
        JRadioButton crimeup = new JRadioButton("Crime - Alhabetically");
        JRadioButton crimedn = new JRadioButton("Crime - Reverse-Alphabetical");
        JLabel year = new JLabel("By Year of release");
        JRadioButton yearup = new JRadioButton("Expected Year of Release - First");
        JRadioButton yeardn = new JRadioButton("Expected Year of Release - Last");
        ButtonGroup sortgroup = new ButtonGroup();
        sortgroup.add(none);
        sortgroup.add(frnameup);
        sortgroup.add(frnamedn);
        sortgroup.add(lsnameup);
        sortgroup.add(lsnamedn);
        sortgroup.add(ageup);
        sortgroup.add(agedn);
        sortgroup.add(crimeup);
        sortgroup.add(crimedn);
        sortgroup.add(yearup);
        sortgroup.add(yeardn);
        h.insets = new Insets(10,10,10,10);
        h.gridx = 0;
        h.gridy = 2;
        sortselect.add(frname, h);
        h.gridx = 0;
        h.gridy = 3;
        sortselect.add(frnameup, h);
        h.gridx = 0;
        h.gridy = 4;
        sortselect.add(frnamedn, h);
        h.gridx = 1;
        h.gridy = 2;
        sortselect.add(lsname, h);
        h.gridx = 1;
        h.gridy = 3;
        sortselect.add(lsnameup, h);
        h.gridx = 1;
        h.gridy = 4;
        sortselect.add(lsnamedn, h);
        h.gridx = 2;
        h.gridy = 2;
        sortselect.add(byage, h);
        h.gridx = 2;
        h.gridy = 3;
        sortselect.add(ageup, h);
        h.gridx = 2;
        h.gridy = 4;
        sortselect.add(agedn, h);
        h.gridx = 3;
        h.gridy = 2;
        sortselect.add(bycrime, h);
        h.gridx = 3;
        h.gridy = 3;
        sortselect.add(crimeup, h);
        h.gridx = 3;
        h.gridy = 4;
        sortselect.add(crimedn, h);
        h.gridx = 4;
        h.gridy = 2;
        sortselect.add(year, h);
        h.gridx = 4;
        h.gridy = 3;
        sortselect.add(yearup, h);
        h.gridx = 4;
        h.gridy = 4;
        sortselect.add(yeardn, h);
        h.gridwidth = 5;
        h.gridheight = 1;
        h.gridx = 0;
        h.gridy =0;
        sortselect.add(label, h);
        h.gridx = 0;
        h.gridy = 1;
        sortselect.add(none, h);
            show.addActionListener(
                    new ActionListener() {
                        public void actionPerformed(ActionEvent e1) {
                            Menu.setVisible(false);
                            int entries = 0;
                            BufferedReader lines = null;
                            try {
                                lines = new BufferedReader(new FileReader("L:\\workspace\\new java\\sources\\c-database.txt"));
                            } catch (FileNotFoundException e) {
                                e.printStackTrace();
                            }
                            try {
                                while (lines.readLine() != null) {
                                    entries++;
                                }
                            } catch (IOException e2) {
                                e2.printStackTrace();
                            }
                            BufferedReader crimeinfo = null;
                            try {
                                crimeinfo = new BufferedReader(new FileReader("L:\\workspace\\new java\\sources\\c-database.txt"));
                            } catch (FileNotFoundException e) {
                                e.printStackTrace();
                            }
                            String namelist[] = new String[entries];
                            String agelist[] = new String[entries] ;
                            String crimelist[] = new String[entries];
                            String release[] = new String[entries];
                            for (int i = 0; i < entries; i++) {
                                String full = null;
                                try {
                                    full = crimeinfo.readLine();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                                String split[] = full.split(",");
                                namelist[i] = split[0];
                                agelist[i] = split[1];
                                crimelist[i] = split[2];
                                release[i] = split[3];
                            }
                            String firstnamelist[] = new String[entries];
                            String lastnamelist[] = new String[entries];
                            for (int i = 0; i < entries; i++) {
                                String namesplit[] = namelist[i].split(" ");
                                firstnamelist[i] = namesplit[0];
                                lastnamelist[i] = namesplit[1];
                            }
                            final String[] headers = {"First Name",
                                                "Last Name",
                                                "Age",
                                                "Crime committed",
                                                "Expected Year of Release"
                            };
                            final String[][] crimedata = new String[entries][5];
                            for (int i = 0; i < entries; i++) {
                                crimedata[i][0] = firstnamelist[i];
                                crimedata[i][1] = lastnamelist[i];
                                crimedata[i][2] = agelist[i];
                                crimedata[i][3] = crimelist[i];
                                crimedata[i][4] = release[i];
                                };
                            for (int i = 0; i < entries; i++) {
                                for (int j = 0; j < 5; j++) {
                                    System.out.println(headers[j]);
                                    System.out.println(crimedata[i][j]);
                                }
                            }
                            final JTable crimetable = new JTable(crimedata, headers);
                            JScrollPane scrollpane = new JScrollPane(crimetable);
                            crimetable.setAutoCreateRowSorter(true);
                            data.add(scrollpane);
                            Show.setVisible(true);
                        }
                    }
                );

我只是把这个代码在这里到Eclipse,然后拿出所有的单选按钮,它八九不离十工作。 虽然我不知道为什么

Answer 1:

  1. JTable不能正确返回DimensionPreferredSize ,有三种方法

  2. table.setPreferredScrollableViewportSize(table.getPreferredSize()); 但要注意的有几个行和列小JTable中太

  3. 计算列和(部分)行为(部分)所需的尺寸也一样,然后传递该Dimension在形式table.setPreferredScrollableViewportSize(new Dimension(x, y));

  4. 覆盖getPreferredSizeJScrollPane

  5. 然后JFrame.pack(before JFrame.setVisible(true)) ,以计算在屏幕上所需的尺寸

  6. JPanelFlowLayout的API实现的,我建议改到BorderLayout的,然后在JScrollPane的中心区域可填充全部(可用)区,将可调整大小与JFrame的,无法调整的JComponent(连同其容器一起)通过奠定的FlowLayout

  7. 必须调用data.revalidate() data.repaint()Show.pack()作为最后的代码行,而不是(删除此代码线)Show.setVisible(真);

  8. 重命名ShowmyFrameshowmyButton



Answer 2:

究竟了么setPreferredScrollableViewportSize办? 难道它只是迫使表始终是多大? 什么是全包的事情吗?

getPreferredScrollableViewportSize()方法是在所定义的Scrollable界面,在所讨论的实现一个滚动精明客户端 。 而不是设置首选大小,可以覆盖getPreferredScrollableViewportSize()来更改默认值。 制作的倍数的高度getRowHeight()被示出在这里 。 更多关于首选大小可能会发现在这里 。 便利地, pack()方法“使此Window的大小,以适应首选大小和其子组件的布局。”



文章来源: JTable sizing issue