我设置了最大化的框架:setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
现在我如何才能获得此尺寸,因为当我致电getSize()
或getPreferredSize
时,它会返回0 0
?
答案 0 :(得分:5)
执行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);
}
});
}
答案 1 :(得分:0)
我希望框架的大小与框架一样大(大)。
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);
}
});
}
}
答案 2 :(得分:0)
您可以尝试获取最大“通用”窗口大小。由于您的应用程序已最大化,因此应该产生相同的结果:
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;