Jbutton (ActionListener) 可以启动静态方法吗?
Can a Jbutton (ActionListener) start a static Method?
我目前正在编写一个问答程序,巧合的是 "Jarvis"(钢铁侠 AI)。
该程序不会那么复杂,所以它只会回答几个问题,并会做一些简单的事情,比如在我写下特定句子时开始播放歌曲或打开资源管理器。
我与经验丰富的Java-程序员完全相反,
所以我把所有重要的代码都写进了 JButton-Actionlistener started 方法,
剩下的代码只是为了应用程序的设计 window.
我现在的问题是,我的方法,
public void actionPerformed(ActionEvent arg0)
由我的 JButton ActionListener 启动的只是一个 "public void" 方法,因此我的代码非常有限。
例如:对于某些事情你需要有一个
public static void main (String[]args)
方法,
但我不知道如何通过我的 actionlistener 激活这种方法,
因此,当我尝试使用我的 actionlistener 启动其中一个时,到处都是错误,甚至 qickfix 也从方法中删除了 "static"。
(如果您有任何提高效率的建议,请告诉我!)
但是,到目前为止,这是我的 "Program":
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);
}
}
此致
是的,你可以
myButton.addActionListener(e -> myStaticMethod());
祝你好运!
如上所述,您可以从静态和非静态上下文中调用静态方法。
但是,方法
public static void main (String[] args)
是您程序的入口点(上面的注释也指出了这一点),因此您不太可能希望从按钮的操作处理程序中调用它。当你以你的 JarvisOS class 作为目标启动 java 时(或从你的 IDE 启动 运行 时,它会被自动调用,我假设你使用它,因为你有快速修复)。
main 方法的签名是固定的,所以无论快速修复建议如何,都不要删除 static 关键字,因为那样会阻止您启动程序。
您需要更具体地说明另一个 "errors everywhere"。
我目前正在编写一个问答程序,巧合的是 "Jarvis"(钢铁侠 AI)。
该程序不会那么复杂,所以它只会回答几个问题,并会做一些简单的事情,比如在我写下特定句子时开始播放歌曲或打开资源管理器。
我与经验丰富的Java-程序员完全相反,
所以我把所有重要的代码都写进了 JButton-Actionlistener started 方法,
剩下的代码只是为了应用程序的设计 window.
我现在的问题是,我的方法,
public void actionPerformed(ActionEvent arg0)
由我的 JButton ActionListener 启动的只是一个 "public void" 方法,因此我的代码非常有限。
例如:对于某些事情你需要有一个
public static void main (String[]args)
方法,
但我不知道如何通过我的 actionlistener 激活这种方法,
因此,当我尝试使用我的 actionlistener 启动其中一个时,到处都是错误,甚至 qickfix 也从方法中删除了 "static"。
(如果您有任何提高效率的建议,请告诉我!)
但是,到目前为止,这是我的 "Program":
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);
}
}
此致
是的,你可以
myButton.addActionListener(e -> myStaticMethod());
祝你好运!
如上所述,您可以从静态和非静态上下文中调用静态方法。
但是,方法
public static void main (String[] args)
是您程序的入口点(上面的注释也指出了这一点),因此您不太可能希望从按钮的操作处理程序中调用它。当你以你的 JarvisOS class 作为目标启动 java 时(或从你的 IDE 启动 运行 时,它会被自动调用,我假设你使用它,因为你有快速修复)。
main 方法的签名是固定的,所以无论快速修复建议如何,都不要删除 static 关键字,因为那样会阻止您启动程序。
您需要更具体地说明另一个 "errors everywhere"。