ActionListener阻止程序运行?

时间:2015-12-30 16:46:53

标签: java swing actionlistener

好吧我在这里有两个问题。

  1. 在我将help.addActionListener(this)添加到程序之前,我的程序用户界面正常工作。我在使用静态方法时遇到了一些问题,因为Eclipse想在那里的某处使用main方法,而ActionListener并不喜欢使用静态方法。 (我当时正在public static void main(String args[])进行方法调用。)一旦我将方法调用移到构造函数或单独的方法,程序就不会执行代码中的任何内容。
  2.  JFrame screen; 
     JButton start, submit, help;
     JPanel UI,userWhite, passWhite;
     JLabel usrTxt, passTxt;
     JTextArea usrInput, passInput;
    
    static String[] strings = new String[2]; //See getInformation() method
    HELP runner = new HELP();
    
    public UserInterface()
    {
        run(); 
    }
    
    public void run()
    {
        uiSetup();
        displaySetup();
        inputSetup();
    }
    
    public static void main(String args[])
    {
        // Had to make every method static if I did the method call in here
        // which was fine until it came time to use "help.addActionListener(this)".
        // When I did use this method in a static context, I kept getting an error. 
    }
    
    
    public void uiSetup()
    {
        // This method sets up the initial interface which all other
        // elements within this program will be built off of
    
        screen = new JFrame("Bing Rewards Bot v.Development 1.0");
        screen.setVisible(true);
        screen.setSize(800, 600);
        screen.setResizable(false);
        screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        screen.getContentPane().setLayout(new BorderLayout());
    
        UI = new JPanel();
        UI.setVisible(true);
        UI.setLayout(null);
        UI.setBackground(Color.CYAN);
        screen.add(UI);
                                            // Buttons must me initialized here. If done in another method,
                                            // it can sometimes block JPanels from appearing. Fixed: 12/27/15
        start = new JButton("Start Bot");
        start.setVisible(true);
        start.setFocusable(false);
        start.setBounds(300,300,200,75);
        UI.add(start);
    
        submit = new JButton("Submit");
        submit.setVisible(true);
        submit.setFocusable(false);
        submit.setBounds(75,170,100,50);
        UI.add(submit);
    
        help = new JButton("HELP");
        help.setVisible(true);
        help.setFocusable(false);
        help.setBounds(355,500,100,50);
        help.setActionCommand("helpClicked");
        help.addActionListener(this);
        UI.add(help); 
    }
    
    
    public void displaySetup()
    {
        // This method sets up the interface text
    
        usrTxt = new JLabel("Bing/Microsoft Account Username");
        usrTxt.setFont(new Font("Monospaced", Font.BOLD, 16));
        usrTxt.setForeground(Color.BLACK);
        usrTxt.setBounds(0,0,310,20); // Adjusts Absolute Size
        usrTxt.setLocation(25,50); // Sets Location
        UI.add(usrTxt);
    
        passTxt = new JLabel("Password");
        passTxt.setFont(new Font("Monospaced", Font.BOLD, 16));
        passTxt.setForeground(Color.BLACK);
        passTxt.setBounds(0,0,310,20);
        passTxt.setLocation(25,100);
        UI.add(passTxt);
    }
    
    public void inputSetup()
    {
        // This method handles the User name and Password field setup
    
        //----- Adds White Space In JTextAreas ----------------------------
        userWhite = new JPanel();
        userWhite.setVisible(true);
        userWhite.setBackground(Color.WHITE);
        userWhite.setLocation(25,70);
        userWhite.setSize(200,25);
        UI.add(userWhite);
    
        passWhite = new JPanel();
        passWhite.setVisible(true);
        passWhite.setBackground(Color.WHITE);
        passWhite.setLocation(25,120);
        passWhite.setSize(200,25);
        UI.add(passWhite);
    
        //----------- JTextAreas ------------------------------------------
        usrInput = new JTextArea();
        usrInput.setBounds(0,0,200,18);
        usrInput.setLocation(25,75);
        usrInput.setBackground(Color.WHITE);
        usrInput.setForeground(Color.BLACK);
        UI.add(usrInput);
    
        passInput = new JTextArea();
        passInput.setBounds(0,0,200,18);
        passInput.setLocation(25,125);
        passInput.setBackground(Color.WHITE);
        passInput.setForeground(Color.BLACK);
        UI.add(passInput);
    }
    
    public void getInformation()
    {
        // This method gets the information from the
        // JTextAreas in the inputSetup method and
        // stores it into an array called "strings"
        // ("strings" is initialized in the class)
    
        strings[0] = usrInput.getText();
        strings[1] = passInput.getText();
    }
    
    public void actionPerformed(ActionEvent e){
        if (e.getActionCommand().equals("helpClicked")) 
        {
            UI.setVisible(false);
            runner.helpSetup(screen);
    
        }
    } 
    
    }
    

    点击该按钮后,计划是JPanel"用户界面":UI.setVisible(false),然后为其他班级创建Object将会有不同的JPanelsetVisible(true),然后将其添加到JFrame"屏幕"。如何在仍然使按钮工作的同时执行方法调用?

    1. 是否有人知道有关获取用户输入信息然后将其发送到网站,然后从网站获取数据以在java程序中显示的任何教程?我在这里和其他地方环顾四周,但它要么是我想要的,要么是不够的。

