1 回答

TA貢獻1777條經驗 獲得超3個贊
最初的問題是您initComponents
提前調用,即在應用您想要的尺寸之前調用。pack
因此,在組件方法中調用initComponents
可將面板縮小到最小尺寸。
之后您更改了對話框的大?。ㄍㄟ^調用setSize(w,h)
),但這并沒有直接影響所涉及的組件。但是,當對話框設置為visible
組件時,組件會自動調整以適合定義的尺寸。這不適用于您的列大小,因為您沒有定義ComponentListener
會觸發此操作的列大?。ㄕ垍㈤喯旅娴牡诙€示例)。
update
這導致第一次單擊按鈕來考慮組件的調整大小,因此它們被應用到列。
要解決不同大小的問題,請將構造函數中的方法調用更改為
(下面進一步介紹構造函數的完整示例):
int w = (int) (Math.round(d.getWidth()) / 2);
int h = (int) (Math.round(d.getHeight()) / 2);
setPreferredSize(new Dimension(w, h));
initComponents();
setSize(new java.awt.Dimension(0, 0));
您可能想從您的方法中刪除initComponents()
。
如果您想在用戶手動調整對話框大小時保持列大小,請考慮添加 a?ComponentListener
,作為另一個示例,請檢查此answer
.
這也可以用作原始代碼的替代解決方案,但首先正確定義大小可能會更清晰。
public Test(Dimension d) {
? ? int w = (int) (Math.round(d.getWidth()) / 2);
? ? int h = (int) (Math.round(d.getHeight()) / 2);
? ? setPreferredSize(new Dimension(w, h));
? ? initComponents();
? ? setLocationRelativeTo(null);
? ? bUpdate.addActionListener(new ActionListener() {
? ? ? ? public void actionPerformed(ActionEvent e) {
? ? ? ? ? ? cargarProductos();
? ? ? ? }
? ? });
? ? this.addComponentListener(new ComponentAdapter() {
? ? ? ? ?@Override
? ? ? ? ?public void componentResized(ComponentEvent e) {
? ? ? ? ? ? ?cargarProductos();
? ? ? ? ?}
? ? });
}
添加回答
舉報