在 java 中单击按钮时如何获取文本区域的文本
how to get the text of a textArea when button is clicked in java
大家好,我正在 java.In 我的客户端程序中编写一个聊天服务器项目,我使用两个文本 areas.One 来显示客户端之间的对话,另一个用于输入客户端消息。起初我使用了 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。最好的办法是使用 Key Bindings 绑定到 JTextArea 的 VK_ENTER
KeyStroke,并将代码放在用于绑定的 AbstractAction 中。按键绑定教程将向您展示详细信息: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.In 我的客户端程序中编写一个聊天服务器项目,我使用两个文本 areas.One 来显示客户端之间的对话,另一个用于输入客户端消息。起初我使用了 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。最好的办法是使用 Key Bindings 绑定到 JTextArea 的 VK_ENTER
KeyStroke,并将代码放在用于绑定的 AbstractAction 中。按键绑定教程将向您展示详细信息: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());
}
}