如何将actionPerformed分配给特定的actionListeners?

时间:2017-01-16 12:55:55

标签: java

我正在尝试制作一个最终的聊天室代码。现在,我正在研究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命令。当我想为每个Listener使用唯一的监听器时,我添加的任何监听器都使用默认的actionPerform。

每个actionPerformed都可以像使用Listener一样直接进行挤压!

    // ***          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 个答案:

答案 0 :(得分:3)

解决方案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 
    } 
});