Java swing 窗體居中
阿新 • • 發佈:2019-02-03
1.頂層元件JFrame或者對話方塊JDialog居中
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize = frame.getSize(); if (frameSize.height > screenSize.height) frameSize.height = screenSize.height; if (frameSize.width > screenSize.width) frameSize.width = screenSize.width; frame.setLocation((screenSize.width-frameSize.width)/2,screenSize.height-frameSize.height) / 2);
2.顯示器尺寸改變螢幕解析度變化元件自適應
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); screenSize = Toolkit.getDefaultToolkit().getScreenSize(); frame.setPreferredSize(new Dimension(512,450)); int frameWidth = this.getPreferredSize().width; int frameHeight = this.getPreferredSize().height; frame.setSize(frameWidth, frameHeight); frame.setLocation((screenSize.width - frameWidth) / 2,(screenSize.height - frameHeight) / 2);
3.Swing預設字型改變
Font vFont = new Font("Dialog", Font.PLAIN, 13); UIManager.put("ToolTip.font", vFont); UIManager.put("Table.font", vFont); UIManager.put("TableHeader.font", vFont); UIManager.put("TextField.font", vFont); UIManager.put("ComboBox.font", vFont); UIManager.put("TextField.font", vFont); UIManager.put("PasswordField.font", vFont); UIManager.put("TextArea.font", vFont);