我有一些JButton,我为它们设置了动作监听器,但是当按下它们时它们仍然不起作用。我希望他们在按下时调用方法。其中一些未实现(在按下时不应执行任何操作),但我所谈论的是if语句中不起作用的那些。请帮忙!

这是我的代码:

package com.robot;

import java.awt.AWTException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;

public class GUI extends JFrame implements Runnable {

//defines the panels
JPanel mainPanel;
JPanel labelPanel;
JPanel buttonPanel1;
JPanel buttonPanel2;
JPanel consolePanel;

//defines the label
JLabel title;

//defines the buttons
JButton runDemo;
JButton runLive;
JButton scan;
JButton findPatterns;
JButton cleanFolder;
JButton configureSettings;

//defines the console
JTextArea console;

//defines the line break
String newline = System.getProperty("line.separator");

//start of the constructor method for GUI
public GUI() {

    //makes the program unable to be resized
    this.setResizable(false);

    //allows the user to close the program with the x button
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //sets the title of the program
    this.setTitle("ROBOT Alpha Alfred Version 3.0");

    //creates panels to hold the elements of the GUI
    mainPanel = new JPanel();
    labelPanel = new JPanel();
    buttonPanel1 = new JPanel();
    buttonPanel2 = new JPanel();
    consolePanel = new JPanel();

    //creates label
    title = new JLabel("Robotically Operated Binary Options Trader");

    //creates buttons
    runDemo = new JButton("Run Demo");
    runLive = new JButton("Run Live");
    scan = new JButton("Scan Market");
    findPatterns = new JButton("Find Patterns");
    cleanFolder = new JButton("Clean Up Folder");
    configureSettings = new JButton("Configure Settings");

    //defines button listener objects
    ButtonListener buttonListener = new ButtonListener();

    //adds buttons to button listeners
    runDemo.addActionListener(buttonListener);

    //creates the console
    console = new JTextArea(6, 40);

    //sets the default text of the console
    console.setText("----------------------- ROBOT Console -----------------------" + newline);

    //makes the console unable to be edited
    console.setEditable(false);

    //sets the line wrapping of the console
    console.setLineWrap(true);
    console.setWrapStyleWord(true);

    //creates scroll bars
    JScrollPane scrollBar = new JScrollPane(console);
    scrollBar.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    scrollBar.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);

    //adds label to the label panel
    labelPanel.add(title);

    //adds buttons to the button panel
    buttonPanel1.add(runDemo);
    buttonPanel1.add(runLive);
    buttonPanel2.add(scan);
    buttonPanel2.add(findPatterns);
    buttonPanel2.add(cleanFolder);
    buttonPanel2.add(configureSettings);

    //adds the console to the console panel
    consolePanel.add(scrollBar);

    //adds panels to the main panel
    mainPanel.add(labelPanel);
    mainPanel.add(buttonPanel1);
    mainPanel.add(buttonPanel2);
    mainPanel.add(consolePanel);

    //adds the main panel to the frame
    this.add(mainPanel);

    //packs the GUI
    this.pack();

    //sizes the GUI
    this.setSize(600, 400);

    //centers the GUI
    this.setLocationRelativeTo(null);

    //sets the GUI to be visible
    this.setVisible(true);

}

public void run() {

}

public void add(String string) {
    console.append(string + newline);
}

//implement listeners
private class ButtonListener implements ActionListener {

    public void actionPerformed(ActionEvent e) {

        if(e.getSource() == runDemo) {



        } else if(e.getSource() == runLive) {



        } else if(e.getSource() == scan) {

            try {
                ScanMarket.scanMarket();
            } catch (IOException e1) {
                e1.printStackTrace();
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            } catch (AWTException e1) {
                e1.printStackTrace();
            }

        } else if(e.getSource() == findPatterns) {

            try {
                FindPattern.findPattern("Images");
            } catch (AWTException e1) {
                e1.printStackTrace();
            } catch (IOException e1) {
                e1.printStackTrace();
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }

        } else if(e.getSource() == cleanFolder) {

            AddNeededFiles.addNeededFiles();

        } else if(e.getSource() == configureSettings) {



        }

    }

}

}


这是动作侦听器类,以便您可以更仔细地查看它:

    //implement listeners
private class ButtonListener implements ActionListener {

    public void actionPerformed(ActionEvent e) {

        if(e.getSource() == runDemo) {



        } else if(e.getSource() == runLive) {



        } else if(e.getSource() == scan) {

            try {
                ScanMarket.scanMarket();
            } catch (IOException e1) {
                e1.printStackTrace();
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            } catch (AWTException e1) {
                e1.printStackTrace();
            }

        } else if(e.getSource() == findPatterns) {

            try {
                FindPattern.findPattern("Images");
            } catch (AWTException e1) {
                e1.printStackTrace();
            } catch (IOException e1) {
                e1.printStackTrace();
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }

        } else if(e.getSource() == cleanFolder) {

            AddNeededFiles.addNeededFiles();

        } else if(e.getSource() == configureSettings) {



        }

    }

}


非常感谢你的帮助!

最佳答案

您必须将按钮侦听器添加到它应该侦听的所有按钮。当前,您仅将其添加到您的runDemo按钮

 //adds buttons to button listeners
runDemo.addActionListener(buttonListener);
runLive.addActionListener(buttonListener);
scan.addActionListener(buttonListener);
findPatterns.addActionListener(buttonListener);
cleanFolder.addActionListener(buttonListener);
configureSettings.addActionListener(buttonListener);

关于java - 我的JButton在按下时不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21412838/

10-13 09:03