java-使用BoxLayout动态扩展JPanel(在null布局上)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-使用BoxLayout动态扩展JPanel(在null布局上),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3060字,纯文字阅读大概需要5分钟。
内容图文
![java-使用BoxLayout动态扩展JPanel(在null布局上)](/upload/InfoBanner/zyjiaocheng/695/9b2133d3e43c4981beee730d97c3a288.jpg)
我有一个带有垂直BoxLayout的JPanel,在带有空布局的JPanel之上.
我希望带有BoxLayout的JPanel随着组件的添加而增长.
参见以下代码:
public static void main (String[] args) {
JFrame f = new JFrame();
f.setSize(500,500);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel total = new JPanel();
total.setLayout(null);
total.setSize(f.getWidth(),f.getHeight());
total.setBackground(Color.green);
JPanel box = new JPanel();
box.setLocation(100,200);
box.setLayout(new BoxLayout(box, BoxLayout.Y_AXIS));
box.add(new JButton("test"));
box.add(new JLabel("hey"));
total.add(box);
f.add(total);
f.setVisible(true);
}
您会注意到没有组件出现.
我如何使JPanel“盒子”成为这样,当我添加更多组件(垂直添加)时,其大小会动态增加.
提前:我需要“盒子”的位置正好在100,200,所以请不要建议我不要使用空布局.我必须使用空布局. “总计”的空布局不应影响我的问题的答案,该问题集中在“框”面板上.
解决方法:
通过扔掉布局管理器,您突然对它的工作负责.我可能要添加的工作,这并不容易…
基本上,以您为例,您没有设置子组件的大小…
JFrame f = new JFrame();
f.setSize(500, 500);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel total = new JPanel();
total.setLayout(null);
total.setSize(f.getWidth(), f.getHeight());
total.setBackground(Color.green);
JPanel box = new JPanel();
box.setLocation(100, 200);
box.setLayout(new BoxLayout(box, BoxLayout.Y_AXIS));
box.add(new JButton("test"));
box.add(new JLabel("hey"));
box.setSize(100, 100); // <-- Don't forget this..
total.add(box);
f.add(total);
f.setVisible(true);
就个人而言,我认为您是在自找麻烦,但我会知道…
一个更好的主意可能是使用EmptyBorder之类的东西来提供填充…
JFrame f = new JFrame();
f.setSize(500, 500);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel total = new JPanel(new BorderLayout());
total.setSize(f.getWidth(), f.getHeight());
total.setBackground(Color.green);
total.setBorder(new EmptyBorder(100, 200, 100, 200));
JPanel box = new JPanel();
box.setLayout(new BoxLayout(box, BoxLayout.Y_AXIS));
box.add(new JButton("test"));
box.add(new JLabel("hey"));
total.add(box);
f.add(total);
f.setVisible(true);
更新了布局管理器示例
现在,如果所有布局管理器都使您失望,则可以尝试编写自己的布局管理器.这具有空布局管理器所需的好处,以及集成到Swing的组件更改过程中的好处,而无需求助于ComponentListeners和ContainerListeners
JPanel total = new JPanel();
total.setLayout(new SuperAwesomeBetterThenYoursLayout());
自定义布局管理器
public static class SuperAwesomeBetterThenYoursLayout implements LayoutManager {
@Override
public void addLayoutComponent(String name, Component comp) {
}
@Override
public void removeLayoutComponent(Component comp) {
}
@Override
public Dimension preferredLayoutSize(Container parent) {
return new Dimension(100, 300);
}
@Override
public Dimension minimumLayoutSize(Container parent) {
return new Dimension(100, 300);
}
@Override
public void layoutContainer(Container parent) {
boolean laidOut = false;
for (Component child : parent.getComponents()) {
if (child.isVisible() && !laidOut) {
child.setLocation(200, 100);
child.setSize(child.getPreferredSize());
} else {
child.setSize(0, 0);
}
}
}
}
这基本上代表了您无论如何都必须做的工作,但实际上与Swing的设计工作息息相关.
内容总结
以上是互联网集市为您收集整理的java-使用BoxLayout动态扩展JPanel(在null布局上)全部内容,希望文章能够帮你解决java-使用BoxLayout动态扩展JPanel(在null布局上)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。