Java development GUI tutorial jframe listens for form size change events and jframe creates forms
- 2020-04-01 03:05:05
- OfStack
import java.awt.event.WindowEvent;
import java.awt.event.WindowStateListener;
import javax.swing.JFrame;
public class WinFrame extends JFrame {
public WinFrame(){
this.setName("Window Window state ");
this.setSize(300,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addWindowStateListener(new WindowStateListener () {
public void windowStateChanged(WindowEvent state) {
if(state.getNewState() == 1 || state.getNewState() == 7) {
System.out.println(" Window minimization ");
}else if(state.getNewState() == 0) {
System.out.println(" The window returns to its original state ");
}else if(state.getNewState() == 6) {
System.out.println(" Window maximization ");
}
}
});
this.setVisible(true);
}
public static void main(String[] args) {
new WinFrame();
}
}
Another small example of creating a form using JFrame
The Windows created with JFrame contain a title, a minimize button, a maximize button, and a close button, respectively
public class Test(){
public static void main(String[] args){
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JTextArea textArea = new JTextArea();
panel.setLayout(new GridLayout());
textArea.setText("test");
//Generates a scroll bar when the contents of the TextArea are too long
panel.add(new JScrollPane(textArea));
frame.add(panel);
frame.setSize(200,200);
frame.setVisible(true);
}
}