java JPanel setSize 和 setLocation
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18105936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java JPanel setSize and setLocation
提问by user2580555
Hey this is my second post so don't get mad at me but I'm having issues with the JPanel in java. I am trying to set the size and the location but it won't work, I have tried repaint(); but that does not work. Any help?
嘿,这是我的第二篇文章,所以不要生我的气,但我在 Java 中遇到了 JPanel 问题。我正在尝试设置大小和位置,但它不起作用,我尝试过 repaint(); 但这不起作用。有什么帮助吗?
here's my code:
这是我的代码:
package test.test.test;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.TextField;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test extends JFrame {
JPanel colorPanel = new JPanel();
public Display(){
super("JPanel test");
setLayout(new FlowLayout());
add(colorPanel);
colorPanel.setBackground(Color.CYAN);
colorPanel.setSize(300, 300);
repaint();
}
}
采纳答案by Gilbert Le Blanc
For the benefit of anyone reading this question later, here's a short, self contained, correct example of defining a JPanel with a background color.
为了以后阅读此问题的任何人的利益,这里有一个简短的、自包含的、正确的示例,用于定义具有背景颜色的 JPanel。
Almost all the time, you should let the Swing component layout manager determine the size of Swing components. In this case, we define the preferred size of the JPanel because the JPanel does not contain any other Swing components.
几乎所有的时间,您都应该让 Swing 组件布局管理器确定 Swing 组件的大小。在这种情况下,我们定义 JPanel 的首选大小,因为 JPanel 不包含任何其他 Swing 组件。
The default layout manager of a JFrame is BorderLayout. The JPanel is positioned in the center of the BorderLayout.
JFrame 的默认布局管理器是 BorderLayout。JPanel 位于 BorderLayout 的中心。
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class SimplePanel implements Runnable {
@Override
public void run() {
JFrame frame = new JFrame("JPanel Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel colorPanel = new JPanel();
colorPanel.setBackground(Color.CYAN);
colorPanel.setPreferredSize(new Dimension(300, 300));
frame.add(colorPanel);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new SimplePanel());
}
}
回答by Joris W
When using Flowlayout, you should set the prefered size (of the component you add to your panel) instead of size, because the layoutmanager will handle the size an location for you.
使用 Flowlayout 时,您应该设置首选大小(您添加到面板的组件的)而不是大小,因为 layoutmanager 会为您处理位置的大小。
public class Test extends JFrame {
JPanel colorPanel = new JPanel();
public Display(){
super("JPanel test");
getContentPane().setLayout(new FlowLayout());
colorPanel = new JPanel
colorPanel.setPreferedSize(new Dimension(300,300));
colorPanel.setBackground(Color.CYAN);
getContentPane().add(colorPanel);
pack();
repaint();
}
}
and don't forget to set your JFrame visible and the size (of use pack()) ;)
并且不要忘记设置您的 JFrame 可见和大小(使用 pack()) ;)