简体   繁体   English

JTextArea Java的ActionListener

[英]ActionListener from JTextArea Java

I'm new to Java, and trying to figure out one last thing for my program. 我是Java的新手,正在尝试为我的程序弄清楚最后一件事。

This is the program that I have coded and with the layout it is perfectly fine no problem at all. 这是我编写的程序,布局合理,完全没有问题。

在此处输入图片说明

Now my program suppose highlight the buttons whenever to press it on the keyboard (NOT BY PRESSING THE BUTTON ON THE SCREEN) 现在我的程序假设只要在键盘上按下它,就突出显示按钮(不要通过按屏幕上的按钮)

I'm not sure what I have to use since the action that it needs to take is when they type it in the JTextArea . 我不确定我必须使用什么,因为它需要执行的操作是当他们在JTextArea键入它时。 I'm trying to use KeyEvent with KeyPressed but not sure if that is the right thing to do since it doesn't really work. 我正在尝试将KeyEventKeyPressed一起使用,但不确定这样做是否正确,因为它实际上无法正常工作。

I can't post my code at the moment here since this is an assignment and I don't want some of my classmate to google and use it if they found it here. 我目前无法在此处发布代码,因为这是一项作业,并且我不希望我的一些同学去google并在他们在这里找到的情况下使用它。 (LOL) (大声笑)

As required here is my codes :) 根据需要,这里是我的代码:)

import javax.swing.*; // import all javax.swing
import java.awt.*; // import all java.awt
import java.awt.event.*;

public class Sample extends JFrame implements KeyListener { // start of the
                                                            // class

    private JTextArea field = new JTextArea(10,15); // create an instance of
                                                        // JTextField
    private JPanel mainPanel = new JPanel(); // create an instance of JPanel
    private JPanel TopFieldPan = new JPanel();
    private JPanel MainBtnsPan = new JPanel();
    private JPanel FifthRowPan = new JPanel();

    JPanel fifthBtn = new JPanel();

    private static JButton Space = new JButton("");

    public Sample() { // start of the weather constructor

        Space.setPreferredSize(new Dimension(280, 45));
        fifthBtn.add(Space);


        TopFieldPan.add(field);

        FifthRowPan.setLayout(new BoxLayout(FifthRowPan, BoxLayout.X_AXIS));
        FifthRowPan.add(fifthBtn);
        MainBtnsPan.setLayout(new GridLayout(5, 5, 0, 0));

        MainBtnsPan.add(FifthRowPan);

        mainPanel.add(TopFieldPan);
        mainPanel.add(MainBtnsPan);

        this.add(mainPanel);

        Space.setSelected(true);
        field.addKeyListener(this); // important !!!

        setTitle("Typing Tutor"); // set the title to the frame
        setSize(300, 300); // set the fixed size
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        setLocationRelativeTo(null); 
        setVisible(true); // make it visible

    } // ends of the constructor

    public void keyPressed(KeyEvent e) {

        if (e.getKeyCode() == KeyEvent.VK_SHIFT) {
            Space.setBackground(Color.green);

        }


    }

    public void keyReleased(KeyEvent evt) {
        Space.setBackground(null);
    }

    public void keyTyped(KeyEvent evt) {
        // TODO Auto-generated method stub

        if(evt.getKeyChar() == 'a' || evt.getKeyChar() ==  'A')
        {
            Space.setBackground(Color.green);
        }
        else if(evt.getKeyChar() == 'b' || evt.getKeyChar() == 'B')
        {
            Space.setBackground(Color.red);
        }       
    }

    public static void main(String[] args) { // start of the main method

        new Sample();

    } // ends of main method

} // ends of class

I tried to simplified the code as much as I could and here is the final one. 我尽力简化了代码,这是最后一个。

So I'm trying to make it when I press a or A it should highlight that space JButton. 所以我要把它当我按下aA应该强调的是空间的JButton。

Create a map of your buttons and the keys they map to, like this: 创建一个按钮和它们所映射的键的映射,如下所示:

Map<String, JButton> buttonMap = new HashMap<String, Button>();

Then, when you are adding the buttons, add them to the map, like this: 然后,在添加按钮时,将其添加到地图,如下所示:

buttonMap.put(FirstRow[i].toLowerCase(), btn);

Then, add something like this into your KeyTyped: 然后,将以下内容添加到您的KeyTyped中:

public void keyTyped(KeyEvent evt) {
    String keyPressed = new String(""+evt.getKeyChar()).toLowerCase();
    JButton tmp = buttonMap.get(keyPressed);
    if(null != tmp){
        tmp.doClick();
    }
}

I did this quickly to your code for row 1 and 2. You'll have to play around with it to get it to work for the special keys, but it should get you where you're trying to go. 我很快对您的第1行和第2行的代码进行了处理。您必须对其进行处理才能使其适用于特殊键,但是它应该可以将您带到尝试的位置。

I pasted it here, to keep the answer small. 我将其粘贴在此处,以缩小答案范围。 http://pastebin.com/t1v8d6Hi http://pastebin.com/t1v8d6Hi

You're code looks okay for a first pass, you seem to have a basic mechanism in place that works with the KeyListener. 您的代码在第一遍看起来还不错,您似乎已经具备与KeyListener一起使用的基本机制。 You probably need to think about how to stop mouse clicks on the buttons, JButton.setEnabled(false) works but changes how the button is drawn so you may need to override the paint method. 您可能需要考虑如何停止对按钮的鼠标单击,JButton.setEnabled(false)可以工作,但是会更改按钮的绘制方式,因此您可能需要覆盖paint方法。 You probably only need to hook the keylistener up to one component, the window will get all events, the text area when it has focus. 您可能只需要将keylistener挂接到一个组件上,该窗口将获得所有事件,即具有焦点的文本区域。 The main task you have is to work out how to map the key pressed events to the buttons, maybe use a hashmap or something to store the JButtons with the key being the character code? 您的主要任务是弄清楚如何将按键事件映射到按钮,也许使用哈希图或其他东西来存储JButton,而按键是字符代码?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM