我设置最大化的框架: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
只需在面板上添加一个面板就会导致面板成为内容区域的大小。 –