Reputation:
I'm facing a problem when the program runs console print the message of frame width and height with getSize()
method but when i increase the width of frame with mouse resize.Then i want to update that print statement with that increase width.I don't know how can i do that.
Code
public class Frame extends JFrame {
public Frame() {
super("Frame");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
}
Main Method
public class Main {
public static void main(String[] args) {
Frame frame = new Frame();
System.out.println(frame.getSize());
}
}
Upvotes: 0
Views: 370
Reputation: 4188
Add a ComponentListener
to your frame and let the program print the size inside the componentResized
method.
frame.addComponentListener(new ComponentListener() {
@Override
public void componentHidden(ComponentEvent arg0) {
}
@Override
public void componentMoved(ComponentEvent arg0) {
}
@Override
public void componentResized(ComponentEvent arg0) {
System.out.println(frame.getSize());
}
@Override
public void componentShown(ComponentEvent e) {
}
});
Or since we are overriding only one method we can use adapters
frame.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent arg0) {
System.out.println(frame.getSize());
}
});
Upvotes: 3