Java 我需要听一个JFrame按钮,让它更新一个布尔值

Java 我需要听一个JFrame按钮,让它更新一个布尔值,java,swing,if-statement,jbutton,actionlistener,Java,Swing,If Statement,Jbutton,Actionlistener,我不知道如何做到这一点,花了2天的时间研究Java API,在这些论坛上,我什么也没有发现,如果有人能告诉我如何使用action listener来做到这一点,那将是非常棒的,我发现的大部分东西都只是一个按钮,而不是其他一些东西。这是我的密码: import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import java.awt.*;

我不知道如何做到这一点,花了2天的时间研究Java API,在这些论坛上,我什么也没有发现,如果有人能告诉我如何使用action listener来做到这一点,那将是非常棒的,我发现的大部分东西都只是一个按钮,而不是其他一些东西。这是我的密码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Skeleton extends JFrame implements ActionListener{

    public static void addComponentsToPane(Container pane) {
    pane.setLayout(null);

    JButton b1 = new JButton("Login");
    JTextField field2 = new JTextField(2);
    JTextField field = new JTextField(1);

    pane.add(field);
    pane.add(field2);
    pane.add(b1);

    Insets insets = pane.getInsets();
    Dimension size = field.getMaximumSize();
    field.setBounds(25 + insets.left, 5 + insets.top,
                 200, 20);
    size = field2.getPreferredSize();
    field2.setBounds(25 + insets.left, 40 + insets.top,
                 200, 20);
    size = b1.getPreferredSize();
    b1.setBounds(75 + insets.left, 75 + insets.top, 100, 40);


}
    private static void createAndShowGUI() {


        JFrame frame = new JFrame("User Login"); // GUI gui = new GUI() as well
        // default value JFrame.HIDE_ON_CLOSE
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        addComponentsToPane(frame.getContentPane());

        //Create the menu bar.  Make it have a Blue background.
        JMenuBar blueMenuBar = new JMenuBar();
        blueMenuBar.setOpaque(true);
        blueMenuBar.setBackground(new Color(211, 221, 222));
        blueMenuBar.setPreferredSize(new Dimension(300, 20));        

        //Create a grey label to put in the content pane.
        JLabel greyLabel = new JLabel();
        greyLabel.setOpaque(true);
        greyLabel.setBackground(new Color(205, 209, 209));
        greyLabel.setPreferredSize(new Dimension(300, 400));

        //Adding a custom BorderLayout
        JPanel panel = new JPanel(new BorderLayout());
        Container contentPane = frame.getContentPane();
        //Set the menu bar and add the label to the content pane.
        frame.setJMenuBar(blueMenuBar);
        frame.getContentPane().add(greyLabel, BorderLayout.CENTER);    

        //Display the window.

        frame.setSize(300, 200);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub

    }
}
只需在按钮中添加一个:

boolean state = false;

JButton b1 = new JButton("Login");
b1.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        // some action to perform
        state = true;
    }
});

那么,您到底想做什么呢?Java GUI必须在不同的操作系统、屏幕大小、屏幕分辨率等上工作。因此,它们不利于像素完美布局。使用布局管理器,或者与布局填充和边框一起使用。我发现的大多数东西都只是一个按钮,而不是一堆其他的东西。首先,让它以这种方式工作。然后将按钮合并到复杂的GUI中。如果您总是在复杂的示例中搜索如何做简单的事情,您将不会找到太多..进一步的注释:1此代码既扩展了框架,又使用了框架的实例。最好只做后者。2鉴于它似乎是一个登录GUI,它更适合显示在模态JDialog或JOptionPane中。顺便说一句-JButton+boolean建议我使用JCheckBox或JToggleButton。。