gpt4 book ai didi

java - ActionListener 阻止程序运行?

转载 作者:行者123 更新时间:2023-12-02 04:04:09 25 4
gpt4 key购买 nike

好的,我有两个问题。

  1. 在添加 help.addActionListener(this) 之前,我的程序 UI 已经正常工作了进入我的程序。我在静态方法方面遇到了一些问题,因为 Eclipse 想要其中的某个 main 方法,而 ActionListener不喜欢使用静态方法。 (当时我正在 public static void main(String args[]) 中进行方法调用。)一旦我将方法调用移至构造函数或单独的方法,程序将不会执行代码中的任何内容。

 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到了不同的类(class),那里会有不同的JPanelsetVisible(true) ,然后将其添加到 JFrame “屏幕”。如何在让按钮正常工作的同时执行方法调用?

  • 有谁知道有关获取用户输入的信息然后将其发送到网站,然后从网站获取数据以在 java 程序中显示它的教程吗?我环顾过这里和其他地方,但它要么满足了我正在寻找的大部分内容,要么还不够。
  • 最佳答案

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

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

    或类似的东西。

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

    其他问题:

    • 避免空布局和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);
    }
    }
    }

    关于java - ActionListener 阻止程序运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34533573/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com