Java 摆动 + UI 比例尺
Java swing + UI scale
我需要创建一个具有界面缩放功能的应用程序。我有一个按钮,里面有图标和 jpanel,它按住这个按钮。问题是当比例打开时 - 图标模糊,为了解决这个问题,我在 paintComponent 中使用了缩小比例。结果,当系统比例打开时,我有正常的图像。但是 JPanel 仍然有缩放的大小。我也尝试覆盖 JPanel paintComponent,但结果我的按钮太小了,因为按钮上的缩小和 JPanel 上的缩小一起工作。我不能仅使用 JPanel 中的比例,当我单击一个按钮时,它会采用缩放后的尺寸并再次模糊图像。
这是一个简单的例子。
代码是:
public class Test{
public static void main(String[] args) throws Exception{
System.setProperty("sun.java2d.uiScale", "1.5");
JFrame j = new JFrame();
Image img = ImageIO.read(new File("D:\1.png"));
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j.setPreferredSize(new Dimension(300, 150));
j.setVisible(true);
j.setLocationRelativeTo(null);
j.setLayout(new BorderLayout());
img = img.getScaledInstance((int) (60 * 1.5),(int) (60 * 1.5),Image.SCALE_DEFAULT);
JToggleButton tb = new JToggleButton(){
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.scale(0.67,0.67);
super.paintComponent(g2);
}
};
tb.setIcon(new ImageIcon(img));
JToggleButton tb2 = new JToggleButton(){
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.scale(0.67,0.67);
super.paintComponent(g2);
}
};
tb2.setIcon(new ImageIcon(img));
JPanel jPanel = new JPanel(){
};
jPanel.setLayout(new GridLayout(1,1));
jPanel.add(tb);
jPanel.setBackground(Color.RED);
JPanel content = new JPanel();
content.setLayout(new FlowLayout());
content.add(jPanel);
j.setContentPane(content);
j.pack();
}
}
我使用 java10。
谢谢。
您使用 1.5 的比例因子渲染 UI:所有 UI 包括图标和图像都经过缩放,以便在高 DPI 显示器上正确显示。如果图像无法缩放,则对于更高的 DPI 设置来说它太小了。
如果您的应用程序支持高 DPI 显示,即 UI 缩放,您应该提供不同分辨率的图像。参见 Java 中的 MultiResolutionImage 9.
您可以在 this answer 中找到相关问题的示例代码。
我需要创建一个具有界面缩放功能的应用程序。我有一个按钮,里面有图标和 jpanel,它按住这个按钮。问题是当比例打开时 - 图标模糊,为了解决这个问题,我在 paintComponent 中使用了缩小比例。结果,当系统比例打开时,我有正常的图像。但是 JPanel 仍然有缩放的大小。我也尝试覆盖 JPanel paintComponent,但结果我的按钮太小了,因为按钮上的缩小和 JPanel 上的缩小一起工作。我不能仅使用 JPanel 中的比例,当我单击一个按钮时,它会采用缩放后的尺寸并再次模糊图像。 这是一个简单的例子。
代码是:
public class Test{
public static void main(String[] args) throws Exception{
System.setProperty("sun.java2d.uiScale", "1.5");
JFrame j = new JFrame();
Image img = ImageIO.read(new File("D:\1.png"));
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j.setPreferredSize(new Dimension(300, 150));
j.setVisible(true);
j.setLocationRelativeTo(null);
j.setLayout(new BorderLayout());
img = img.getScaledInstance((int) (60 * 1.5),(int) (60 * 1.5),Image.SCALE_DEFAULT);
JToggleButton tb = new JToggleButton(){
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.scale(0.67,0.67);
super.paintComponent(g2);
}
};
tb.setIcon(new ImageIcon(img));
JToggleButton tb2 = new JToggleButton(){
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.scale(0.67,0.67);
super.paintComponent(g2);
}
};
tb2.setIcon(new ImageIcon(img));
JPanel jPanel = new JPanel(){
};
jPanel.setLayout(new GridLayout(1,1));
jPanel.add(tb);
jPanel.setBackground(Color.RED);
JPanel content = new JPanel();
content.setLayout(new FlowLayout());
content.add(jPanel);
j.setContentPane(content);
j.pack();
}
} 我使用 java10。 谢谢。
您使用 1.5 的比例因子渲染 UI:所有 UI 包括图标和图像都经过缩放,以便在高 DPI 显示器上正确显示。如果图像无法缩放,则对于更高的 DPI 设置来说它太小了。
如果您的应用程序支持高 DPI 显示,即 UI 缩放,您应该提供不同分辨率的图像。参见 Java 中的 MultiResolutionImage 9.
您可以在 this answer 中找到相关问题的示例代码。