Java Layout Manager を使い始めたばかりですが、これまでのところ、GUI を希望どおりに動作させるのはかなり困難でした。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class ex1 extends JFrame {
private javax.swing.JButton AddBtn,perviousBtn,NextBtn;
private javax.swing.JLabel LatLabel, LongLabel, EvlLabel;
private javax.swing.JTextField LatText,LongText,EvlText;
String columns[] = { "ID", "Name", "Age", "Gender" };
Object data[][] = { { "0", "Tom", new Integer(20), "Male" },
{ "1", "Tina", new Integer(18), "Female" },
{ "2", "Raj", new Integer(19), "Male" },
{ "3", "Tina", new Integer(18), "Female" },
{ "4", "Raj", new Integer(19), "Male" },
{ "5", "Tina", new Integer(18), "Female" },
{ "6", "Raj", new Integer(19), "Male" },
{ "7", "Tina", new Integer(18), "Female" },
{ "8", "Raj", new Integer(19), "Male" },
{ "9", "Tina", new Integer(18), "Female" },
{ "10", "Raj", new Integer(19), "Male" }
};
public void addComponentsToPane(final Container pane) {
JPanel compsToExperiment = new JPanel();
GridLayout experimentLayout = new GridLayout(3, 3);
compsToExperiment.setLayout(experimentLayout);
JPanel Table = new JPanel();
Table.setLayout(new GridLayout(8, 1));
JPanel nav = new JPanel();
nav.setLayout(new GridLayout(8, 3));
LatLabel = new javax.swing.JLabel("Latitude: ");
LongLabel = new javax.swing.JLabel("Longitude: ");
EvlLabel = new javax.swing.JLabel("Elevation: ");
LatText = new javax.swing.JTextField();
LongText = new javax.swing.JTextField();
EvlText = new javax.swing.JTextField();
AddBtn = new javax.swing.JButton("Add");
perviousBtn = new javax.swing.JButton("Next");
NextBtn = new javax.swing.JButton("pervious");
// perviousBtn.setPreferredSize(new Dimension(60, 60));
compsToExperiment.add(LatLabel);
compsToExperiment.add(LongLabel);
compsToExperiment.add(EvlLabel);
compsToExperiment.add(LatText);
compsToExperiment.add(LongText);
compsToExperiment.add(EvlText);
compsToExperiment.add(AddBtn);
// jButton1.addActionListener((ActionListener) this);
// this.add(jButton1);
JTable table = new JTable(data, columns);
Table.add(table.getTableHeader());
Table.add(table);
nav.add(NextBtn);
nav.add(perviousBtn);
pane.add(compsToExperiment, BorderLayout.NORTH);
pane.add(nav, BorderLayout.EAST);
pane.add(Table, BorderLayout.CENTER);
}
/**
* Create the GUI and show it. For thread safety, this method is invoked
* from the event dispatch thread.
*/
private static void createAndShowGUI() {
ex1 frame = new ex1();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addComponentsToPane(frame.getContentPane());
frame.setSize(500, 500);
//frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(
"javax.swing.plaf.metal.MetalLookAndFeel");
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
/* Turn off metal's use of bold fonts */
UIManager.put("swing.boldMetal", Boolean.FALSE);
// Schedule a job for the event dispatch thread:
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
また、すべてのエントリを表示するテーブルを取得する際に問題が発生しています。最初の 3 つのエントリのみが表示されます。GUI を整頓し、テーブルの問題を解決するのを手伝ってくれませんか。