[英]Java Swing: problems with width
我在理解我的應用程序的行為時遇到問題。 我想創建一個簡單的窗口( 1000x700px ),分為兩個部分(分別為250px和750px寬度)。 我嘗試了以下代碼:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Example extends JFrame
{
private static final long serialVersionUID = 1L;
public Example()
{
this.setSize(1000, 700);
this.setTitle("Example");
this.setResizable(false);
this.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
JPanel navigation_panel_wrap = new JPanel();
JPanel content_panel_wrap = new JPanel();
navigation_panel_wrap.setPreferredSize(new Dimension(250, 700));
content_panel_wrap.setPreferredSize(new Dimension(750, 700));
content_panel_wrap.setBackground(Color.green);
navigation_panel_wrap.setBackground(Color.red);
this.getContentPane().add(navigation_panel_wrap);
this.getContentPane().add(content_panel_wrap);
}
public static void main(String[] args)
{
Example example = new Example();
example.setVisible(true);
}
}
如您所見,我為JFrame
手動設置了布局管理器(使用FlowLayout
代替了BorderLayout
, 水平和垂直間隙為零 )。 當然,我只能使用BorderLayout
而不是對BorderLayout.EAST
和BorderLayout.WEST
參數使用add()
方法,但是我想了解FlowLayout
。 運行我的應用程序時,得到以下信息(沒有綠色的JPanel
):
例如,如果我減小content_panel_wrap
寬度並將其設置為744px而不是750px ,則一切正常。 所以問題是-這些奇怪的6像素是什么? 我不確定該值在所有操作系統中是否都是恆定值,因此我想了解其來源。
FlowLayout
沒什么問題,但是您需要為所有要調整大小的組件調用pack()
。
至於您的代碼問題(+1 @Reimeus),調用pack()
是解決方案。 根據文檔:
使此窗口的大小適合其子組件的首選大小和布局。 如果窗口和/或其所有者尚不可顯示,則在計算首選大小之前,都應使其可顯示。 在計算preferredSize之后,將驗證Window。
提示:
JFrame
。 創建和更改UI組件時,請使用事件調度線程 :
SwingUtilities.invokeLater(new Runnable() { @Override public void run() { // create UI components etc here } });
setPreferredSize()
而要重寫組件的getPrefferedSize()
。 JFrame
上調用setSize(...)
,而應在設置它可見之前調用JFrame#pack()
。 JFrame#defaultCloseOperation(..)
否則在關閉JFrame
時,您的初始/ EDT線程將不會終止。 這是結合我的建議和您的代碼的示例:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Example {
private final JFrame frame;
public Example() {
frame = new JFrame();
frame.setTitle("Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//app exited when frame closes
frame.setResizable(false);
frame.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
JPanel navigation_panel_wrap = new JPanel() {
@Override
public Dimension getPreferredSize() {
return new Dimension(250, 700);
}
};
JPanel content_panel_wrap = new JPanel() {
@Override
public Dimension getPreferredSize() {
return new Dimension(750, 700);
}
};
content_panel_wrap.setBackground(Color.green);
navigation_panel_wrap.setBackground(Color.red);
frame.add(navigation_panel_wrap);
frame.add(content_panel_wrap);
//pack frame (size components to preferred size)
frame.pack();
frame.setVisible(true);//make frame visible
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Example();
}
});
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.