Java Swing:相对定位和自动调整大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3867167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Swing: Relative Positioning and Automatic Resizing?
提问by KJW
I have a createUI() function which loads JPanel into NORTH, SOUTH, EAST.
我有一个 createUI() 函数,它将 JPanel 加载到北、南、东。
private void createUI() {
add(createToolBar(), BorderLayout.NORTH);
add(createDataView(), BorderLayout.SOUTH);
add(createHistoryView(), BorderLayout.EAST);
createMenuBar();
setTitle("My Application");
}
Each components display in the correct location, however, they are all in fixed sized JPanel. I would like to know if it's possible for the JPanel to completely fill the BorderLayout region.
每个组件都显示在正确的位置,但是,它们都在固定大小的 JPanel 中。我想知道 JPanel 是否有可能完全填充 BorderLayout 区域。
For example, the createDataView() is a simple Jpanel with tables. I would like it to expand horizontally, and have a fixed height.
例如,createDataView() 是一个带有表格的简单 Jpanel。我希望它水平扩展,并有一个固定的高度。
private JPanel createDataView(){
JPanel panel = new JPanel();
String data[][] = {{"001","vinod","Bihar","India","Biology","65","First"},
{"002","Raju","ABC","Kanada","Geography","58","second"},
{"003","Aman","Delhi","India","computer","98","Dictontion"},
{"004","Ranjan","Bangloor","India","chemestry","90","Dictontion"},
{"004","Ranjan","Bangloor","India","chemestry","90","Dictontion"}};
String col[] = {"Roll","Name","State","country","Math","Marks","Grade"};
JTable table = new JTable(data,col);
table.setPreferredScrollableViewportSize(new Dimension(900,100));
JTableHeader header = table.getTableHeader();
header.setBackground(Color.yellow);
JScrollPane pane = new JScrollPane(table);
table.setAutoResizeMode(JTable.WIDTH);
table.doLayout();
panel.add(pane);
return panel;
}
Right now I am relying on manually setting the dimension. However, I would a way to get the current window width, and set it as below.
现在我依靠手动设置尺寸。但是,我想获取当前窗口宽度的方法,并将其设置如下。
table.setPreferredScrollableViewportSize(new Dimension(900,100));
采纳答案by trashgod
The default layout of a new JPanel
is FlowLayout
; a GridLayout
may be an alternative for your data view. You can compare the possibilities in A Visual Guide to Layout Managers.
new 的默认布局JPanel
是FlowLayout
; aGridLayout
可能是您的数据视图的替代方案。您可以在A Visual Guide to Layout Managers 中比较这些可能性。
JPanel panel = new JPanel(new GridLayout(1, 0));
Addendum: Your setAutoResizeMode()
looks suspicious; JTable.WIDTH
is unlikely to beonly coincidentally a valid value. GridLayout
works well with the default, JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS
, but you might experiment with other settings.
附录:你setAutoResizeMode()
看起来很可疑;JTable.WIDTH
是不太可能只是巧合的有效值。GridLayout
使用默认设置效果很好JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS
,但您可以尝试其他设置。
table.setAutoResizeMode(JTable.WIDTH);
回答by Codemwnci
With the borderlayout, the NORTH, SOUTH, EAST and WEST borders all take on the default size of the containing panel, but the CENTER expands to the take up all remaining space. I would suggest putting your createDataView in this section of the layout.
使用borderlayout,NORTH、SOUTH、EAST和WEST边框都采用包含面板的默认大小,但CENTER扩展到占用所有剩余空间。我建议将您的 createDataView 放在布局的这一部分。