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

ens*_*orj 2 java user-interface swing jbutton actionlistener

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

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")) {

                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

mpr*_*hat 5

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

您需要在按钮中添加列表器,如下所示:

oneButton.addActionListener(this);
Run Code Online (Sandbox Code Playgroud)

然后改变你的行动像以下一样:

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);
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

阅读本教程