java – 屏幕右下角的位置

前端之家收集整理的这篇文章主要介绍了java – 屏幕右下角的位置前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要在屏幕上放置JFrame.但我无法让它们出现在屏幕底部的右侧.

请有人解释我如何定位它们,如果你能描述如何做,那就太好了.

这是迄今为止的代码.

//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);

解决方法

试试下面的例子.请注意0​​7000“如何调整此窗口的大小以适合其子组件的首选大小和布局.”
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 https://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();
            }
        });
    }
}

猜你在找的Java相关文章