我正在尝试编写一些最终的聊天室代码。现在,我正在研究GUI组件,并试图使自己熟悉ActionListeners并使事情发生!不幸的是,我似乎遇到了障碍。

我的动作侦听器都自动跳到相同的“ actionPerformed”。我不确定如何为添加的每个ActionListener创建唯一的actionPerforms。

请您解释一下我如何接受我的:

// ADD ACTION LISTENERS TO COMPONENTS
I1.addActionListener(this);         // INPUT
B1.addActionListener(this);         // SEND BUTTON


并为不同的代码段分别进行addActionListener分配。毕竟我不能让他们都使用它!

public void actionPerformed(ActionEvent e){
if(I1.getText() != null){
   A1.append(" USERNAME " + ": " + I1.getText() + n);
   I1.setText("");
   }
}


问题

我不知道如何为每个ActionListener分配唯一的actionPerformed命令。当我想为每个侦听器使用唯一的侦听器时,我添加的所有侦听器都使用默认的actionPerform。



可以使用Listener紧紧压缩每个actionPerformed!

    // ***          ADD ACTION LISTENERS TO COMPONENTS
    // INPUT | TEXT AREA
    I1.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            A1.append(" USERNAME " + ": " + I1.getText() + n);
            I1.setText("");
        }
    });

    // INPUT | SENT BUTTON
    B1.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            A1.append(" USERNAME " + ": " + I1.getText() + n);
            I1.setText("");
        }
    });


完整代码:

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

public class GUI_ScreenWindow extends JFrame implements ActionListener{
// DECLARATIONS : TEXT AND USER INPUTS
private final static String n = "\n";

// DECLARATIONS : APPEARENCE
private JPanel PN = new JPanel();                   // NORTH PANEL  | MENU BAR
private JPanel PS = new JPanel();                   // SOUTH PANEL  | TEXT INPUT
private JPanel PS1 = new JPanel();                  // SOUTH PANEL  | SEND
private JPanel PC = new JPanel();                   // CENTER PANEL | TEXT AREA
private JTextField I1 = new JTextField(45);         // INPUT
private JTextArea A1 = new JTextArea(25, 51);       // DISPLAY
private JButton B1 = new JButton("SEND");           // SEND

// DECLARATION : MENU BARS
private JMenuBar MENU = new JMenuBar();
private JMenu file = new JMenu("FILE");                             // MENU: FILE
private JMenu display = new JMenu("DISPLAY");                       // MENU: DISPLAY
private JMenuItem S = new JMenuItem("Save", KeyEvent.VK_N);         // ITEM | FILE | SAVE
private JMenuItem C = new JMenuItem("Clear", KeyEvent.VK_N);        // ITEM | FILE | CLEAR
private JMenuItem D = new JMenuItem("Disconnect", KeyEvent.VK_N);   // ITEM | FILE | DISCONNECT
private JMenuItem red = new JMenuItem("Red", KeyEvent.VK_N);        // ITEM | EDIT | RED
private JMenuItem d = new JMenuItem("Default", KeyEvent.VK_N);      // ITEM | EDIT | DEFAULT

public GUI_ScreenWindow(){
    super("J&G CHATROOM");

    // LAYOUT POSITION/TYPE
    setLayout(new BorderLayout());
    PN.setLayout(new GridLayout());
    PS1.setLayout(new BorderLayout());
    PS.setLayout(new BorderLayout());
    PC.setLayout(new FlowLayout());

    // BUILD THE MENU BAR
    file.setMnemonic(KeyEvent.VK_F);
    MENU.add(file);
    display.setMnemonic(KeyEvent.VK_F);
    MENU.add(display);

    //                  FILE
    file.add(S);        // SAVE
    file.add(C);        // CLEAR CHAT
    file.add(D);        // DISCONNECT FROM SERVER

    //                  DISPLAY COLOR
    display.add(d);     // DEFAULT
    display.add(red);   // RED

    // EDIT TEXT AREA
    JScrollPane A1scroll = new JScrollPane(A1);
    A1scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    A1scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    A1.setEditable(false);
    A1.setLineWrap(true);
    A1.setWrapStyleWord(true);

    // ADD COMPONENTS TO THE FRAME
    add("North", PN);
    add("Center", PC);
    add("South", PS);
    PC.add(A1scroll);
    PS.add("East", PS1);
    PS.add(I1);
    PS1.add(B1);
    PN.add(MENU);

    // ADD ACTION LISTENERS TO COMPONENTS
    I1.addActionListener(this);         // INPUT
    B1.addActionListener(this);         // SEND BUTTON
}

public void actionPerformed(ActionEvent e){
    if(I1.getText() != null){
        A1.append(" USERNAME " + ": " + I1.getText() + n);
        I1.setText("");
    }
}}

最佳答案

解决方案1:

您可以获取事件的来源,例如:
e.getSource()并将其与I1和B1进行比较。
就像这样:

if(e.getSource() == aButton){
    //do something
}


解决方案2:

使用匿名类:

button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    //do whatever you want here
    }
});

10-06 14:42