Java 使用ActionListeners,无法将文本字段设置为不可见

Java 使用ActionListeners,无法将文本字段设置为不可见,java,swing,user-interface,jbutton,actionlistener,Java,Swing,User Interface,Jbutton,Actionlistener,我只是想让一个文本字段在我单击其中一个编号按钮之前不可见。忽略我糟糕的间距和缺乏评论,因为这是一个早期的草稿。文本字段称为inputField1,我希望在单击标记为1的按钮时将其设置为可见。谢谢 import java.lang.String.*; import java.lang.Exception.*; import java.util.EventObject; import javax.swing.*; import java.awt.*; import java.awt.event.*;

我只是想让一个文本字段在我单击其中一个编号按钮之前不可见。忽略我糟糕的间距和缺乏评论,因为这是一个早期的草稿。文本字段称为inputField1,我希望在单击标记为1的按钮时将其设置为可见。谢谢

import java.lang.String.*;
import java.lang.Exception.*;
import java.util.EventObject;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Airplanes extends JFrame implements ActionListener {
    private static final int FRAME_WIDTH = 330;
    private static final int FRAME_HEIGHT = 300;

    private static final int FRAME_X_ORIGIN = 150;
    private static final int FRAME_Y_ORIGIN = 300;

    private static final int BUTTON_WIDTH = 50;
    private static final int BUTTON_HEIGHT = 30;

    private JTextField inputLine1;

    private JButton oneButton;
    private JButton twoButton;
    private JButton threeButton;
    private JButton fourButton;
    private JButton fiveButton;
    private JButton sixButton;
    private JButton sevenButton;
    private JButton eightButton;
    private JButton nineButton;
    private JButton tenButton;
    private JButton elevenButton;
    private JButton twelveButton;

    public static void main(String[] args) {
        Airplanes airplanes = new Airplanes();
        airplanes.setVisible(true);

    }

    @SuppressWarnings("null")
    public Airplanes() {
        Container contentPane;
        setSize(FRAME_WIDTH, FRAME_HEIGHT);
        setResizable(false);
        setTitle("Island Airways");
        setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
        contentPane = getContentPane();
        contentPane.setLayout(null);
        contentPane.setBackground(Color.white);

        oneButton = new JButton("1");
        oneButton.setBounds(10, 100, BUTTON_WIDTH, BUTTON_HEIGHT);
        contentPane.add(oneButton);

        twoButton = new JButton("2");
        twoButton.setBounds(10, 140, BUTTON_WIDTH, BUTTON_HEIGHT);
        contentPane.add(twoButton);

        threeButton = new JButton("3");
        threeButton.setBounds(60, 100, BUTTON_WIDTH, BUTTON_HEIGHT);
        contentPane.add(threeButton);

        fourButton = new JButton("4");
        fourButton.setBounds(60, 140, BUTTON_WIDTH, BUTTON_HEIGHT);
        contentPane.add(fourButton);

        fiveButton = new JButton("5");
        fiveButton.setBounds(110, 100, BUTTON_WIDTH, BUTTON_HEIGHT);
        contentPane.add(fiveButton);

        sixButton = new JButton("6");
        sixButton.setBounds(110, 140, BUTTON_WIDTH, BUTTON_HEIGHT);
        contentPane.add(sixButton);

        sevenButton = new JButton("7");
        sevenButton.setBounds(160, 100, BUTTON_WIDTH, BUTTON_HEIGHT);
        contentPane.add(sevenButton);

        eightButton = new JButton("8");
        eightButton.setBounds(160, 140, BUTTON_WIDTH, BUTTON_HEIGHT);
        contentPane.add(eightButton);

        nineButton = new JButton("9");
        nineButton.setBounds(210, 100, BUTTON_WIDTH, BUTTON_HEIGHT);
        contentPane.add(nineButton);

        tenButton = new JButton("10");
        tenButton.setBounds(210, 140, BUTTON_WIDTH, BUTTON_HEIGHT);
        contentPane.add(tenButton);

        elevenButton = new JButton("11");
        elevenButton.setBounds(260, 100, BUTTON_WIDTH, BUTTON_HEIGHT);
        contentPane.add(elevenButton);

        JButton twelveButton = new JButton("12");
        twelveButton.setBounds(260, 140, BUTTON_WIDTH, BUTTON_HEIGHT);
        contentPane.add(twelveButton);

        JButton firstClass = new JButton("First Class");
        firstClass.setBounds(50, 30, 100, 30);
        contentPane.add(firstClass);

        JButton coach = new JButton("Coach");
        coach.setBounds(175, 30, 100, 30);
        contentPane.add(coach);
        inputLine1 = new JTextField();
        inputLine1.setBounds(70, 200, 135, 30);
        contentPane.add(inputLine1);
        inputLine1.setVisible(false);

    }

    public void actionPerformed(ActionEvent event) {
        if (event.getSource() instanceof JButton) {
            JButton clickedButton = (JButton) event.getSource();
            String buttonText = clickedButton.getText();
            if (buttonText.equals("First Class")) {
                inputLine1.setVisible(true);
                if (buttonText.equals("1")) {

                }
            }
        }
    }
}

您已经实现了
ActionListener
,但是您的按钮没有附加到任何侦听器,因此它不会调用
actionPerformed
方法

您需要像这样向按钮添加列表器:

oneButton.addActionListener(this);
然后更改您的操作,如:

public void actionPerformed(ActionEvent event) {
        if (event.getSource() instanceof JButton) {
            JButton clickedButton = (JButton) event.getSource();
            String buttonText = clickedButton.getText();
            if (buttonText.equals("First Class")) {
                //do something
            } else if (buttonText.equals("1")) {
                inputLine1.setVisible(true);
            }
        }
    }

当您正在实施ActionListener时,请阅读此

  • 你必须实现它的方法

  • 您必须将该侦听器添加到所需的特定组件中

  • 你忘了做第二个

    oneButton.addActionListener(this);
    
    将以下代码添加到actionPerformed方法中

    if (buttonText.equals("1")) {
                    inputLine1.setVisible(true);
                }