【问题标题】:Position on Screen Right Bottom屏幕右下角位置
【发布时间】:2012-04-02 23:13:10
【问题描述】:

我需要在我的屏幕上定位 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);

【问题讨论】:

  • 一些平台对此进行了限制,如here所述。

标签: java swing jframe point layout-manager


【解决方案1】:

我知道的最简单的方法是使用自己的布局管理器嵌套每个 JPanel。

  • 主 JPanel 将使用 BorderLayout
  • 在 BorderLayout.SOUTH 位置添加到 main 的另一个 JPanel 也使用 BorderLayout。
  • 需要走SE角的组件添加到上面JPanel的BorderLayout.EAST位置。
  • 一般来说,与尝试设置组件的绝对位置相比,使用布局管理器几乎总是更好。

【讨论】:

  • 是的!我想设置 JFrame。我为我的误会道歉!但是感谢您尝试 Pal!
  • 当我们必须分别使用 BorderLayout.PAGE_ENDBorderLayout.LINE_START 时,参考 BorderLayout.SOUTHBorderLayout.EAST 只是一个建议,用于 jdk 1.4+,如 BorderLayout Tutorials 所指: -)
【解决方案2】:

试试下面的例子。请注意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();
            }
        });
    }
}

【讨论】:

    猜你喜欢
    • 2013-02-17
    • 1970-01-01
    • 2013-04-22
    • 1970-01-01
    • 2019-01-05
    • 2015-07-11
    • 2020-11-05
    • 2011-01-28
    • 2019-09-04
    相关资源
    最近更新 更多