我再次陷入困境,试图弄清楚如何找出单击到label1的按钮。

当我单击button01时,我将打印出A。我需要它打印到label1上,并在按下A时说文件夹A,而在按下B时说文件夹B,依此类推。有没有朝着正确的方向前进?

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class JFileCabinet extends JFrame {

    private JButton button01 = new JButton("A");
    private JButton button02 = new JButton("B");
    private JButton button03 = new JButton("C");
    private JButton button04 = new JButton("D");
    private JButton button05 = new JButton("E");
    private JButton button06 = new JButton("F");
    private JButton button07 = new JButton("G");
    private JButton button08 = new JButton("H");
    private JButton button09 = new JButton("I");
    private JButton button10 = new JButton("J");
    private JButton button11 = new JButton("K");
    private JButton button12 = new JButton("L");
    private JButton button13 = new JButton("M");
    private JButton button14 = new JButton("N");
    private JButton button15 = new JButton("O");
    private JButton button16 = new JButton("P");
    private JButton button17 = new JButton("Q");
    private JButton button18 = new JButton("R");
    private JButton button19 = new JButton("S");
    private JButton button20 = new JButton("T");
    private JButton button21 = new JButton("U");
    private JButton button22 = new JButton("V");
    private JButton button23 = new JButton("W");
    private JButton button24 = new JButton("X");
    private JButton button25 = new JButton("Y");
    private JButton button26 = new JButton("Z");
    private JButton button27 = new JButton(" ");
    private JButton button28 = new JButton(" ");
    private JButton button29 = new JButton(" ");
    private JButton button30 = new JButton(" ");
    private static JLabel label1 = new JLabel("Folder ");
    private JPanel panel01 = new JPanel(new GridLayout(1, 6));
    private JPanel panel02 = new JPanel(new GridLayout(1, 6));
    private JPanel panel03 = new JPanel(new GridLayout(1, 6));
    private JPanel panel04 = new JPanel(new GridLayout(1, 6));
    private JPanel panel05 = new JPanel(new GridLayout(1, 2));
    private GridLayout layout = new GridLayout(5, 1, 5, 5);

    public JFileCabinet() {

        setLayout(layout);
        add(panel01);
        add(panel02);
        add(panel03);
        add(panel04);
        add(panel05);

        button01.addActionListener(new ButtonListener());
        panel01.add(button01);

        panel01.add(button02);
        panel01.add(button03);
        panel01.add(button04);
        panel01.add(button05);
        panel01.add(button06);

        panel02.add(button07);
        panel02.add(button08);
        panel02.add(button09);
        panel02.add(button10);
        panel02.add(button11);
        panel02.add(button12);

        panel03.add(button13);
        panel03.add(button14);
        panel03.add(button15);
        panel03.add(button16);
        panel03.add(button17);
        panel03.add(button18);

        panel04.add(button19);
        panel04.add(button20);
        panel04.add(button21);
        panel04.add(button22);
        panel04.add(button23);
        panel04.add(button24);

        panel05.add(button25);
        panel05.add(button26);
        panel05.add(button27).setVisible(false);
        panel05.add(button28).setVisible(false);
        //panel05.add(button29).setVisible(false);
        panel05.add(label1);
        panel05.add(button30).setVisible(false);

        setSize(400, 350);
        setVisible(true);
    }
    public static void main(String[] args) {
        JFileCabinet frame = new JFileCabinet();
    }
    String selection;
    class ButtonListener implements ActionListener {
        private String e;
        public void actionPerformed(ActionEvent e) {
            //throw new UnsupportedOperationException("Not supported yet.");
            String clicked = null;
            if (e.getActionCommand().equals("A")) {                //clicked.equals("A");
                System.out.println("A");
            }
        }
    }
}

最佳答案

label1.setText("Folder " + e.getActionCommand());

10-06 10:03