在使用这样的 Jbutton 设置单击 "Next" 按钮后,我将如何执行操作?

How would I go about preforming actoins after "Next" button is clicked with a Jbutton setup like this?

我使用 javax.swing 找到了这个漂亮的双按钮界面,我将其用于我的程序,该程序会询问一系列问题。我已经设置好了问题框,但我需要让它在这个界面之后进入第二个界面。

所以我希望下一个按钮在按下时执行一段代码。这是我的代码:

package test;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.*;

public class SwingJPanelDemo extends JFrame {
String letter = "apple";
private JLabel LetterTest = new JLabel(Main.text);
private JButton NextButton = new JButton("Next");
private JButton NoButton = new JButton("No");

public SwingJPanelDemo() {
    super("Is This Your Letter?");

    // create a new panel with GridBagLayout manager
    JPanel newPanel = new JPanel(new GridBagLayout());

    GridBagConstraints constraints = new GridBagConstraints();
    constraints.anchor = GridBagConstraints.WEST;
    constraints.insets = new Insets(10, 10, 10, 10);

    // add components to the panel
    constraints.gridx = 0;
    constraints.gridy = 0;     
    newPanel.add(LetterTest, constraints);

    constraints.gridx = 0;
    constraints.gridy = 1;
    constraints.gridwidth = 1;
    constraints.anchor = GridBagConstraints.CENTER;
    newPanel.add(NextButton, constraints);

    constraints.gridx = 0;
    constraints.gridy = 3;
    constraints.gridwidth = 1;
    constraints.anchor = GridBagConstraints.CENTER;
    newPanel.add(NoButton, constraints);

    // set border for the panel
    newPanel.setBorder(BorderFactory.createTitledBorder(
            BorderFactory.createEtchedBorder(), "Is This Your Letter?"));

    // add the panel to this frame
    add(newPanel);

    pack();
    setLocationRelativeTo(null);
}

public static void main(String[] args) {
    // set look and feel to the system look and feel
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new SwingJPanelDemo().setVisible(true);
        }
    });
}
} 

当使用这种界面设置时,按下按钮后如何执行操作?

顺便说一句:如果有任何您认为错误的事情或可以做得更好的帮助,我们总是很感激,但不会请求

您需要为 nextButton 添加一个 ActionListener。至少有两种常见模式

匿名class

nextButton.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e)
  {
    //Your code to do something here 
  }
});

使用自定义 ActionListener class:

class MyNextButtonActions implements ActionListener
{

    // lots of rich method etc that code the meaning of life

    public void actionPerformed(ActionEvent e)
    {
        //code to trigger on click, using all your lovely methods
    }

}

然后将其添加到创建 GUI 的按钮,即

nextButton.addActionListener(new MyNextButtonActions());

Swing framework here 提供了有关所有这些内容的体面教程。您可以变得非常有创意 - 例如,您可以让您的 SwingJPanelDemo class 实现 ActionListener 并将其添加为其所有组件的 ActionListener。

确保导入 java.awt.event.*;,然后通过执行

添加一个动作监听器到你的 jbutton
NextButton.addActionListener(new MyFirstActionListener());

接下来我们必须创建 MyFirstActionListener() class。所以你的 class 里面的任何地方都很好..也许在 main 方法和你的构造函数之间,让它像这样:并确保它实现了 ActionListener 接口。 ActionListener 接口只需要一种方法; actionPerformed 方法,它将 ActionEvent 对象作为参数。

private class MyFirstActionListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        String whatIsWrittenOnTheButton = e.getActionCommand();
        if(whatIsWrittenOnTheButton.equals("Next")) {
            System.out.println("You pressed the \'Next\' JButton.");
        }
        if(e.getSource() == NextButton) {
            System.out.println("Here is a 2nd message upon pressing next");
        }
    }
}

e.getActionCommand() 将 return 一个表示按下按钮上所写内容的字符串,而 e.getSource() 将 return 一个对象。如果您对多个不同的按钮使用相同的动作侦听器,这将特别方便;这是区分它们的一种方式。