Home > Mobile >  Trying to display JOptionPane on button click
Trying to display JOptionPane on button click

Time:11-28

I am trying to make a JOptionPane display on button click but I keep getting an error saying I'm missing a return statement in my constructor. Any help would be really appreciated. Thanks This is my code so far.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;

public class lab9Part1 extends JFrame implements ActionListener {
    JButton button = new JButton("Show Message Dialog");
    JFrame box = new JFrame();

    public lab9Part1() {
        super("lab9Part1");
        Container c = getContentPane();
        button.addActionListener(this);
        c.add(button, BorderLayout.SOUTH);
        setVisible(true);
        setSize(500,500);
    }

    public static Void main (String [] args){
        JFrame frame = new lab9Part1();
    }

    public void actionPerformed(ActionEvent e){
        if (e.getSource()== button) {
            JOptionPane.showMessageDialog(box,"hello","This is Cal and this is my first message dialog", JOptionPane.INFORMATION_MESSAGE);
        }
    }
}

CodePudding user response:

You've used Void (uppercase V) instead of void (lowercase v) in your method declaration of main. It should be:

 public static void main (String [] args){

        JFrame frame = new lab9Part1();

 }
  • Related