Debug the following program: public class Test { static JFrame jframe = new JFra
ID: 3804216 • Letter: D
Question
Debug the following program:
public class Test {
static JFrame jframe = new JFrame("JFRAME Sample");
public static void constructFrame() {
jframe.setSize(500, 400);
jframe.getContentPane().setLayout(new FlowLayout());
WindowListener wListener = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
jframe.addWindowListener(wListener);
}
public static void main(String[] args) {
setupJFrame();
JButton jButton = new JButton("Press the Button");
jframe.getContentPane().add(jButton);
jframe.setVisible(true);
}
}
Explanation / Answer
You have written method as constructFrame().
In main method you are calling method setupJFrame() which is not defined. Change that method to constructFrame()
Your program will run.
I am pasting the class for your reference:
package com.test.example;
import java.awt.FlowLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Test {
static JFrame jframe = new JFrame("JFRAME Sample");
public static void constructFrame() {
jframe.setSize(500, 400);
jframe.getContentPane().setLayout(new FlowLayout());
WindowAdapter wListener = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
jframe.addWindowListener(wListener);
}
public static void main(String[] args) {
constructFrame();
JButton jButton = new JButton("Press the Button");
jframe.getContentPane().add(jButton);
jframe.setVisible(true);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.