Border titleBorder2 = BorderFactory.createTitledBorder("your infomation"); scrollPane.setBorder(titleBorder2); JTextArea infomation = new JTextArea(); scrollPane.add(infomation); JPanel panel_3 = new JPanel(); JPanel panel_4 = new JPanel(); splitPane.setRightComponent(panel_4); panel_4.add(scrollPane); panel_4.setPreferredSize(100,100); panel_4.add(panel_3); JButton savebtn = new JButton("Save"); panel_3.add(savebtn);优化代码
时间: 2024-02-13 14:05:08 浏览: 377
Swing编程边框(Border)的用法总结.doc
可以对这段代码进行如下优化:
```
JPanel panel = new JPanel(new BorderLayout());
Border border = BorderFactory.createTitledBorder("Your Information");
JTextArea information = new JTextArea();
JScrollPane scrollPane = new JScrollPane(information);
scrollPane.setBorder(border);
JPanel buttonPanel = new JPanel();
JButton saveButton = new JButton("Save");
buttonPanel.add(saveButton);
panel.add(scrollPane, BorderLayout.CENTER);
panel.add(buttonPanel, BorderLayout.SOUTH);
panel.setPreferredSize(new Dimension(100, 100));
```
这段代码将三个面板合并为一个面板,并使用BorderLayout布局管理器来排列组件。另外,使用了更加简洁的变量名,同时也避免了不必要的重复代码。
阅读全文