简体   繁体   English

Jbutton(ActionListener)可以启动静态方法吗?

[英]Can a Jbutton (ActionListener) start a static Method?

Im currently coding a Q&A Program, coincidentally called "Jarvis" (Iron Man AI). 我目前正在编写一个问答程序,恰好叫做“ Jarvis”(钢铁侠AI)。
The program isnt going to be that complex, so it just answers a few questions and will do simple things like start a song or open the explorer when I write a specific sentence. 该程序不会那么复杂,因此它只回答一些问题,并会在写特定句子时做一些简单的事情,例如开始播放歌曲或打开浏览器。
I'm the complete opposite of a experienced Java-Programmer, 我和经验丰富的Java程序员完全相反,
so I wrote all the important code into a JButton-Actionlistener started Method, 所以我将所有重要的代码写到了JButton-Actionlistener启动的Method中,
the rest of the code is just for design of the application window. 其余代码仅用于设计应用程序窗口。
My problem right now is, that my Method, 我现在的问题是我的方法
public void actionPerformed(ActionEvent arg0)
which is started by my JButton ActionListener is just a "public void" Method, so that Im verry limited in my Code. 由我的JButton ActionListener启动的方法只是一个“公共无效”方法,因此我的代码中的图像受到限制。
For example: for some things you need to have a 例如:对于某些事情,您需要
public static void main (String[]args)
method, but i dont know how to activate such a method by my actionlistener, 方法,但我不知道如何通过我的动作监听器激活这种方法,
so when I try to start one of these with my actionlistener, there are errors everywhere and even the qickfix removed the "static" from the method. 因此,当我尝试使用actionlistener启动其中之一时,到处都有错误,甚至qickfix也从方法中删除了“静态”。
(If you have any suggestions of making it more efficient, please let me know!) (如果您有任何提高效率的建议,请告诉我!)

However, here is my "Program" so far: 但是,到目前为止,这是我的“程序”:

import java.awt.EventQueue;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.Font;
import java.awt.SystemColor;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.util.Random;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import java.awt.Toolkit;
import javax.swing.ImageIcon;
import java.awt.Color;
import javazoom.jl.player.Player;
import java.io.FileInputStream;


public class JarvisOS {

