Java – bug about getbounds () and setbounds () on Linux_ Solution with id = 4806603?

On Linux platform, frame:: getbounds and frame:: setbounds cannot work consistently This was in 2003 (!) Report, see here:

http://bugs.java.com/bugdatabase/view_bug.do?bug_id=4806603

For convenience, I simplified the declaration code that caused the error and pasted it as:

import java.awt.Button;
import java.awt.Frame;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/** Demonstrates a bug in the java.awt.Frame.getBounds() method.
 * @author Mirko Raner,PTSC
 * @version 1.0 (2003-01-22) **/
public class GetBoundsBug extends Frame implements ActionListener {
  public static void main(String[] arg) {
    GetBoundsBug frame = new GetBoundsBug();
    Button button = new Button("Click here!");
    button.addActionListener(frame);
    frame.add(button);
    frame.setSize(300,300);
    frame.setVisible(true);
  }

  @Override
  public void actionPerformed(ActionEvent event) {
    Rectangle bounds = getBounds();
    bounds.y--;
    setBounds(bounds);
    bounds.y++;
    setBounds(bounds);
  }
}

Unexpected behavior: the window moves slightly after clicking the button! (28 pixels per click on my system.)

This is a screen recording: @ L_ 419_ 2@

This behavior has existed for 13 years, so the official may not change

Does anyone have a solution to this bug? Specifically, I want to reliably store and restore the previous window / frame / dialog box on all platforms

PS: my java installation is jdk1 8.0_ 102 for Oracle AMD64 on Ubuntu 16 Linux Since I recently migrated from windows to Ubuntu, I know that on windows, the above code works as expected

Using swingworker to adapt to swing will produce the same effect:

import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingWorker;

public class GetBoundsBug extends JFrame implements ActionListener {
  public static void main(String[] arg) {
    GetBoundsBug myJFrame = new GetBoundsBug();
    JButton myJButton = new JButton("Click here!");
    myJButton.addActionListener(myJFrame);
    myJFrame.setContentPane(myJButton);
    myJFrame.setSize(300,300);
    myJFrame.setVisible(true);
  }

  @Override
  public void actionPerformed(ActionEvent event) {
    SwingWorker<Void,Void> mySwingWorker = new SwingWorker<Void,Void>() {
      @Override
      public Void doInBackground() {
        Rectangle myRectangle = getBounds();
        myRectangle.y--;
        setBounds(myRectangle);
        myRectangle.y++;
        setBounds(myRectangle);
        return null;
      }
    };
    mySwingWorker.execute();
  }
}

Solution

Well, in the original bug database entry, this is marked "irreparable" and interpreted as a quirk in the window manager, not JDK

What window manager are you using?

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>