如何解决Java setMaximizedbounds 在双屏环境下无法正常工作
第一个主显示器(左):1680x1050,文字大小为 %100
第二台显示器(右):1920x1080,文字大小为 %100
只是主显示器的分辨率低于辅助显示器。
我有一个 jframe(请参阅下面的测试代码),其中 setUndecorated=true 并且我确实实现了自己的最大化窗口操作。 简单地说,我确实计算了每台显示器的屏幕插图,并根据可用空间最大化帧。 但是,当涉及到辅助监视器时,这会失败。尽管尺寸根据显示器值给出,但屏幕并未覆盖整个屏幕。
提示:相同的代码适用于两个显示器具有相同分辨率或主显示器的分辨率大于辅助显示器的两种情况。
任何想法,解决方法?
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class FrameTest extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FrameTest frame = new FrameTest();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public FrameTest() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100,100,800,600);
contentPane = new JPanel();
contentPane.setBackground(Color.YELLOW);
contentPane.setBorder(new EmptyBorder(5,5,5));
setContentPane(contentPane);
contentPane.setLayout(null);
FrameDragListener frameDragListener = new FrameDragListener(this);
addMouseListener(frameDragListener);
addMouseMotionListener(frameDragListener);
JButton btnNewButton = new JButton("Max");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (getExtendedState() == MAXIMIZED_BOTH)
setExtendedState(NORMAL);
else {
setMaximizedBounds(getMaximizedBounds());
setExtendedState(MAXIMIZED_BOTH);
}
}
});
btnNewButton.setBounds(21,21,61,34);
contentPane.add(btnNewButton);
JButton btnNewButton_1 = new JButton("X");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
});
btnNewButton_1.setBounds(92,43,34);
contentPane.add(btnNewButton_1);
setUndecorated(true);
}
public static class FrameDragListener extends MouseAdapter {
private final JFrame frame;
private Point mouseDownCompCoords = null;
public FrameDragListener(JFrame frame) {
this.frame = frame;
}
public void mouseReleased(MouseEvent e) {
mouseDownCompCoords = null;
}
public void mousePressed(MouseEvent e) {
mouseDownCompCoords = e.getPoint();
}
public void mouseDragged(MouseEvent e) {
Point currCoords = e.getLocationOnScreen();
frame.setLocation(currCoords.x - mouseDownCompCoords.x,currCoords.y - mouseDownCompCoords.y);
}
}
public Rectangle getMaximizedBounds() {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gs = ge.getScreenDevices();
Rectangle bounds = this.getGraphicsConfiguration().getBounds();
Rectangle maxBounds = null;
Insets screenInsets = this.getToolkit().getScreenInsets(this.getGraphicsConfiguration());
//fails to have full screen in secondary monitor
maxBounds = new Rectangle(
screenInsets.left,screenInsets.top,bounds.width - screenInsets.right - screenInsets.left,bounds.height - screenInsets.bottom - screenInsets.top);
return maxBounds;
}
}
解决方法
https://bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8263086
这是一个java错误,可以在url上方跟踪
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。