我编写了一个简单的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;
}
}
}
这就是页面的样子:
有任何建议如何解决?
答案 0 :(得分:1)
我建议您重构代码,因为它很难按照原样进行操作 - 例如,showTable()
方法可以重命名为getTable()
,因为您需要返回你创建的表。
从我的评论中 - 将表格添加到布局并将其设置为BorderLayout.CENTER
。请参阅布局here上的文档。
我的意思是您根据各自的位置将组件布局到BorderLayout上。
您希望表格位于中心位置,这会使SOUTH
(也称为PAGE_END
)对其自己的组件(即空格)开放。
JPanel btnPanel = new JPanel("Button"); // Add your logic to create the button panel
frame.add(btnPanel , BorderLayout.PAGE_START); // Or BorderLayout.NORTH
JPanel tblPanel = new JPanel("Table"); // Table panel
frame.add(button, BorderLayout.CENTER ); // Inhabits center
JPanel southPanel = new JPanel("South Panel");
frame.add( southPanel,BorderLayout.SOUTH );
答案 1 :(得分:1)
您要做的是嵌套简单的布局管理器
创建你的布局。我认为正式的Swing教程
通过引入这些来对程序员造成损害
简单的布局经理首先。我建议避免它们
花一些时间投入更强大的经理人。
FlowLayout
,BorderLayout
和Gridlayout
非常非常
简单的经理,但他们做不了多少。
我建议使用这两位经理:
MigLayout
GroupLayout
(JGoodies' s FormLayout
也是一个不错的选择。)
这些经理不仅更强大,而且还能应对 其他人失败的更高级要求。 (解析度 独立性,坚持OS设计原则等。)
在我提出两个代码示例之前,我想说几点
指向您的代码。您不必要地拨打repaint()
和revalidate()
方法。
setPreferredSize(new Dimension(1000, 500));
指定像素大小不可移植。你最好依靠
pack()
方法。
setLayout(new BorderLayout());
JFrame's
默认布局管理器(更准确地说是其内容窗格' s)
是BorderLayout
。因此,不需要这一行。
以下两个示例是包含MigLayout
和GroupLayout
的解决方案。
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);
}
});
}
}
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);
}
});
}
}
我建议学习两位经理并选择自己喜欢的。