微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

拉伸后,最后一个面板没有拉伸,但面板中的倒数第二个

如何解决拉伸后,最后一个面板没有拉伸,但面板中的倒数第二个

关注Java官方教程:(https://docs.oracle.com/javase/tutorial/uiswing/components/table.html)

不过这个没有源码,图:

enter image description here

所以我自己做了。我的愚蠢想法是这里有 4 个区域。所以 BorderLayout 对我来说效果不佳。因为我已经尝试过 BorderLayout.PAGE_END。它不起作用。所以我制作了一个面板来合并除第一个滚动面板之外的所有 3 个面板。它效率低下,但有效。

但现在的问题是当我拖动(拉伸)框架时,最后一个文本区域没有拉伸,但倒数第二个选择选项被拉伸。

如何在调整框架大小时使最后一个文本区域拉伸?

下面是我的代码

wrong stretch

JRadioButton mulintselRadioButton = new JRadioButton("Multiple Interval Selection");
JRadioButton singleselRadioButton = new JRadioButton("Single Selection");
JRadioButton singleIIntSelRadioButton = new JRadioButton("Single Interval Selection");
JCheckBox rowSelection = new JCheckBox("Row Selection");
JCheckBox columnSelection = new JCheckBox("Column Selection");
JCheckBox cellSelection = new JCheckBox("cell Selection");

ButtonGroup G = new ButtonGroup();
ButtonGroup GButton = new ButtonGroup();
GButton.add(rowSelection);
GButton.add(columnSelection);
GButton.add(cellSelection);
G.add(mulintselRadioButton);
G.add(singleIIntSelRadioButton);
G.add(singleselRadioButton);

JTable table = new JTable(data,columnNames);
TableColumn column = null;
table.setPreferredScrollableViewportSize(new Dimension(400,70));
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

table.setFillsViewportHeight(true);
JScrollPane scrollPane = new JScrollPane(table);
JPanel selectionMode = new JPanel(new GridLayout(4,1));
JPanel selectionoption = new JPanel(new GridLayout(4,1));
JPanel textAreaPanel = new JPanel(new GridLayout(1,1));
JPanel consolidatedPanel = new JPanel(new BorderLayout());
JLabel selcttionModeTitle = new JLabel("Selection Mode");
JLabel selcttionoptionTitle = new JLabel("Selection Options");
JTextArea textArea = new JTextArea("thsisaskjhkjk  shial");
textAreaPanel.add(textArea);
selectionMode.add(selcttionModeTitle);
selectionMode.add(singleIIntSelRadioButton);
selectionMode.add(singleselRadioButton);
selectionMode.add(mulintselRadioButton);
selectionoption.add(selcttionoptionTitle);
selectionoption.add(rowSelection);
selectionoption.add(columnSelection);
selectionoption.add(cellSelection);

textArea.setText("hsknd hkcjshksdjl sldh RadioButton mulintselRadioButton = new JRadioButton(\"Multiple Interval Selection\");\n" +
        "        JRadioButton singleselRadioButton = new JRadioButton(\"Single Selection\");\n" +
        "        JRadioButton singleIIntSelRadioButton = new JRadioButton(\"Single Interval Selection\");\n" +
        "        JCheckBox rowSelection = new JCheckBox ");

add(scrollPane,BorderLayout.norTH);
consolidatedPanel.add(selectionMode,BorderLayout.norTH);
consolidatedPanel.add(selectionoption,BorderLayout.CENTER);
consolidatedPanel.add(textAreaPanel,BorderLayout.soUTH);
add(consolidatedPanel);

解决方法

按照 Java 的官方教程(您在问题中发布的链接),可以看到它说您可以咨询 example index...如果您按照您可以在此链接中找到一个表格,该表格为您提供了指向您正在搜索的文件的链接。具体您要查找的代码在以下链接中:

https://docs.oracle.com/javase/tutorial/uiswing/examples/components/TableSelectionDemoProject/src/components/TableSelectionDemo.java

至于您的问题,您可以使用 BoxLayout 来实现您的要求,与 TableSelectionDemo 完全一样。结果可以拉伸表格和文本区域,但不能拉伸中间部分。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。