Debugging
The following program is supposed to display two buttons—Switch and Exit. When Switch is pressed, the two buttons switch their text (Switch becomes Exit and vice versa). When Exit is pressed, the program terminates. As written, the program has numerous syntax and semantic errors, some careless and some more serious. Debug the program and fix it so that it works correctly.
import java.awt.*; import javax.swing.*; public class Switch extends JFrame { private JButton switchButton; private JButton exitButton ; public Switch() // constructor { switchButton = new JButton("Switch"); exitButton = new JButton("Exit"); setTitle("Switch"); setBounds(0, 0, 300, 300); switchButton.addActionListener(new ButtonListener()); exitButton.addActionListener(newButtonListener()); JPanel buttonPanel = new JPanel(); buttonPanel.add(switchButton); // add buttons to panel buttonPanel.add(exitButton); add(buttonPanel,BorderLayout.CENTER); // add panel to the frame setVisible(true); } private class ButtonListener implements ActionListener // the listener { public void actionPerformed(ActionEvent e) // must implement this method { if (e.getSource() == switchButton) { if (switchButton.getText() = "switch") { switchButton.setText() = "exit"; exitButton.Text = "switch"; repaint(); // repaint the frame } else if (exitButton.text == "switch") // the source is exit Button { switchButton.text = "switch"; exitButton.text = "exit"; } else System.exit(0); } } public static void main(String [ ] args) { Switch frame = new Switch(); 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.