Playing Compiler
The following program is supposed to display an image on one panel, and the word “huh?” on another, without using layout managers. In fact, the program contains several syntax errors. When the errors are corrected, the program displays an empty frame. Find and correct the syntax errors. Explain why the program displays an empty frame, and then fi x the program. Hint: The panels and buttons did not call the setBounds(…) method. To test your solution, use an image of your choice stored in a fi le called test.jpg.
import javax.swing.*;
import java.awt.*;
public class NoLayoutManagers extends JFrame { public NoLayoutManagers() { super("No Layout Managers"); setLayout(null); // no layout manager setBounds(0, 0, 300, 300); // for the frame JPanel panel1 = new JPanel (null); // no layout manager for the panel JButton picture = new JButton(new ImageIcon("test.jpg")); picture.setBounds(125, 125, 50, 50); panel1.add(picture); panel1.setResizable(false); setResizable(false); JPanel panel2 = new JPanel (null); // no layout manager for the panel JButton button = new JButton(new String “huh?”); // add the word “huh?” panel.add(button); panel.setBounds(20, 20, 60, 60); add(panel); } public static void main(String[] args) { JFrame frame = new NoLayoutManager(); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
We need at least 10 more requests to produce the solution.
0 / 10 have requested this problem solution
The more requests, the faster the answer.