    JFrame JarvisOS;
    private JTextField Input;
    private JTextField Output;
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    JarvisOS window = new JarvisOS();
                    window.JarvisOS.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public JarvisOS() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        JarvisOS = new JFrame();
        JarvisOS.setAlwaysOnTop(true);
        JarvisOS.getContentPane().setBackground(SystemColor.window);
        JarvisOS.setIconImage(Toolkit.getDefaultToolkit().getImage("E:\\Programme\\Eclipse\\JarvisOS\\JarvisICO.png"));
        JarvisOS.setTitle("JarvisOS");
        JarvisOS.setBounds(100, 100, 1932, 1368);
        JarvisOS.setExtendedState(JFrame.MAXIMIZED_BOTH); 
        JarvisOS.setVisible(true);
        JarvisOS.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton Send = new JButton("");
        Send.setIcon(new ImageIcon("E:\\Programme\\Eclipse\\JarvisOS\\SendButton.png"));
        Send.setRolloverIcon(new ImageIcon("E:\\Programme\\Eclipse\\JarvisOS\\SendButton Activated.png"));
        Send.setBounds(1092, 481, 130, 130);
        Send.setForeground(SystemColor.window);
        Send.setBorderPainted(false);
        JarvisOS.getRootPane().setDefaultButton(Send);
        Send.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) { 


                String Text = Input.getText();


                if(Text.equalsIgnoreCase("Hello"))  {
                     Output.setText("Hi");  }

                else if(Text.equalsIgnoreCase("What are you doing"))    {
                     Output.setText("Nothing Special"); }           

                else if(Text.equalsIgnoreCase("Do you like me?"))   {
                     Output.setText("Yes"); }   



                else{
                     Output.setText("Cant understand that!");}

            }
        });
        JarvisOS.getContentPane().setLayout(null);
        Send.setFont(new Font("Arial Black", Font.PLAIN, 16));
        Send.setBackground(Color.BLACK);
        JarvisOS.getContentPane().add(Send);

        JButton RecVoice = new JButton("");
        RecVoice.setToolTipText("Record Voice");
        RecVoice.setSelectedIcon(new ImageIcon("E:\\Programme\\Eclipse\\JarvisOS\\JarvisOSLogo.png"));
        RecVoice.setBackground(Color.BLACK);
        RecVoice.setIcon(new ImageIcon("E:\\Programme\\Eclipse\\JarvisOS\\Record Button.png"));
        RecVoice.setRolloverIcon(new ImageIcon("E:\\Programme\\Eclipse\\JarvisOS\\Record Button Animated.gif"));
        RecVoice.setBounds(1598, 698, 296, 298);
        RecVoice.setBorderPainted(false);
        JarvisOS.getContentPane().add(RecVoice);

        Input = new JTextField();
        Input.setBorder(javax.swing.BorderFactory.createEmptyBorder());
        Input.setBounds(230, 445, 824, 68);
        Input.setForeground(SystemColor.window);
        Input.setToolTipText("");
        Input.setFont(new Font("Arial", Font.BOLD, 40));
        Input.setBackground(Color.BLACK);
        JarvisOS.getContentPane().add(Input);
        Input.setColumns(10);

        Output = new JTextField();
        Output.setBorder(javax.swing.BorderFactory.createEmptyBorder());
        Output.setBounds(230, 584, 824, 68);
        Output.setForeground(SystemColor.window);
        Output.setEditable(false);
        Output.setFont(new Font("Arial", Font.BOLD, 30));
        Output.setColumns(10);
        Output.setBackground(Color.BLACK);
        JarvisOS.getContentPane().add(Output);

        JLabel Circle = new JLabel("");
        Circle.setIcon(new ImageIcon("E:\\Programme\\Eclipse\\JarvisOS\\Circle.gif"));
        Circle.setBounds(1756, 43, 150, 150);
        JarvisOS.getContentPane().add(Circle);

        JLabel JarvisBackground = new JLabel("");
        JarvisBackground.setBounds(0, 0, 1920, 1080);
        JarvisBackground.setIcon(new ImageIcon("E:\\Programme\\Eclipse\\JarvisOS\\Jarvis Background.png"));
        JarvisOS.getContentPane().add(JarvisBackground);
    }
}

Best regards 最好的祝福

As noted above, you can call a static method from both a static and non-static context. 如上所述,您可以从静态和非静态上下文中调用静态方法。

However, the method 但是,方法

public static void main (String[] args)

is the entry point to your program (as also noted by the comment above it), so it is unlikely you would want to call that from the action handler of your button. 是程序的入口点(也通过其上方的注释指出),因此不太可能希望从按钮的操作处理程序中调用它。 It will be called automatically when you launch java with your JarvisOS class as target (or run from your IDE, which I assume you use since you have quick fixes). 当您以JarvisOS类作为目标启动Java时(或从您的IDE运行,由于具有快速修复的功能,我假设您使用它),它将自动被调用。

The signature of the main method is fixed, so whatever the quick fix suggests, do not remove the static keyword as that will prevent you from starting your program. main方法的签名是固定的,因此无论快速修复建议如何,都不要删除static关键字,因为这会阻止您启动程序。

You will need to be more specific about the other "errors everywhere". 您将需要更具体地了解其他“到处都是错误”。

Yes, you could do 是的,你可以做

myButton.addActionListener(e -> myStaticMethod());

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法从静态方法运行Javascript - Can not run Javascript from static method Javascript子类中的实例方法可以将其称为父方法静态方法吗? - Can a instance method in a Javascript subclass call it's parents static method? 触发JavaScript actionListener时如何在模型内运行方法? - How to run a method inside a Model when a JavaScript actionListener is triggered? 您可以在TypeScript的本地方法中访问本地静态变量吗? - Can you access local static variables within local method in TypeScript? 无法在 JavaScript 父类中调用静态方法 - Can't call static method in JavaScript parent class 我可以使用原型向外部类添加非静态方法吗? - can I add non static method to external class using prototypes? 如何注释该参数是 class 并具有某些 static 方法? - How can I annotate that argument is a class and has certain static method? 是否可以从导入的对象定义ES6静态方法? - Can I define an ES6 static method from an imported object? 您如何判断js类方法是否是静态的? - How can you tell if a js class method is static or not? 无法从客户端应用程序引用插件中定义的静态方法 - Can't reference static method defined in plugin from an client application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM