我設置最大化的框架:setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
如何獲得最大化的框架尺寸?
現在我怎麼能得到這個尺寸,因爲當我打電話getSize()
或getPreferredSize
返回0 0
?
我設置最大化的框架:setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
如何獲得最大化的框架尺寸?
現在我怎麼能得到這個尺寸,因爲當我打電話getSize()
或getPreferredSize
返回0 0
?
在執行setVisible(true);
後,您將獲得正確尺寸的最大化。
public NewJFrame() { // Constructor
initComponents();
this.setExtendedState(getExtendedState() | JFrame.MAXIMIZED_VERT | Frame.MAXIMIZED_HORIZ);
// height and width still prints the original values
System.out.println(this.getSize().height + " " + this.getSize().width);
}
....
public static void main(String args[]) { // main
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
NewJFrame foo = new NewJFrame();
foo.setVisible(true);
// after setVisible(true) actual maximized values
System.out.println(foo.getSize().height + " " + foo.getSize().width);
}
});
}
欲面板,其是在框一樣大(大)的尺寸是幀。
import java.awt.*;
import javax.swing.*;
class FullSizePanel {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// this is the panel we want to fill the entire content area
// of the parent frame.
JPanel redFullSizePanel = new JPanel(new BorderLayout());
// make it true to the first part of the attribute name
redFullSizePanel.setBackground(Color.RED);
JFrame f = new JFrame("Full Size Panel");
// 1) The default layout for a content pane is BorderLayout
// 2) A component added to a BL with no layout constraint ends
// up in the CENTER
// 3) A component in the CENTER of a BL takes the full space
// (that is not used up by components in the EAST, WEST,
// NORTH & SOUTH).
f.add(redFullSizePanel);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.pack();
f.setSize(400,400);
f.setLocationByPlatform(true);
f.setVisible(true);
}
});
}
}
什麼佈局有用嗎?
網格或方塊佈局1個和x行
import java.awt.*;
import javax.swing.*;
class FullSizePanel {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// grid or box layout with 1 column and x rows
LayoutManager lm = new GridLayout(0,1,5,5);
// this is the panel we want to fill the entire content area
// of the parent frame.
JPanel redFullSizePanel = new JPanel(lm);
// make it true to the first part of the attribute name
redFullSizePanel.setBackground(Color.RED);
for (int ii=1; ii<6; ii++) {
redFullSizePanel.add(new JLabel("Label " + ii));
redFullSizePanel.add(new JButton("Button " + ii));
}
JFrame f = new JFrame("Full Size Panel");
// 1) The default layout for a content pane is BorderLayout
// 2) A component added to a BL with no layout constraint ends
// up in the CENTER
// 3) A component in the CENTER of a BL takes the full space
// (that is not used up by components in the EAST, WEST,
// NORTH & SOUTH.
f.add(redFullSizePanel);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.pack();
f.setSize(400,400);
f.setLocationByPlatform(true);
f.setVisible(true);
}
});
}
}
您可以嘗試獲得最大的 「通用」 窗口大小。由於您的應用程序已最大化,它應該產生同樣的結果:
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
Dimension screenDimension = env.getMaximumWindowBounds().getSize();
不要忘記,你的窗口也有所謂的插圖:
Insets insets = frame.getInsets();
final int left = insets.left;
final int right = insets.right;
final int top = insets.top;
final int bottom = insets.bottom;
final int width = screenDimension.width - left - right;
final int height = screenDimension.height - top - bottom;
*「如何獲得...幀的大小?「*你爲什麼需要知道? 1)使用組件的佈局2)在JComponent或JPanel中執行自定義繪畫,並僅在繪畫時查詢大小。順便說一句 - 爲了更好地幫助更快,說明與戰略相對的目標(例如「使組件適合」)(例如「獲取框架大小」)。 –
我需要這個設置在這個框架中的面板的尺寸與這個框架一樣大小 – hudi
只需在面板上添加一個面板就會導致面板成爲內容區域的大小。 –