通过这个类,我可以获得屏幕的分辨率,之后我可以正确设置 java swing 组件的高度和宽度。
如果我在计算机的第一台显示器上运行应用程序,那么这是有效的。如果我尝试在第二个显示器上启动应用程序,类将获得第一个屏幕的分辨率。
这是我计算屏幕分辨率的课程:
包 com.mcsolution.common.Componenti_Swing;
import java.awt.Dimension;
import java.awt.Toolkit;
public class Dimensione extends Dimension {
Dimension screenSize;
double larghezza,altezza;
public Dimensione(){
Toolkit t = Toolkit.getDefaultToolkit();
screenSize = t.getScreenSize();
double width = screenSize.getWidth();
double height= screenSize.getHeight()*0.9;
this.setSize(width, height);
}
/********PANEL******************/
public Dimension DimensionePanel(){
larghezza = screenSize.getWidth();
altezza = screenSize.getHeight()*0.95;
// System.out.println("Pannello la larghezza � " + width + "la larghezza � " + larghezza);
this.setSize(larghezza,altezza);
return this;
}
public Dimension PanelArticoliScontrini(){
larghezza = screenSize.getWidth();
altezza = screenSize.getHeight()*0.65;
// System.out.println("Pannello la larghezza � " + width + "la larghezza � " + larghezza);
this.setSize(larghezza,altezza);
return this;
}
public Dimension PanelArticoliDaStampare(){
larghezza = screenSize.getWidth();
altezza = screenSize.getHeight()*0.90;
// System.out.println("Pannello la larghezza � " + width + "la larghezza � " + larghezza);
this.setSize(larghezza,altezza);
return this;
}
}
有办法检测应用程序在哪个显示器上运行并获取屏幕尺寸吗?
jframe.getGraphicsConfiguration().getDevice();
获取设备。然后使用
device.getDefaultConfiguration().getBounds();
获得分辨率。这是一个工作示例:
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import java.awt.GraphicsDevice;
import java.awt.Rectangle;
public class Demo {
private Demo() {
JFrame jframe = new JFrame("Test");
// Get the current device
GraphicsDevice device = jframe.getGraphicsConfiguration().getDevice();
// get the resolution of the device
Rectangle r = device.getDefaultConfiguration().getBounds();
String s = "Device: " + device.getIDstring() + "\nResolution: " + r.width + "x" + r.height;
jframe.add(new JTextArea(s), BorderLayout.CENTER);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.pack();
jframe.setSize(400,300);
jframe.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Demo();
}
});
}
}