我是 Swing 的新用户,我在按坐标绘制组件时遇到了问题。请看这段代码:
JFrame frame=new JFrame();
frame.setBounds(new Rectangle(0, 0, 700, 600));
frame.getContentPane().setBackground(Color.yellow);
frame.setVisible(true);
JPanel graph=new JPanel();
graph.setBounds(new Rectangle(0, 0, 700, 300));
graph.setBackground(Color.white);
graph.setOpaque(true);
frame.getContentPane().add(graph);
我需要 JPanel 从 JFrame 关闭 50%,但现在它关闭 100%,并且 JFrame 的背景为白色。我应该如何纠正错误?另外,是否有使用百分比设置宽度和高度的方法?例如,宽度为 50%。或者可能存在我的问题的任何容器?谢谢
最佳答案
不要使用whatever#setBound()
,使用Standard LayoutManager , 在这种情况下是 GridLayout(2, 0)最好的方法,你的JFrame及其 JPanels将在两个方向上调整大小
关于java - 如何为组件设置坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9453149/