1
A
回答
-2
如果不使用布局(setLayout(null)
),你需要计算JFrame
内JPanel
的位置,如:
import java.awt.Color;
import javax.swing.*;
public class a extends JFrame {
public a()
{
JPanel panel = new JPanel();
panel.setSize(200, 200);
panel.setBackground(Color.RED);
setSize(400, 400); // JFrame arbitrary size.
getContentPane().setLayout(null);
getContentPane().add(panel);
setVisible(true);
// Caculate panel location after showing the JFrame in order to get the right insets (window's title bar).
int panelX = (getWidth() - panel.getWidth() - getInsets().left - getInsets().right)/2;
int panelY = ((getHeight() - panel.getHeight() - getInsets().top - getInsets().bottom)/2);
panel.setLocation(panelX, panelY);
}
public static void main(String[] args) {
new a();
}
}
+0
`getContentPane()。setLayout(null);`这个问答在SO的另一个问题中被引用。为了记录,'null'布局不是这里的路。如果**是正确的,那么很少。 – 2013-05-17 09:17:34
相关问题
- 1. JFrame和JPanel居中
- 2. JFrame中的JPanel
- 3. 如何在JFrame中居中并扩展我的JPanel?
- 4. 在JFrame中用JPanel替换JPanel
- 5. JFrame中的Sandbox JPanel
- 6. Eclipse:在JFrame中添加JPanel
- 7. JPanel不显示在JFrame中
- 8. 在从的JFrame JPanel中
- 9. 在JFrame中移动JPanel
- 10. 在JFrame中设置JPanel?
- 11. JFrame中看不到JPanel中的子JPanel
- 12. 在java中的JPanel中显示JFrame
- 13. 将JPanel添加到JFrame中
- 14. 添加的JPanel到JFrame中
- 15. 从JFrame到Netbeans中的JPanel
- 16. 从JFrame中清除JPanel
- 17. JFrame中的CardLayout JPanel GUI
- 18. 把JFrame放到jPanel中
- 19. 更新JFrame中的JPanel
- 20. JPanel vs Java中的JFrame
- 21. 将JPanel添加到JFrame中
- 22. 在JPanel中居中一个JCheckBox
- 23. 在JFrame中居中放置图像?
- 24. 无法将jdialog居中在jframe中
- 25. 如何在JFrame中居中对象
- 26. JPanel不显示在JFrame中 - Java
- 27. 在JFrame中显示JPanel的内容
- 28. 在单个JFrame中使用多个JPanel
- 29. Java:在JFrame/JPanel中定位JButtons
- 30. 在JFrame中编辑多个JPanel
我认为它是一个非常简单的程序。但是如果有人会告诉我如何将JPanel放在JFrame中,无论是否有布局管理器,我都会很高兴。谢谢。 – tomericco 2011-02-04 09:35:03