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, 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, 21, 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

上面跟踪。

最新更新