2 个答案:

答案 0 :(得分:2)

您的main方法应该用于创建和设置运行代码的主要对象,这里是UserInterface对象。由于main方法为空,因此程序会运行,但不显示任何内容,因为永远不会告诉JVM创建或显示任何GUI。因此,对于您来说,您需要创建UserInterface,然后调用其uiSetup方法以显示GUI:

public static void main(String args[]) {
    UserInterface userInterface = new UserInterface();
    userInterface.uiSetup();
}

或类似的东西。

要交换视图,请使用CardLayout(请查看教程的链接),这是专为更改视图而构建的布局。

其他问题:

  • 避免使用null布局和setBounds(...)。虽然这似乎是创建GUI的最简单方法,但如果您需要更改组件或向GUI添加新组件,它就不会成为维护的噩梦 - 经常发生。而是使用布局管理器。

例如,您的代码可能类似于:

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import javax.swing.*;

public class MyUserInterface extends JPanel {
    public static final String MAIN = "main";
    public static final String HELP = "help";
    private MainPanel mainPanel = new MainPanel(this);
    private HelpPanel helpPanel = new HelpPanel();

    private CardLayout cardLayout = new CardLayout();

    public MyUserInterface() {
        setLayout(cardLayout);
        add(mainPanel, MAIN);
        add(helpPanel, HELP);

    }

    public void showview(String key) {
        cardLayout.show(this, key);
    }

    private static void createAndShowGui() {
        JFrame frame = new JFrame("MyUserInterface");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new MyUserInterface());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}

class HelpPanel extends JPanel {
    public HelpPanel() {
        add(new JLabel("Help", SwingConstants.CENTER));
        setBorder(BorderFactory.createTitledBorder("Help Panel"));
    }
}

class MainPanel extends JPanel {
    private static final Color BG = Color.CYAN;
    private static final int PREF_W = 800;
    private static final int PREF_H = 600;
    private MyUserInterface myUserInterface;

    public MainPanel(MyUserInterface myUserInterface) {
        setBackground(BG);
        setBorder(BorderFactory.createTitledBorder("Main Panel"));
        this.myUserInterface = myUserInterface;
        add(new JButton(new HelpAction("Help")));
    }

    @Override
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet()) {
            return super.getPreferredSize();
        }
        return new Dimension(PREF_W, PREF_H);
    }

    private class HelpAction extends AbstractAction {
        public HelpAction(String name) {
            super(name);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            myUserInterface.showview(MyUserInterface.HELP);
        }
    }
}

答案 1 :(得分:0)

我猜你以前从另一个类开始你的程序,因为java总是会启动main方法。因此,目前没有任何反应,因为您的main方法是空的如果你会做这样的事情

public static void main(String args[])
{
    UserInterface userInterface = new UserInterface();
}

它应该启动程序。但是如果你在另一个类中启动程序,你可以安全地删除main方法,并告诉eclipse运行其他类main方法。

要使用ActionListener解决错误,您必须实现接口ActionListener,如

public class UserInterface implements ActionListener {
    //Your code...

    //The ActionListeners method
    @Override
    public void actionPerformed(ActionEvent e){
        if (e.getActionCommand().equals("helpClicked"))
        {
            UI.setVisible(false);
            runner.helpSetup(screen);

        }
    }
}

或者您也可以在构造函数中使用ActionListener的匿名实现:

help.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent actionEvent) {
        UserInterface.this.UI.setVisible(false);
        UserInterface.this.runner.helpSetup(screen);
    }
});