【问题标题】:Is there any function operates as JButton pressed?按下 JButton 时是否有任何功能运行?
【发布时间】:2014-12-18 18:22:19
【问题描述】:

我正在为聊天程序中的 JTextArea 制作 KeyListener

如果我按 ENTER 键,JTextArea 会换行

但是,我想让 ENTER 键作为“发送”功能操作

这是我的代码

public void keyPressed(KeyEvent ke) {
            if(ke.getKeyCode() == KeyEvent.VK_ENTER)
                sendButton.???
            else if(ke.getKeyCode() == KeyEvent.VK_ALT + KeyEvent.VK_ENTER)
                inputTextArea.setText(inputTextArea.getText() + "\n");
        }

此时我该怎么办 -> ???

还有什么有用的建议,教教我,让我的程序开发:)

【问题讨论】:

标签: java awt jbutton jtextarea keylistener


【解决方案1】:

不要将KeyListener 与文本组件一起使用,这通常是不好的做法,相反,您希望控制KeyEvent.VK_ENTER 键绑定操作并在那里插入您的自定义功能...

现在,诀窍是,让现有的 Action 仍然有效,因为您不想再做任何需要的工作...

这可以通过将ActionKeyEvent.VK_ENTER 关联来实现...

InputMap im = ta.getInputMap(JTextArea.WHEN_FOCUSED);
ActionMap am = ta.getActionMap();

Object key = im.get(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0));
Action insertBreak = am.get(key);

然后,您可以将自己的操作设置为在其位置触发,将前一个操作传递给它,以便它可以在您想要它的时候调用...

am.put(key, new MyNewOnEnterAction(insertBreak));

//...

public class MyNewOnEnterAction extends AbstractAction {

    private Action proxy;

    public MyNewOnEnterAction(Action proxy) {
        this.proxy = proxy;
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        // Your custom functionality here...
        proxy.actionPerformed(e);
    }

}

例如...

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.Element;
import javax.swing.text.JTextComponent;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    private JLabel status;

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JTextArea ta = new JTextArea(10, 10);
                InputMap im = ta.getInputMap(JTextArea.WHEN_FOCUSED);
                ActionMap am = ta.getActionMap();

                Object key = im.get(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0));
                Action insertBreak = am.get(key);

                am.put(key, new MyNewOnEnterAction(insertBreak));

                status = new JLabel("...");

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new JScrollPane(ta));
                frame.add(status, BorderLayout.SOUTH);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public void sendMessage(String message) {

    }

    public class MyNewOnEnterAction extends AbstractAction {

        private Action proxy;

        public MyNewOnEnterAction(Action proxy) {
            this.proxy = proxy;
        }

        public int getLineAtCaret(JTextComponent component) {
            int caretPosition = component.getCaretPosition();
            Element root = component.getDocument().getDefaultRootElement();

            return root.getElementIndex(caretPosition) + 1;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            JTextArea ta = (JTextArea) e.getSource();
            int line = getLineAtCaret(ta);
            try {
                int startPos = ta.getLineStartOffset(line - 1);
                int endPos = ta.getLineEndOffset(line - 1);
                Document doc = ta.getDocument();

                String text = doc.getText(startPos, endPos - startPos);
                status.setText(text);
            } catch (BadLocationException ex) {
                ex.printStackTrace();
            }

            proxy.actionPerformed(e);
        }

    }

}

这将获取当前文本行并将其设置为statusJLabeltext...

就个人而言,我会设置一个类或方法,您的键绑定Action 和您的按钮都可以调用...或者您甚至可以使用相同的Action,谁知道;)

请参阅How to Use Key BindingsHow to Use Actions 了解更多详情

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-27
    • 1970-01-01
    • 1970-01-01
    • 2015-12-01
    • 2023-01-26
    • 1970-01-01
    • 2021-08-20
    相关资源
    最近更新 更多