Java 屏幕右下角位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9753722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Position on Screen Right Bottom
提问by Isuru
I need to position JFrame on my screen. But I can't make them appear on the right side of the screen bottom.
我需要在我的屏幕上放置 JFrame。但我不能让它们出现在屏幕底部的右侧。
Please can someone explain me how to position them, if you can describe how to do it, it would be great.
请有人向我解释如何定位它们,如果您能描述如何做到这一点,那就太好了。
Here is the code so far.
这是到目前为止的代码。
//Gets the screen size and positions the frame left bottom of the screen
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice defaultScreen = ge.getDefaultScreenDevice();
Rectangle rect = defaultScreen.getDefaultConfiguration().getBounds();
int x = (int)rect.getMinX();
int y = (int)rect.getMaxY()- frame.getHeight();
frame.setLocation(x ,y - 45);
采纳答案by trashgod
Try the example below. Note how pack()
"Causes this Window
to be sized to fit the preferred size and layouts of its subcomponents."
试试下面的例子。请注意如何pack()
“导致Window
其大小适合其子组件的首选大小和布局。”
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JPanel;
/** @see http://stackoverflow.com/q/9753722/230513 */
public class LowerRightFrame {
private void display() {
JFrame f = new JFrame("LowerRightFrame");
f.add(new JPanel() {
@Override // placeholder for actual content
public Dimension getPreferredSize() {
return new Dimension(320, 240);
}
});
f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice defaultScreen = ge.getDefaultScreenDevice();
Rectangle rect = defaultScreen.getDefaultConfiguration().getBounds();
int x = (int) rect.getMaxX() - f.getWidth();
int y = (int) rect.getMaxY() - f.getHeight();
f.setLocation(x, y);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new LowerRightFrame().display();
}
});
}
}
回答by Hovercraft Full Of Eels
The easiest way I know is to nest JPanels each using its own layout manager.
我知道的最简单的方法是使用自己的布局管理器嵌套 JPanel。
- The main JPanel would use a BorderLayout
- Another JPanel that is added to the main at BorderLayout.SOUTH position also uses BorderLayout.
- the component that needs to go at the SE corner is added to the above JPanel at the BorderLayout.EAST position.
- In general, you're almost always better off using layout managers vs. trying to set absolute position of components.
- 主 JPanel 将使用 BorderLayout
- 在 BorderLayout.SOUTH 位置添加到 main 的另一个 JPanel 也使用 BorderLayout。
- 在上面的 JPanel 中的 BorderLayout.EAST 位置添加了需要走 SE 角的组件。
- 一般来说,与尝试设置组件的绝对位置相比,使用布局管理器几乎总是更好。