gpt4 book ai didi

java - 表格位于框架底部

转载 作者:行者123 更新时间:2023-12-02 05:33:47 26 4
gpt4 key购买 nike

我编写了一个简单的swing gui。但是,我的问题是我的表格位于页面底部。我希望按钮下方有一些空间,并且底部还有更多空间。这是一个简短的程序,我正在做的事情:

public class minimumExample extends JFrame {

private JTabbedPane tabbedPane;
private FilteredTabPanel filteredTabPanel;

public void createTabBar() {

tabbedPane = new JTabbedPane(JTabbedPane.TOP);

filteredTabPanel = new FilteredTabPanel();
tabbedPane.addTab("Test", filteredTabPanel.createLayout());

add(tabbedPane);
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
}

private void makeLayout() {

setTitle("Test App");
setLayout(new BorderLayout());
setPreferredSize(new Dimension(1000, 500));
createTabBar();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);

}

public void start() {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
makeLayout();
}
});
}

public static void main(String[] args) throws IOException {
minimumExample ex = new minimumExample();
ex.start();
}

public class FilteredTabPanel extends JPanel {

private JPanel selectionArea;
private JLabel lCity;
private JComboBox cityBox;
private JTable filterTable;
String[] columnNames = {"Cities"};
String[][] data = {
{"NY"}, {"NY"}, {"NY"}, {"NY"}, {"LA"}, {"LA"},{"Columbia"},{"DC"},{"DC"},{"DC"},{"DC"},{"DC"},{"DC"}
};

private JScrollPane scrollPane;

public JPanel createLayout() {
JPanel panel = new JPanel(new GridLayout(0, 1));
//add panels to the layout
panel.add(addButtons());
panel.add(showTable());

repaint();
revalidate();

return panel;
}

public JPanel addButtons(){

selectionArea = new JPanel(new FlowLayout(FlowLayout.LEFT));

lCity = new JLabel("City");

String[] fillings = {"NY", "LA", "Columbia", "DC"};
cityBox = new JComboBox(fillings);

cityBox.addActionListener(new ActionListener() {

private String cityFilter;

@Override
public void actionPerformed(ActionEvent arg0) {
//2. get data
cityFilter = cityBox.getSelectedItem().toString();
}
});

selectionArea.add(lCity);
selectionArea.add(cityBox);

selectionArea.repaint();

return selectionArea;
}

private JScrollPane showTable() {

filterTable =new JTable(data, columnNames);
filterTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

scrollPane = new JScrollPane(filterTable);

scrollPane.repaint();
scrollPane.validate();

return scrollPane;
}
}
}

页面如下所示:

enter image description here

有什么建议可以解决这个问题吗?

最佳答案

您想要做的是嵌套简单的布局管理器创建您的布局。我认为官方的 Swing 教程引入这些对程序员来说是一种伤害首先是简单的布局管理器。我建议避免它们并且投入一些时间给更有权力的管理者。FlowLayoutBorderLayoutGridlayout 真的非常好简单的管理者,但他们做不了多少事情。

我建议使用这两个管理器:

  • MigLayout

  • GroupLayout

(JGoodies 的 FormLayout 也是一个不错的选择。)

这些管理者不仅更有权力,而且还能应对具有其他人失败的更高级的要求。 (解决独立性、遵守操作系统设计原则等)

在展示我的两个代码示例之前,我想说几句话指向你的代码。您不必要地调用 repaint()revalidate() 方法。

setPreferredSize(new Dimension(1000, 500));

以像素为单位指定尺寸是不可移植的。你最好依靠pack() 方法。

setLayout(new BorderLayout());

JFrame 的 默认布局管理器(更准确地说是其内容 Pane 的)是BorderLayout。因此,不需要此行。

以下两个示例是使用 MigLayoutGroupLayout 的解决方案。

MigLayout解决方案

MigLayout是第三方布局管理器,所以需要下载并将额外的 jar 添加到您的项目中。

package com.zetcode;

import java.awt.EventQueue;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import net.miginfocom.swing.MigLayout;

public class MigLayoutEx extends JFrame {

private JComboBox cityBox;
private JTable filterTable;

public MigLayoutEx() {

initUI();
}

private void initUI() {

JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
add(tabbedPane);

filterTable = createTable();
cityBox = createCityBox();

JPanel pnl = new JPanel(new MigLayout(""));
pnl.add(new JLabel("City"), "split 2");
pnl.add(cityBox, "wrap");

pnl.add(new JScrollPane(filterTable));

tabbedPane.addTab("Test", pnl);
pack();

setTitle("MigLayout example");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

private JTable createTable() {

String[] columnNames = {"Cities"};
String[][] data = {
{"NY"}, {"NY"}, {"NY"}, {"NY"}, {"LA"}, {"LA"}, {"Columbia"},
{"DC"}, {"DC"}, {"DC"}, {"DC"}, {"DC"}, {"DC"}
};

JTable table = new JTable(data, columnNames);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

return table;
}

private JComboBox createCityBox() {

String[] fillings = {"NY", "LA", "Columbia", "DC"};
JComboBox box = new JComboBox(fillings);

return box;
}

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
MigLayoutEx ex = new MigLayoutEx();
ex.setVisible(true);
}
});
}
}

MigLayout example

GroupLayout解决方案

GroupLayout是一个内置的布局管理器,无需添加额外的jar。

package com.zetcode;

import java.awt.EventQueue;
import javax.swing.GroupLayout;
import static javax.swing.GroupLayout.Alignment.BASELINE;
import javax.swing.JComboBox;
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 GroupLayoutEx extends JFrame {

private JComboBox cityBox;
private JTable filterTable;

public GroupLayoutEx() {

initUI();
}

private void initUI() {

JPanel pnl = new JPanel();
GroupLayout gl = new GroupLayout(pnl);
pnl.setLayout(gl);

JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
add(tabbedPane);

JLabel cityLbl = new JLabel("City");
filterTable = createTable();
JScrollPane spane = new JScrollPane(filterTable);
cityBox = createCityBox();

gl.setAutoCreateGaps(true);
gl.setAutoCreateContainerGaps(true);

gl.setHorizontalGroup(gl.createParallelGroup()
.addGroup(gl.createSequentialGroup()
.addComponent(cityLbl)
.addComponent(cityBox))
.addComponent(spane)
);

gl.setVerticalGroup(gl.createSequentialGroup()
.addGroup(gl.createParallelGroup(BASELINE)
.addComponent(cityLbl)
.addComponent(cityBox))
.addComponent(spane)
);

tabbedPane.addTab("Test", pnl);
pack();

setTitle("GroupLayout example");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

private JTable createTable() {

String[] columnNames = {"Cities"};
String[][] data = {
{"NY"}, {"NY"}, {"NY"}, {"NY"}, {"LA"}, {"LA"}, {"Columbia"},
{"DC"}, {"DC"}, {"DC"}, {"DC"}, {"DC"}, {"DC"}
};

JTable table = new JTable(data, columnNames);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

return table;
}

private JComboBox createCityBox() {

String[] fillings = {"NY", "LA", "Columbia", "DC"};
JComboBox box = new JComboBox(fillings);

return box;
}

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
GroupLayoutEx ex = new GroupLayoutEx();
ex.setVisible(true);
}
});
}
}

我建议研究两位经理并选择你最喜欢的。

关于java - 表格位于框架底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25247220/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com