java - 根据 JFrame 大小调整 JPanel 宽度
问题描述
我想知道是否有一种方法可以通过从框架大小中获取面板的宽度来调整面板的宽度,而对于高度值,我想保留默认值。
提前致谢!
解决方案
使用BorderLayout
卢克!
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;
public class BorderLayoutExample {
public static void main(String[] args) {
JTextArea area = new JTextArea(15, 40);
JFrame frm = new JFrame("Horizontal resizing");
// not required, because the default layout here is the BorderLayout.
// but in common case you probably need to do it.
frm.setLayout(new BorderLayout());
// Use BorderLayout.SOUTH - when you want that the top space will not be unused.
frm.add(new JScrollPane(area), BorderLayout.NORTH);
frm.pack();
frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frm.setLocationRelativeTo(null);
frm.setVisible(true);
}
}
调整框架大小时,只会修改文本区域的宽度。有关更多信息,请阅读布局管理器
推荐阅读
- android - Zxing android库二维码扫描更改为横向模式
- python - 在一列字符串中搜索多个子字符串并返回子字符串类别
- ruby - 正则表达式从字符串中选择所有逗号,它们周围没有任何空格
- c# - Visual Studio Code .NET Core 调试器没有遇到断点
- javascript - 在 npm 中安装模块会给出错误消息缺少宽对齐模块
- php - php-fpm 慢日志:curl_multi_select 超过 10 秒
- if-statement - 如何重构深度嵌套的函数?
- .htaccess - 将目录中的所有文件重定向到其子文件夹
- python - 如何使用基于 TCP 的 HTTP 在 python 中下载图像?
- java - Maven 在生成 Sure-fire 报告时抛出“java.lang.OutOfMemoryError”