2016-05-21 4 views
0

Imは現在、Qプログラムをコーディングしています。同時に、「Jarvis」(Iron Man AI)とも呼ばれます。
プログラムはそれほど複雑ではないので、いくつかの質問に答えるだけで、特定の文章を書くときに曲を開始したりエクスプローラを開いたりといった簡単なことができます。
私は経験豊富なJava-Programmerの完全な反対です。
JButton-ActionListener起動メソッドにすべての重要なコードを書きました。
残りのコードはアプリケーションウィンドウの設計用です。
今私の問題は、私のメソッド、
public void actionPerformed(ActionEvent arg0)
私のJButton ActionListenerで起動されているので、私のコードではIm verryが "public void"メソッドに制限されています。たとえば
:あなたは
public static void main (String[]args)
方法、 を持っている必要がありますが、私は私のActionListenerにより、このような方法を有効にする方法を知らないいくつかのもののために、
ので、私は私のActionListenerでこれらのいずれかを起動しようとすると、どこでもエラーがあり、qickfixでもメソッドから "静的"が取り除かれました。
(あなたはそれがより効率的に行うことの提案があれば、私に知らせてください!)

しかし、ここではこれまでのところ、私の「プログラム」である:Jbutton(ActionListener)は静的メソッドを開始できますか?

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); 
    } 
} 

敬具

+0

'' Jbutton(ActionListener)は静的メソッドを起動できますか? " - 答え、"はい " –

+0

また、すべての質問と回答が表示されるサイトであることをご理解ください。この質問を今後のユーザーに役立てるため、**すべての**関連情報はリンク先ではなく質問に記載してください。また、ボランティアは皆ボランティアであり、できるだけ簡単にあなたの質問を理解していただきたいと思います。 –

+0

リンクを削除し、それを含むコードに置き換えました。 –

答えて

1

前述のとおり、静的メソッドを静的コンテキストと非静的コンテキストの両方から呼び出すことができます。 (また、それ以上のコメントで述べたように)しかし

、方法

public static void main (String[] args) 

は、プログラムへのエントリポイントであるので、あなたがあなたのボタンのアクションハンドラからそれを呼び出すしたいそうです。 JarvisOSクラスをターゲットにしてJavaを起動すると(またはIDEから実行すると、自動的に呼び出されます。

メインメソッドのシグネチャは固定されていますので、クイックフィックスの示唆どおり、静的キーワードを削除しないでください。そうしないと、プログラムが起動しなくなります。

他の「どこでもエラー」について詳しく説明する必要があります。

0

はい、あなたはできるか

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

幸運を祈る!

関連する問題