我正在尝试制作一个 JFrame 来显示我的 beta 测试人员要发送给我的数据(这部分我已经完成),以及一个用于图表的 JTabbedPane(供他们使用)。//我希望有这样的东西,忽略外观和感觉(和着色;我懒得一直给图片着色),看起来像这样:
. //我将不得不为 JComboBox 使用 CardLayout 来为新选择的模式显示不同的表格和不同的 JTabbedPane;这意味着两个表和两个 JTabbedPanes。
我试图为这个设置制作一个小(非常简化!)示例,一个只有一个(非常简单!)JTabbedPane 和一个小的 JTable 的 JFrame。如果我给我的 JPanel(包含两个组件)一个 BorderLayout,则该示例有效,但是一旦我给它一个 GridLayout(或一个 GridBagLayout //我最终将使用的布局),无论如何都只会显示最后一个组件我试试! 为什么是这样?
如果它可以帮助任何(它可能,考虑到我对此有多少新手),这里是示例代码:
JPanelLayoutTest.java
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.HeadlessException;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
public class JPanelLayoutTest extends JFrame {
private JPanel aPanel;
private JTabbedPane tabbedPane;
private JTable someTable;
public JPanelLayoutTest(String title) throws HeadlessException {
super(title);
aPanel = setupPanel();
}
private JPanel setupPanel()
{
GridBagLayout gridBag = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
//making the panel have two columns and one row
JPanel panel = new JPanel(gridBag);
//add someTable to top
someTable = new JTable(new SampleTableModel());
/*JPanel somePanel = new JPanel();
somePanel.add(new JScrollPane(someTable));*/
constraints.gridx = 0;
constraints.gridy = 0;
constraints.gridheight = 1;
constraints.gridwidth = 1;
gridBag.setConstraints(new JScrollPane(someTable), constraints);
add(new JScrollPane(someTable));
//add tabbedPane to bottom
tabbedPane = new JTabbedPane();
tabbedPane.addTab("some component", new JLabel("some text"));
/*JPanel someOtherPanel = new JPanel();
someOtherPanel.add(tabbedPane);*/
constraints.gridy = 1;
gridBag.setConstraints(tabbedPane, constraints);
add(tabbedPane);
//add(tabbedPane);
return panel;
}
public void turnOnWindow()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
//setSize(400,200);
pack();
}
public static void main(String[] args) {
JPanelLayoutTest frame = new JPanelLayoutTest("JPanel Layout Test");
frame.turnOnWindow();
}
}
//Pardon the indentation; I wish this forum had support for the [code][/code] tag
SampleTableModel.java
import javax.swing.table.AbstractTableModel;
public class SampleTableModel extends AbstractTableModel {
//declaring the static arrays that will be the data for the table
private final String[] columnNames = {"Account", "Full Name", "Balance"};
private final int[] acctNumbers = {1000443749, 190238420, 928355};
private final String[] fullNames = {"Mike Warren", "Jack Smith", "Sarah Brown"};
private final double[] acctBalances = {193.38, 289.28, 21034.29};
public SampleTableModel()
{
// I do nothing in here; there is no reason to.
}
@Override
public int getColumnCount() { return columnNames.length; }
@Override
public int getRowCount() { return acctNumbers.length; }
@Override
public Object getValueAt(int row, int column) {
switch (column)
{
case 0:
return new Integer(acctNumbers[row]);
case 1:
return fullNames[row];
case 2:
return new Double(acctBalances[row]);
default:
return null; //there should be no way the code would ever reach here
}
}
@Override
public String getColumnName(int index) { return columnNames[index]; }
//Again, forgive my horrible indentation near the end here...
}