java - 如何制作带有类的 if 语句(java)

标签 java

我想返回主菜单,但我不想制作一百万个返回按钮,而是想做一个 if 语句,该语句表示如果类方法=所有这些类,它将返回主菜单,我该怎么做那?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Container;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.event.*;


public class How_wants_to_be_a_millionaire extends JFrame implements ActionListener
{
    JFrame start_Frame, help_Frame, quit_Frame, bstart;
    Container bstartCon, helpCon, thisContainer3, thisContainer4, thisContainer5, thisContainer6;
    JPanel centerPanel, southPanel, northPanel, midPanel, topPanel, downPanel, quitup, quitdown, donateup, donatedown, helptop, helpdown;
    JButton start_button, help_button, quit_button, extra_button, return_bs;
    String L1 = "Welcome to the Game!";
    String L2 = "This a multiple choise game, player would have a question and 4 choises, only one is the correct one";
    String L3 = "When players reache a certent amount of money, they could choose to leave or continue playing";
    String L4 = "When players choose to continue playing,";
    String L5 = "The money beyond that point will not be saved until it reaches the next check point";
    String L6 = "Player have 3 assist that could help, they could only used once, they are 50:50, Phone assist and addience assist";
    String L7 = "50:50 eliminates 2 false answers, Phone assist is to call anyone in 30 sec to help you,";
    String L8 = "Addience assist is to perform a vote live to give a preferce of what people think the answer is";
    JLabel help_Line = new JLabel ("<html>" + L1 + "<br>" + L2 + "<br>" + L3 + "<br>" + L4 + "<br>" + L5 + "<br>" + L6 + "<br>" + L7 + "<br>" + L8 + "</html>");
    Font font = (new Font ("Font", Font.PLAIN, 14));


    public void before_we_start_menu ()
    {
        bstart = new JFrame ("How Wants to be a millionaire?(Guide Menu)");
        bstart.setBounds (100, 100, 450, 150);
        bstartCon = bstart.getContentPane ();
        bstartCon.setLayout (new BorderLayout ());
        midPanel = new JPanel ();
        midPanel.setBackground (Color.BLACK);
        topPanel = new JPanel ();
        topPanel.setBackground (Color.BLACK);
        downPanel = new JPanel ();
        downPanel.setBackground (Color.BLACK);
        bstartCon.add (midPanel, "Center");
        bstartCon.add (downPanel, "South");
        bstartCon.add (topPanel, "North");

        start_button = new JButton ("Start");
        start_button.addActionListener (this);
        topPanel.add (start_button);

        help_button = new JButton ("Help/Rules");
        help_button.addActionListener (this);
        midPanel.add (help_button);

        extra_button = new JButton ("Extra");
        extra_button.addActionListener (this);
        midPanel.add (extra_button);

        quit_button = new JButton ("Quit");
        quit_button.addActionListener (this);
        downPanel.add (quit_button);

        bstart.setVisible (true);
        bstart.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    }


    public void help ()
    {
        help_Frame = new JFrame ("Help/Rules");
        help_Frame.setBounds (350, 350, 500, 350);
        helpCon = help_Frame.getContentPane ();
        helpCon.setLayout (new BorderLayout ());
        helpdown = new JPanel ();
        helpCon.add (helpdown, "South");
        help_Line.setFont (font);
        helpCon.add (help_Line);
        return_bs = new JButton ("Back");
        return_bs.setFont (font);
        return_bs.setForeground (new Color (0xffffdd));
        helpdown.add (return_bs);
        return_bs.addActionListener (this);
        help_Frame.setVisible (true);
        help_Frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    }



    public void actionPerformed (ActionEvent event)
    {
        if (event.getSource () == help_button)
        {
            help ();
            bstart.setVisible (false);
        }
        if (event.getSource () == quit_button)
        {
            System.exit (0);
        }
        if (event.getSource () == return_bs)
        {
            before_we_start_menu ();
            if (class=help ())//This is where i wanted help
                help.setVisible (false);
        }
    }





    public static void main (String[] args)
    {
        How_wants_to_be_a_millionaire c = new How_wants_to_be_a_millionaire ();
        c.before_we_start_menu ();
    }
}

最佳答案

假设我的问题是正确的,并且您想为给定类型的所有类提供特定的功能 - 您可能可以让这些类实现“标记”接口(interface),然后让您的代码检查调用类是否是该类的实例市场。

关于java - 如何制作带有类的 if 语句(java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28008515/

相关文章:

java - jsf-api 和 jsf-impl 有什么关系?

java - 使用java将文件从本地窗口机器复制到远程窗口机器

java - 从实例创建子类来重写一些方法?

Java XOR 运算符问题

java - 如何使 Java 通用方法静态?

JavaFX 动画恢复到原始状态

java.lang.UnsatisfiedLinkError,mach-o 但 Mac10.6.2 上的架构错误

java - 使用 Java 且没有第 3 方框架(iOS 客户端)的谷歌应用引擎自定义身份验证 - 设计合理吗?

java - java 列表的排列

java - 聊天服务器写入关闭的套接字