我刚刚开始使用Java布局管理器,到目前为止,要让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整理一下,还有桌面问题。
答案 0 :(得分:2)
看看以下几行:
JPanel Table = new JPanel();
Table.setLayout(new GridLayout(8, 1));
您应该仔细阅读GridLayout上的文档,以便在代码中使用它,特别是如果您使用多个布局,就像在代码中一样。我建议您在尝试编写它们之前在草稿纸上设计组件及其布局。在此,我将Table
的布局参数更改为:
Table.setLayout(new GridLayout(0, 1));
它显示了所有数据条目。这是API在GridLayout
上所说的内容:
public GridLayout(int rows, int cols)
创建具有指定行数和列数的网格布局。布局中的所有组件都具有相同的大小。 行和列中的一个(但不是两个)可以为零,这意味着任意数量的对象可以放在一行或列中。