【问题标题】:Java: How to open JOptionPane from a JButtonJava:如何从 JButton 打开 JOptionPane
【发布时间】: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


    【解决方案1】:

    你可以这样做,

     if ("Help".equals(command)) {
    //  helpPage2 B=new helpPage2();
          JOptionPane.showMessageDialog(null, "HELPPPPP PAGEE","Help", JOptionPane.INFORMATION_MESSAGE); // Create Information Message  
    }
    

    在方法中可以调用对话框。

    如果您有任何疑问,请告诉我。

    【讨论】:

    • 哦,我明白了,所以这样我什至不需要创建一个新类。非常感谢您的帮助。
    • 是的。您无需为 JOptionPane 创建新类。注意:如果你想创建自定义JOptionPane,那么你只需要创建。
    • 非常感谢。 java新手这个网站真的很有帮助。
    【解决方案2】:

    在 helpPage2 中使用构造函数而不是 main 方法

    【讨论】:

    • 构造函数是“public helpPage2 () {...code...}”,在类初始化时调用
    • 感谢您的帮助,不过其他人有一个非常简单的解决方案。
    • 我不知道您是否想在该课程中添加其他内容,所以我尽量保持开放。
    • 哦,我只是想打开消息框,我会记住你告诉我的以备将来使用。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2012-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多