1

我目前有一个带有 2 个 Jpanel 的简单 Java GUI,即 panel1 和 panel 2。panel1 用于图像显示和标签显示,它可以正常工作。

面板 2 用于在 JTable 中显示数据。我遇到的问题是在没有垂直滚动窗格的情况下显示表格中的所有数据,而不是带有滚动窗格的表格部分。这在 GUI 中造成了一个令人讨厌的差距,我希望通过一次显示所有表数据来纠正它。

我尝试在包含表格的面板、表格本身以及滚动窗格上使用 setPreferedSize,现在我完全不知所措!任何建议都将一如既往地受到赞赏。

import javax.swing.*;
import java.awt.*;
import javax.imageio.*;
import java.io.*;
import java.awt.image.BufferedImage;
import java.util.*;
import javax.swing.table.DefaultTableCellRenderer;

public class GUI2 extends JFrame {

    public int PADDING = 10;
    public GUI2() {

        initUI();
    }

    public final void initUI() {

        // Menu Bar and Menu Items setup
        JMenuBar menuBar;
        JMenu menu;
        JMenuItem menuItem;

        menuBar = new JMenuBar();
        menu = new JMenu("Menu");
        menuBar.add(menu);

        JMenuItem menuNew = new JMenuItem("New");
        menu.add(menuNew);
        JMenuItem menuOpen = new JMenuItem("Open");
        menu.add(menuOpen);
        JMenuItem menuSave = new JMenuItem("Save");
        menu.add(menuSave);
        JMenuItem menuExit = new JMenuItem("Exit");
        menu.add(menuExit);

        setLayout(new GridLayout(4,1));
        setJMenuBar(menuBar);


        JPanel jp1 = new JPanel(new BorderLayout());
        jp1.setBorder(BorderFactory.createEmptyBorder(PADDING, PADDING, PADDING, PADDING));
        jp1.setBorder(BorderFactory.createLineBorder(Color.black));
        jp1.setPreferredSize(new Dimension(800, 300));

        // Create subpanels
        JPanel sp1 = new JPanel();
        JPanel sp2 = new JPanel(new GridLayout(8, 2));
        sp1.setBorder(BorderFactory.createLineBorder(Color.black));

        // Set up sub panel 1
        BufferedImage myPicture = null;
        try{
            myPicture = ImageIO.read(new File("bat.jpg"));
        }catch(IOException e){
            System.out.println("Image not Loaded!");
            e.printStackTrace();
        }

        JLabel picLabel             = new JLabel(new ImageIcon( myPicture ));
        jp1.add(picLabel);


        // For sub panel 2
        JLabel label_location       = new JLabel("Location: ");
        JLabel label_datetime       = new JLabel("Date & Time: ");
        JLabel label_measurementNum = new JLabel("Measurement Number: ");
        JLabel label_vehicleType    = new JLabel("Vehicle Type: ");
        JLabel label_temperature    = new JLabel("Temperature: ");
        JLabel label_weather        = new JLabel("Weather Conditions: ");
        JLabel label_testPersons    = new JLabel("Test Persons: ");
        JLabel label_testType       = new JLabel("Test Type: ");

        String timestamp            = " ";
        Date date                   = new Date();
        timestamp                   = date.toString();


        JTextField  location        = new JTextField();
        JLabel      datetime        = new JLabel(timestamp);
        JLabel      measurementNum  = new JLabel("AZUWADA87623Q");          
        JLabel      vehicleType     = new JLabel("Batmobile");                   
        JTextField  temperature     = new JTextField(); 
        JTextField  weather         = new JTextField();
        JTextField  testPersons     = new JTextField();
        JTextField  testType        = new JTextField();



        sp1.add(picLabel);
        sp2.add(label_location);
        sp2.add(location);
        sp2.add(label_datetime);
        sp2.add(datetime);
        sp2.add(label_measurementNum);
        sp2.add(measurementNum);
        sp2.add(label_vehicleType);
        sp2.add(vehicleType);
        sp2.add(label_temperature);
        sp2.add(temperature);
        sp2.add(label_weather);
        sp2.add(weather);
        sp2.add(label_testPersons);
        sp2.add(testPersons);
        sp2.add(label_testType);
        sp2.add(testType);



        label_location.setHorizontalAlignment(SwingConstants.LEFT);                         



        jp1.add(sp1, BorderLayout.WEST);
        jp1.add(sp2, BorderLayout.EAST);

        add(jp1);



        JPanel jp2 = new JPanel();
        jp2.setPreferredSize(new Dimension(900, 900));


        String[] columnNames = {"", "DAW Referernce Scale", " WIM Vehicle 1"}; 
        Object[][] data = { {"Vehicle Summary", new Integer(100), new Integer(200)},  // Header 1: Green
                            {"Axle Numbers", new Integer(100), new Integer(200)},
                            {"Axle Code", new Integer(100), new Integer(200)},
                            {"Axle Distances (cm)", new Integer(100), new Integer(200)},
                            {"Vehicle Speed (km/h)", new Integer(100), new Integer(200)},
                            {"Gross Weight", new Integer(100), new Integer(200)}, 
                            {"Axle Weight 1", new Integer(100), new Integer(200)},
                            {"Axle Weight 2", new Integer(100), new Integer(200)},

                            // Measurements for Axle 1
                            // Weights
                            {"Axle 1", new Integer(100), new Integer(200)},   // Header 1: Green
                            {"Weights (kg)", " ", " "},
                            {"Axle Weights AW1", new Integer(100), new Integer(200)},
                            {"Wheel Weights WW1R", new Integer(100), new Integer(200)},
                            {"Wheel Weights WW1L", new Integer(100), new Integer(200)},
                            // Dimensions
                            {"Dimensions (cm or cm2)", new Integer(100), new Integer(200)},
                            {"Axle Width AW1", new Integer(100), new Integer(200), new Integer(200)}, 
                            {"Tire Footprints 1R Length", new Integer(100), new Integer(200)},
                            {"Tire Footprints 1R Width", new Integer(100), new Integer(200)},
                            {"Tire Footprints 1R FootPrint", new Integer(100), new Integer(200)},
                            {"Tire Footprints 1L Length", new Integer(100), new Integer(200)},
                            {"Tire Footprints 1L Width", new Integer(100), new Integer(200)},
                            {"Tire Footprints 1L FootPrint", new Integer(100), new Integer(200)},
                            // Speed
                            {"Speed", " ", " "},
                            {"Vehicle Speed", new Integer(100), new Integer(200)},
                            {"Axle Speed", new Integer(100), new Integer(200)},
                            {"Tyre Speed 1R", new Integer(100), new Integer(200)},
                            {"Tyre Speed 1L", new Integer(100), new Integer(200)},

                            // Measurements for Axle 2
                            // Weights
                            {"Axle 2", " ", " "},   // Header 1: Green
                            {"Weights (kg)", " ", " "},
                            {"Axle Weights AW2", new Integer(100), new Integer(200)},
                            {"Wheel Weights WW2R", new Integer(100), new Integer(200)},
                            {"Wheel Weights WW2L", new Integer(100), new Integer(200)},
                            // Dimensions
                            {"Dimensions (cm or cm2)", " ", " "},
                            {"Axle Width AW2", new Integer(100), new Integer(200)}, 
                            {"Tire Footprints 2R Length", new Integer(100), new Integer(200)},
                            {"Tire Footprints 2R Width", new Integer(100), new Integer(200)},
                            {"Tire Footprints 2R FootPrint", new Integer(100), new Integer(200)},
                            {"Tire Footprints 2L Length", new Integer(100), new Integer(200)},
                            {"Tire Footprints 2L Width", new Integer(100), new Integer(200)},
                            {"Tire Footprints 2L FootPrint", new Integer(100), new Integer(200)},
                            // Speed
                            {"Speed", " ", " "},
                            {"Vehicle Speed", new Integer(100), new Integer(200)},
                            {"Axle Speed", new Integer(100), new Integer(200)},
                            {"Tyre Speed 2R", new Integer(100), new Integer(200)},
                            {"Tyre Speed 2L", new Integer(100), new Integer(200)},};


        final JTable table = new JTable(data, columnNames); 

        table.getColumnModel().getColumn(0).setCellRenderer(new CustomRenderer());
        table.setPreferredScrollableViewportSize(new Dimension(500, 70)); 
        table.setFillsViewportHeight(true); 

        JScrollPane scrollPane = new JScrollPane(table); 

        jp2.add(scrollPane);
        add(scrollPane);


        setSize(400,300);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

    }

