1 回答

TA貢獻(xiàn)1777條經(jīng)驗(yàn) 獲得超3個贊
最初的問題是您initComponents
提前調(diào)用,即在應(yīng)用您想要的尺寸之前調(diào)用。pack
因此,在組件方法中調(diào)用initComponents
可將面板縮小到最小尺寸。
之后您更改了對話框的大小(通過調(diào)用setSize(w,h)
),但這并沒有直接影響所涉及的組件。但是,當(dāng)對話框設(shè)置為visible
組件時,組件會自動調(diào)整以適合定義的尺寸。這不適用于您的列大小,因?yàn)槟鷽]有定義ComponentListener
會觸發(fā)此操作的列大?。ㄕ垍㈤喯旅娴牡诙€示例)。
update
這導(dǎo)致第一次單擊按鈕來考慮組件的調(diào)整大小,因此它們被應(yīng)用到列。
要解決不同大小的問題,請將構(gòu)造函數(shù)中的方法調(diào)用更改為
(下面進(jìn)一步介紹構(gòu)造函數(shù)的完整示例):
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()
。
如果您想在用戶手動調(diào)整對話框大小時保持列大小,請考慮添加 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();
? ? ? ? ?}
? ? });
}
添加回答
舉報