按下时我的JButton不起作用

时间:2014-01-28 17:41:42

标签: java button user-interface jbutton

我有一些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) {



        }

    }

}

非常感谢你的帮助!

4 个答案:

答案 0 :(得分:0)

您添加ActionListener的唯一按钮是runDemo,而当runDemo是源时,您不会执行任何操作。

答案 1 :(得分:0)

您必须将按钮监听器添加到它应该收听的所有按钮。目前,您只是将其添加到runDemo按钮

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

答案 2 :(得分:0)

您需要使用调试器来查看按下按钮是否正常工作。

您应该在那里放置一个断点并按照步骤进行操作。

这是一个eclipse调试器教程(http://www.vogella.com/tutorials/EclipseDebugging/article.html

此外,您似乎没有将听众附加到所有按钮上。

答案 3 :(得分:0)

除了@Behe提到的内容之外,您还可以为按钮设置动作命令,以便更容易找出按下的按钮:

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