尝试在单击按钮时显示 JOptionPane
Trying to display JOptionPane on button click
我试图在单击按钮时显示 JOptionPane
,但我一直收到错误消息,说我在构造函数中缺少 return 语句。任何帮助将非常感激。谢谢
到目前为止,这是我的代码。
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);
}
}
}
您在 main
的方法声明中使用了 Void
(大写 V)而不是 void
(小写 v)。应该是:
public static void main (String [] args){
JFrame frame = new lab9Part1();
}
我试图在单击按钮时显示 JOptionPane
,但我一直收到错误消息,说我在构造函数中缺少 return 语句。任何帮助将非常感激。谢谢
到目前为止,这是我的代码。
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);
}
}
}
您在 main
的方法声明中使用了 Void
(大写 V)而不是 void
(小写 v)。应该是:
public static void main (String [] args){
JFrame frame = new lab9Part1();
}