[英]Can a Jbutton (ActionListener) start a static Method?
我目前正在編寫一個問答程序,恰好叫做“ Jarvis”(鋼鐵俠AI)。
該程序不會那么復雜,因此它只回答一些問題,並會在寫特定句子時做一些簡單的事情,例如開始播放歌曲或打開瀏覽器。
我和經驗豐富的Java程序員完全相反,
所以我將所有重要的代碼寫到了JButton-Actionlistener啟動的Method中,
其余代碼僅用於設計應用程序窗口。
我現在的問題是我的方法
public void actionPerformed(ActionEvent arg0)
由我的JButton ActionListener啟動的方法只是一個“公共無效”方法,因此我的代碼中的圖像受到限制。
例如:對於某些事情,您需要
public static void main (String[]args)
方法,但我不知道如何通過我的動作監聽器激活這種方法,
因此,當我嘗試使用actionlistener啟動其中之一時,到處都有錯誤,甚至qickfix也從方法中刪除了“靜態”。
(如果您有任何提高效率的建議,請告訴我!)
但是,到目前為止,這是我的“程序”:
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);
}
}
最好的祝福
如上所述,您可以從靜態和非靜態上下文中調用靜態方法。
但是,方法
public static void main (String[] args)
是程序的入口點(也通過其上方的注釋指出),因此不太可能希望從按鈕的操作處理程序中調用它。 當您以JarvisOS類作為目標啟動Java時(或從您的IDE運行,由於具有快速修復的功能,我假設您使用它),它將自動被調用。
main方法的簽名是固定的,因此無論快速修復建議如何,都不要刪除static關鍵字,因為這會阻止您啟動程序。
您將需要更具體地了解其他“到處都是錯誤”。
是的,你可以做
myButton.addActionListener(e -> myStaticMethod());
祝好運!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.