有沒有辦法設置JFrame
的插圖? 我試圖如何設置JFrame的插頁?
frame.getContentPane().getInsets().set(10, 10, 10, 10);
和
frame.getInsets().set(10, 10, 10, 10);
,但他們都不工作。
有沒有辦法設置JFrame
的插圖? 我試圖如何設置JFrame的插頁?
frame.getContentPane().getInsets().set(10, 10, 10, 10);
和
frame.getInsets().set(10, 10, 10, 10);
,但他們都不工作。
你必須創建LayOutConstraint的對象,並設置其插圖。 像下面的例子一樣,我使用了GridBagLayout()並使用了GridBagConstraint()對象。
GridBagConstraints c = new GridBagConstraints();
JPanel panel = new JPanel(new GridBagLayout());
c.insets = new Insets(5, 5, 5, 5); // top, left, bottom, right
c.anchor = GridBagConstraints.LINE_END;
// Row 1
c.gridx = 0;
c.gridy = 0;
c.anchor = GridBagConstraints.LINE_START;
panel.add(isAlgoEnabledLabel, c);
JPanel contentPanel = new JPanel();
Border padding = BorderFactory.createEmptyBorder(10, 10, 10, 10);
contentPanel.setBorder(padding);
yourFrame.setContentPane(contentPanel);
所以基本上,contentPanel
是你的框架的主容器。
我沒有使用方法'getContentPane',我創建了'JPanel'名稱'contentPanel'來訪問'setBorder'方法。 –
重寫JFrame
的Insets
不會是soultion您的實際問題。 要回答你的問題,你不能設置JFrame
的插圖。您應該擴展JFrame並覆蓋getInsets
方法以提供您需要的插頁。
由於這個問題還沒有確定的答案,但你可以這樣做,就像basiljames說here。正確的方法是擴展JFrame
,然後覆蓋getInsets()
方法。
例如
import javax.swing.JFrame;
import java.awt.Insets;
public class JFrameInsets extends JFrame {
@Override
public Insets getInsets() {
return new Insets(10, 10, 10, 10);
}
private JFrameInsets() {
super("Insets of 10");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setMinimumSize(getSize());
setVisible(true);
}
public static void main(String[] args) {
new JFrameInsets();
}
}
爲什麼你需要的是什麼? –
如果在內容窗格中使用'JPanel',只需'panel.setBorder(new EmptyBorder(10,10,10,10));' –
@AndrewThompson如果OP詢問如何設置幀上的插圖,重寫'getInsets()'? – Dan