Ich versuche, einen kleinen Chatroomcode zu erstellen. Momentan arbeite ich an den GUI-Komponenten und versuche, mich mit ActionListeners vertraut zu machen und etwas zu bewegen! Leider habe ich anscheinend eine Straßensperre getroffen.Wie weisen Sie actionPerformed bestimmten actionListeners zu?
Meine Aktion Listener springen beide automatisch zum gleichen "actionPerformed". Ich bin mir nicht sicher, wie ich für jeden von mir hinzugefügten ActionListener eindeutige actionPerforms erstellen kann.
Bitte können Sie erklären, wie ich meine nehmen:
// ADD ACTION LISTENERS TO COMPONENTS
I1.addActionListener(this); // INPUT
B1.addActionListener(this); // SEND BUTTON
und separate addActionListener Zuweisungen für verschiedene Teile des Codes machen. Ich kann sie nicht alle benutzen!
public void actionPerformed(ActionEvent e){
if(I1.getText() != null){
A1.append(" USERNAME " + ": " + I1.getText() + n);
I1.setText("");
}
}
AUSGABE
Ich weiß nicht, wie zu jeder Action einzigartige actionPerformed-Befehle zuweisen. Alle hinzugefügten Listener verwenden das standardmäßige actionPerform, wenn ich für jeden Listener eindeutige verwenden möchte.
SOLUTION
Jeder actionPerformed kann wie so direkt mit dem Zuhörer gequetscht werden!
// *** 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("");
}
});
FULL CODE:
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("");
}
}}
Thank you! Das hat perfekt funktioniert. Ich schätze es. –