我需要在螢幕上放置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);
試試下面的例子.請注意07000“如何調整此視窗的大小以适合其子元件的首選大小和布局.”
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 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();
}
});
}
}