gpt4 book ai didi

Java Swing : Maximising a JTable display

转载 作者:行者123 更新时间:2023-11-29 03:43:48 25 4
gpt4 key购买 nike

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

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

我已经尝试在包含表格的面板、表格本身以及滚动 Pane 上使用 setPrefferedSize,但现在我完全不知所措!一如既往地欢迎任何建议。

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

最佳答案

如果我理解正确,您希望 JTable 垂直拉伸(stretch)以占用顶部面板下方的所有可用空间。如果是这样,您需要更改布局管理器。现在您使用 4 行的 Gridlayout。 GridLayout 将您的框架分成 4 个等高的垂直 block 。第一个 block 包含顶部面板 - jp1,第二个 block 包含带有 JTableJScrollPane,两个底部 block 是空的,但占框架高度的一半。

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

关于Java Swing : Maximising a JTable display,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11930246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com