1

我正在尝试制作一个 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...
}
4

2 回答 2

3

代替:

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

我认为您将有更好的运气将您的面板构建为 JPanel 的扩展: 编辑:忘记提及此代码非常可重用,并且允许将其他对象传递到 JPanel...为您节省大量工作允许您为不同的场景重载构造函数。

//You can call new objects such as:
new MyPanel(data, otherComponent);



public class MyPanel extends JPanel{
    String data;

    public MyPanel(String someData, SomeOtherClass aClass){
    this.data = someData;
    setLayout(new GridBagLayout());
    GridBagConstraints constraints = new GridBagConstraints();
    JPanel panel = new JPanel(gridBag);

    //New addition
    constraints.gridx = 0;
    constraints.gridy = 0;
    //add components to the JPanel with your grid (constraints)
    add(new JScrollPane(someTable), constraints)

    //New addition
    constraints.gridx = 0;
    constraints.gridy = 1;
    //add components to the JPanel with your grid (constraints)
    add(tabbedPane , constraints);

    }

    public String getData(){
        return this.data;
    }
}

这遵循 MVC 模式(如果您打算设计更多摇摆,我建议您研究一下)。我知道它真的帮助了我!

于 2013-06-30T07:09:07.323 回答
2

你的问题

使用 GridBagLayout(和许多其他布局)时,您需要传递希望用于特定组件的约束,否则布局管理器将使用其默认值。

例如,你正在做...

gridBag.setConstraints(new JScrollPane(someTable), constraints);
add(new JScrollPane(someTable));

但是您要添加的组件不是您应用约束的组件。相反,做一些更像...

add(new JScrollPane(someTable), constraints);

一个(可能的)更好的解决方案

将您的 UI 分解为多个部分。每个部分都有自己独特的要求(我看到三个部分)。

我将 aGridLayout作为基本布局,您希望在其上添加三个附加面板,它们代表 UI 的三个部分。

当您构建一系列布局以产生最终结果时,这通常称为复合布局

于 2013-06-30T06:55:26.207 回答