【问题标题】:Java setMaximizedbounds not working properly on dual screen environmentJava setMaximizedbounds 在双屏环境下无法正常工作
【发布时间】:2021-02-26 13:40:29
【问题描述】:

第一个主显示器(左):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;
    }
}

【问题讨论】:

    标签: java swing fullscreen screen-resolution multiple-monitors


    【解决方案1】:

    https://bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8263086

    这是一个java bug,可以在url上方追踪

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-26
      • 1970-01-01
      • 2020-11-24
      相关资源
      最近更新 更多