            //Custom Renderer - does the default rendering except if told the row should be a different color
    public static class CustomRenderer extends DefaultTableCellRenderer{

        public CustomRenderer(){
            super();
            //Customize the rendering however you want
            setBackground(UIManager.getColor("TableHeader.background"));
        }
    }


    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {

            public void run() {

                try {
                    UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
                } catch (Exception e) {
                    e.printStackTrace();
                }

                GUI2 ex = new GUI2();
                ex.setVisible(true);
            }
        });
    }
}
4

3 回答 3

2

如果我理解正确,您想让 JTable 垂直拉伸以占用顶部面板下方的所有可用空间。如果是这样,您需要更改布局管理器。现在您使用 4 行的 Gridlayout。GridLayout 将您的框架拆分为 4 个高度相等的垂直块。第一个块包含顶部面板 - jp1,第二个块包含JScrollPanewithJTable和两个底部块是空的,但占帧高度的一半。

您可以尝试使用 BoxLayout、BorderLayout 或 GridBagLayout。

于 2012-08-13T09:06:16.553 回答
1

JTable本身不提供滚动功能。这通常使用JScrollPane. 尝试创建JScrollPane,把它放到你的面板上并JTable放入JScrollPane。看看滚动策略。您可以使用构造函数或JScrollPane. 例如,滚动条可能始终出现或仅在需要时自动出现等。

于 2012-08-13T08:16:53.403 回答
1

它看起来很不错,有一些变化。

  • 用于setLayout(new GridLayout(0, 1))仅添加所需数量的行。
  • 不要setPreferredSize()打开jp1and jp2; 组件知道它们有多大。
  • pack()就在窗前呼唤setVisible(true)
于 2012-08-13T13:13:10.160 回答