【发布时间】:2023-03-27 14:36:01
【问题描述】:
我有一个程序,它有许多打开 jframes 的 JButton,我希望我的帮助按钮打开一个 JoptionPane 消息框,但每次我点击帮助按钮时都没有任何反应。
//Main Menu
import javax.swing.*;
// Create Main Menu page
import java.awt.event.*;
import java.awt. *;
import java.io.*;
public class mainMenu implements ActionListener // create new class mainMenu
{
JFrame Start=new JFrame("Main Menu"); // name frame Main Menu
JButton Search ;
JButton Create; // create new buttons
JButton Delete;
JButton Help;
{
Search=new JButton("Search for Existing Contact");
Create=new JButton("Create a New Contact"); // set name of all buttons
Delete=new JButton("Delete a Contact");
Help=new JButton("Help");
Search.setActionCommand("Search");
Create.setActionCommand("Create");
Delete.setActionCommand("Delete");
Help.setActionCommand("Help");
Start.setSize(510,600); // set size of frame
Start.add(new JLabel(new ImageIcon("mainMenuBG.jpg"))); // add background
Start.setVisible(true);
Start.setLayout(null);
Start.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
Start.getContentPane().add(Search);
Start.getContentPane().add(Create); // add buttons to frame
Start.getContentPane().add(Delete);
Start.getContentPane().add(Help);
Search.setBounds(100,25,295,88);
Create.setBounds(100,150,295,88); // set size of buttons
Delete.setBounds(100,275,295,88);
Help.setBounds(100,400,295,88);
Search.addActionListener(this);
Create.addActionListener(this);
Delete.addActionListener(this);
Help.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if ("Search".equals(command)) {
Start.dispose();
LTS B=new LTS();
}
else if ("Create".equals(command)) {
Start.dispose();
LTS B=new LTS();
}
else if ("Delete".equals(command)) {
Start.dispose();
LTS B=new LTS();
}
else if ("Help".equals(command)) {
helpPage2 B=new helpPage2();
}
}
public static void main(String ag[])
{
mainMenu B=new mainMenu();
}
}
helpPage2 应该会打开消息框
// Help Page 2
import javax.swing.JOptionPane;
public class helpPage2 { // Create a new class helpPage
public static void main(String[] args) {
JOptionPane.showMessageDialog(null, "HELPPPPP PAGEE","Help", JOptionPane.INFORMATION_MESSAGE); // Create Information Message
}
}
不确定我错过了什么,非常感谢一些指导,其他按钮也可以工作,只是 helpPage2 没有。 谢谢你。
【问题讨论】:
标签: java jbutton joptionpane messagebox jgrasp