2

我只是想让文本字段不可见,直到我单击其中一个编号按钮。忽略我糟糕的间距和缺乏评论,因为这是一个早期的草稿。文本字段称为 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")) {

                }
            }
        }
    }
}
4

2 回答 2

5

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

您需要像这样向按钮添加侦听器:

oneButton.addActionListener(this);

然后改变你的 actionPerfomed 像:

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);
            }
        }
    }

阅读本教程

于 2012-04-23T05:02:05.613 回答
2

当你实现 ActionListener

  1. 你必须实现它的方法

  2. 您必须将该侦听器添加到您想要的特定组件

你忘了做第二个。

oneButton.addActionListener(this);

您将以下代码添加到您的 actionPerformed 方法中。

if (buttonText.equals("1")) {
                inputLine1.setVisible(true);
            }
于 2012-04-23T05:18:22.867 回答