如何在启动时将JFrame定位到屏幕的右上角

use*_*318 1 java layout swing jframe

如何将JFrame放在TOP_RIGHT上?

我知道中心,左上角是正常的,但如何放入最右边?

Pau*_*tha 6

作为这个答案的一个例子:定位到右下角,我做了一个调整,让它出现在右上角

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;

public class TopRightFrame {

    private void display() {
        JFrame f = new JFrame("Top-Right Frame");
        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 = 0;
        f.setLocation(x, y);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TopRightFrame().display();
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)