gpt4 book ai didi

java - 在java中单击按钮时如何获取textArea的文本

转载 作者:行者123 更新时间:2023-11-30 08:31:03 27 4
gpt4 key购买 nike

大家好,我正在用 java 编写一个聊天服务器项目。在我的客户端程序中,我使用了两个文本区域。一个用于显示客户端之间的对话,另一个用于输入客户端的消息。起初我使用了 TextField 但我想要多行输入所以我最终使用了 textarea 因为没有多行的另一种选择..当发送按钮被点击或按钮输入被按下时我想获取文本并发送它..我已经知道发送它的代码和所有其他东西但是每次我尝试将 actionListener 添加到 textarea 时,编译器不允许我说它不是为 textareas 定义的,我想我会像使用 textfield 一样做类似的东西:

ActionListener sendListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == sendButton){
String str = inputTextArea.getText();}
}
};

然后

inputTextArea.addActionListener(sendListener);

请帮忙..

最佳答案

正如您所发现的,您无法将 ActionListener 添加到 JTextArea。最好的办法是使用键绑定(bind)绑定(bind)到 JTextArea 的 VK_ENTER KeyStroke,并将代码放在用于绑定(bind)的 AbstractAction 中。 Key Binding 教程将向您展示详细信息:Key Binding Tutorial

例如:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.*;

@SuppressWarnings("serial")
public class KeyBindingEg extends JPanel {
private static final int LARGE_TA_ROWS = 20;
private static final int TA_COLS = 40;
private static final int SMALL_TA_ROWS = 3;
private JTextArea largeTextArea = new JTextArea(LARGE_TA_ROWS, TA_COLS);
private JTextArea smallTextArea = new JTextArea(SMALL_TA_ROWS, TA_COLS);
private Action submitAction = new SubmitAction("Submit", KeyEvent.VK_S);
private JButton submitButton = new JButton(submitAction);

public KeyBindingEg() {
// set up key bindings
int condition = JComponent.WHEN_FOCUSED; // only bind when the text area is focused
InputMap inputMap = smallTextArea.getInputMap(condition);
ActionMap actionMap = smallTextArea.getActionMap();
KeyStroke enterStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
inputMap.put(enterStroke, enterStroke.toString());
actionMap.put(enterStroke.toString(), submitAction);

// set up GUI
largeTextArea.setFocusable(false); // this is for display only
largeTextArea.setWrapStyleWord(true);
largeTextArea.setLineWrap(true);
smallTextArea.setWrapStyleWord(true);
smallTextArea.setLineWrap(true);
JScrollPane largeScrollPane = new JScrollPane(largeTextArea);
JScrollPane smallScrollPane = new JScrollPane(smallTextArea);
largeScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
smallScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

JPanel bottomPanel = new JPanel(new BorderLayout());
bottomPanel.add(smallScrollPane, BorderLayout.CENTER);
bottomPanel.add(submitButton, BorderLayout.LINE_END);

setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
setLayout(new BorderLayout(3, 3));
add(largeScrollPane, BorderLayout.CENTER);
add(bottomPanel, BorderLayout.PAGE_END);
}

private class SubmitAction extends AbstractAction {
public SubmitAction(String name, int mnemonic) {
super(name);
putValue(MNEMONIC_KEY, mnemonic);
}

@Override
public void actionPerformed(ActionEvent e) {
String text = smallTextArea.getText();
smallTextArea.selectAll(); // keep text, but make it easy to replace
// smallTextArea.setText(""); // or if you want to clear the text
smallTextArea.requestFocusInWindow();

// TODO: send text to chat server here

// record text in our large text area
largeTextArea.append("Me> ");
largeTextArea.append(text);
largeTextArea.append("\n");
}
}

private static void createAndShowGui() {
JFrame frame = new JFrame("Key Binding Eg");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new KeyBindingEg());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}

编辑:新版本现在允许 Ctrl-Enter 组合键像原来的 Enter 键一样工作。这是通过将最初映射到 Enter 击键的 Action 现在映射到 Ctrl-Enter 击键来实现的:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;

@SuppressWarnings("serial")
public class KeyBindingEg extends JPanel {
private static final int LARGE_TA_ROWS = 20;
private static final int TA_COLS = 40;
private static final int SMALL_TA_ROWS = 3;
private JTextArea largeTextArea = new JTextArea(LARGE_TA_ROWS, TA_COLS);
private JTextArea smallTextArea = new JTextArea(SMALL_TA_ROWS, TA_COLS);
private Action submitAction = new SubmitAction("Submit", KeyEvent.VK_S);
private JButton submitButton = new JButton(submitAction);

public KeyBindingEg() {
// set up key bindings
int condition = JComponent.WHEN_FOCUSED; // only bind when the text area
// is focused
InputMap inputMap = smallTextArea.getInputMap(condition);
ActionMap actionMap = smallTextArea.getActionMap();

// get enter and ctrl-enter keystrokes
KeyStroke enterStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
KeyStroke ctrlEnterStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, InputEvent.CTRL_DOWN_MASK);

// get original input map key for the enter keystroke
String enterKey = (String) inputMap.get(enterStroke);
// note that there is no key in the map for the ctrl-enter keystroke --
// it is null

// extract the old action for the enter key stroke
Action oldEnterAction = actionMap.get(enterKey);
actionMap.put(enterKey, submitAction); // substitute our new action

// put the old enter Action back mapped to the ctrl-enter key
inputMap.put(ctrlEnterStroke, ctrlEnterStroke.toString());
actionMap.put(ctrlEnterStroke.toString(), oldEnterAction);

largeTextArea.setFocusable(false); // this is for display only
largeTextArea.setWrapStyleWord(true);
largeTextArea.setLineWrap(true);
smallTextArea.setWrapStyleWord(true);
smallTextArea.setLineWrap(true);
JScrollPane largeScrollPane = new JScrollPane(largeTextArea);
JScrollPane smallScrollPane = new JScrollPane(smallTextArea);
largeScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
smallScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

JPanel bottomPanel = new JPanel(new BorderLayout());
bottomPanel.add(smallScrollPane, BorderLayout.CENTER);
bottomPanel.add(submitButton, BorderLayout.LINE_END);

setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
setLayout(new BorderLayout(3, 3));
add(largeScrollPane, BorderLayout.CENTER);
add(bottomPanel, BorderLayout.PAGE_END);
}

private class SubmitAction extends AbstractAction {
public SubmitAction(String name, int mnemonic) {
super(name);
putValue(MNEMONIC_KEY, mnemonic);
}

@Override
public void actionPerformed(ActionEvent e) {
String text = smallTextArea.getText();
smallTextArea.selectAll(); // keep text, but make it easy to replace
// smallTextArea.setText(""); // or if you want to clear the text
smallTextArea.requestFocusInWindow();

// TODO: send text to chat server here

// record text in our large text area
largeTextArea.append("Me> ");
largeTextArea.append(text);
largeTextArea.append("\n");
}
}

private static void createAndShowGui() {
JFrame frame = new JFrame("Key Binding Eg");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new KeyBindingEg());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}

关于java - 在java中单击按钮时如何获取textArea的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40855387/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com