Reputation: 1224
I use a JFrame
, which I'd like, as soon as the user resizes it from its original size (400x300) and the width
reaches a value, to do some things, let's call it a print command here. I use:
import javax.swing.JFrame;
public class DC extends JFrame {
public DC() {
setSize(400, 300);
setVisible(true);
if (getWidth() >= 1000) System.out.print("Thousand");
}
public static void main(String[] args) {
DC dc = new DC();
dc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
This works but only if I setSize(1000, 300)
from the beginning for example, and not when the user resizes the frame. I am knew to swing and such and I think this is not the way to approach this. Where should I look to fix this? Ty
Upvotes: 0
Views: 1234
Reputation: 593
Your new modified code (Will print Thousand as soon as the window's width size is greater than equal to 1000)----
import java.awt.Component; //import these 3 header files
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JFrame;
public class DC extends JFrame {
public DC() {
setSize(400, 300);
setVisible(true);
// if (getWidth() >= 1000) System.out.print("Thousand");
addComponentListener(new ComponentAdapter()
{
public void componentResized(ComponentEvent evt) {
Component c = (Component)evt.getSource();
if(c.getWidth()>=1000) //This will print Thousand
{
System.out.println("Thousand");
}
}
});
}
public static void main(String[] args) {
DC dc = new DC();
dc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Upvotes: 4