问题
I need to have a JFrame, which when i change the window taskbar size or position, the frame shouldn't adjust itself. Which method of the JFrame will be called exactly on changing the taskbar size and position? Which method will I be required to override?
To put it in more clear words, By default the instance of JFrame will adjust its height and width by itself when I change the size and position of the window taskbar. But my JFrame shouldn't respond when I extend the window taskbar or when I change the taskbar from horizontal to vertical. It should remain in its default state.
回答1:
By using setResizable(false);
Example:
import javax.swing.JFrame;
public class JavaApplication7 extends JFrame{
public static void main(String[] args) {
JFrame frame = new JFrame("EG");
frame.setVisible(true);
frame.setResizable(false);
}
}
That creates a small JFrame named "EG" that can't be resized.
回答2:
I don't think it's possible to prevent it from being moved. It doesn't seem to call setBounds or setLocation or setSize, but rather it moves the window directly at the OS level. The best you can probably do is detect bounds changes with a ComponentListener and then immediately put the window back to where you want it.
final Rectangle fixedPosition = new Rectangle(...);
frame.setBounds(fixedPosition);
frame.addComponentListener(new ComponentListener() {
public void componentMoved(ComponentEvent e) {
if (!frame.getBounds().equals(fixedPosition)) {
frame.setBounds(fixedPosition);
}
}
public void componentResized(ComponentEvent e) {
componentMoved(e);
}
public void componentShown(ComponentEvent e) {}
public void componentHidden(ComponentEvent e) {}
});
来源:https://stackoverflow.com/questions/19606700/to-have-a-jframe-without-adjusting-for-taskbar-change