私は現在、2 つの JPanel、つまり panel1 と panel 2 を備えた単純な Java GUI を持っています。panel1 は画像表示とラベリング表示に使用され、問題なく動作します。
パネル 2 は、JTable にデータを表示するために使用されます。私が抱えている問題は、スクロール ペインを使用してテーブルのセクションを表示するのではなく、垂直スクロール ペインを使用せずにテーブル内のすべてのデータを表示することです。これにより、GUI に厄介なギャップが生じており、すべてのテーブル データを一度に表示して修正したいと考えています。
テーブルを含むパネル、テーブル自体、およびスクロールペインで setPreferredSize を使用しようとしましたが、今ではまったく失われています! いつものように、どんな提案も大歓迎です。
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);
}
});
}
}