java - 我的 JButtons 按下时不起作用

标签 java button user-interface jbutton

我有一些 JButton,我为其设置了 Action 监听器,但按下它们时它们仍然不起作用。我希望他们在按下时调用方法。其中一些尚未实现(按下时不应该执行任何操作),但我正在谈论 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) {



        }

    }

}

}

这是 Action 监听器类,以便您可以更仔细地查看它:

    //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 - 我的 JButtons 按下时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21412838/

相关文章:

java - 帮助 Java 日历——它给出了一些奇怪的输出

cocoa 圆形取消按钮

html - 调整窗口大小时如何使网站不发生任何变化?

java - 何时使用 Java GUI

python - 在 Traitsui GUI 之外更改属性

java - com.google.gwt.dev.jjs.InternalCompilerException : Unexpected error during visit

java - SPRING4 : Failed to read candidate component class CouchbaseConfig. 类

java - Android多播 - 有时接收阻塞线程

wpf - WPF 中的切换按钮

java - 编辑另一个类的对象?