简体   繁体   中英

My JButtons don't work when pressed

I have some JButtons that I set up action listeners for but they still don't work when they are pressed. I want them to call methods when pressed. Some of them are unimplemented (aren't supposed to do anything when pressed) but I'm talking about the ones that are in the if statement not working. Please help!

Here is my code:

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) {



        }

    }

}

}

Here is the action listener class so that you can look at it more closely:

    //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) {



        }

    }

}

Thank you so much for your help!

添加ActionListener的唯一按钮是runDemo,当runDemo为源代码时,您什么也不做。

You have to add your button listener to all your buttons it should listen to. Currently you are only adding it to your runDemo button

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

You need to use the debugger to see if the button press is working or not.

You should place a break point there and follow step by step.

Here is an eclipse debugger tutorial ( http://www.vogella.com/tutorials/EclipseDebugging/article.html )

Also it seems that you are not attaching the listeners to all your buttons.

In addition to what @Behe mentioned, you can also set an action command for your buttons to make it easier to figure out which button was pressed:

runDemo = new JButton("Run Demo");
runDemo.addActionListener(new buttonListener());
runDemo.setActionCommand("rundemo");
runLive = new JButton("Run Live");
runLive.addActionListener(new buttonListener());
runLive.setActionCommand("runlive");
scan = new JButton("Scan Market");
scan.addActionListener(new buttonListener());
scan.setActionCommand("scan");
...
public void actionPerformed(ActionEvent e) {
   String action = e.getActionCommand();    // Get the action.

   if ( action.equalsIgnoreCase("rundemo") ) {  // Run Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM