屏幕右下角位置

Position on Screen Right Bottom(屏幕右下角位置)

本文介绍了屏幕右下角位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的屏幕上定位 JFrame.但我不能让它们出现在屏幕底部的右侧.

I need to position JFrame on my screen. But I can't make them appear on the right side of the screen bottom.

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

Please can someone explain me how to position them, if you can describe how to do it, it would be great.

这是目前为止的代码.

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

推荐答案

试试下面的例子.注意 pack() "使这个 Window 的大小适合其子组件的首选大小和布局."

Try the example below. Note how pack() "Causes this Window to be sized to fit the preferred size and layouts of its subcomponents."

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();
            }
        });
    }
}

这篇关于屏幕右下角位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:屏幕右下角位置

基础教程推荐