Java 如何在启动时将JFrame定位到屏幕右上角
如何将JFrame放在右上方 我知道中间的位置,正常的位置在左上角,但是如何放置在右上角呢 作为中的一个示例,我进行了调整,使其显示在右上角Java 如何在启动时将JFrame定位到屏幕右上角,java,swing,layout,jframe,Java,Swing,Layout,Jframe,如何将JFrame放在右上方 我知道中间的位置,正常的位置在左上角,但是如何放置在右上角呢 作为中的一个示例,我进行了调整,使其显示在右上角 import java.awt.Dimension; import java.awt.EventQueue; import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; import java.awt.Rectangle; import javax.swing.JFrame; i
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();
}
});
}
}
您只需在以下代码中修改现有的JFrame构造函数粘贴:
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice defaultScreen = ge.getDefaultScreenDevice();
Rectangle rect = defaultScreen.getDefaultConfiguration().getBounds();
int x = (int) rect.getMaxX() - this.getWidth();
int y = 0;
this.setLocation(x, y);
this.setVisible(true);
i、 e.所以它看起来像这样:
public class NewJFrame extends javax.swing.JFrame {
/**
* Creates new form NewJFrame
*/
public NewJFrame() {
initComponents();
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice defaultScreen = ge.getDefaultScreenDevice();
Rectangle rect = defaultScreen.getDefaultConfiguration().getBounds();
int x = (int) rect.getMaxX() - this.getWidth();
int y = 0;
this.setLocation(x, y);
this.setVisible(true);
}
没有帮助,但是谢谢。我刚才在这里找到了这个,我在搜索中更改了一些“单词”,我得到了它。。。。我看到y=0;)多谢各位,*