English 中文(简体)
Java在进入纽顿时取消新的职能
原标题:Removing new line function when enter button is pressed Java

我有一个案文领域,在进入纽顿时,我想变成空白。 我知道,这样做通常会采用一套办法。 然而,当我这样做时,案文被删除,但返回钥匙所产生的新的线功能却受到压力。 我的问题是,阻止这一缺席行动发生是否有任何途径?

感谢

问题回答

你们是否聆听了环境词汇在文字领域的关键,然后予以清除? 以下是我的工作:

final JTextArea ta = new JTextArea();
ta.addKeyListener(new KeyListener() {

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyReleased(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_ENTER){
            ta.setText("");
        }
    }

    @Override
    public void keyPressed(KeyEvent e) {
    }
});

希 腊

我有一个案文领域,在进入纽顿时,我想变成空白。

我理解这一点。 在这方面,你可以做些什么:

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

public class Main {
    public static void main(String[]args) {
        final JFrame frame = new JFrame();
        final JTextArea area = new JTextArea();
        area.setPreferredSize(new Dimension(200, 200));
        area.addKeyListener(new KeyAdapter(){
            @Override
            public void keyReleased(KeyEvent e) {
                if(e.getKeyCode() == KeyEvent.VK_ENTER) {
                    area.setText("");
                }
            }
        });
        frame.add(area);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}

我知道,这样做通常会采用一套办法。 然而,当我这样做时,案文被删除,但返回钥匙所产生的新的线功能却受到压力。 我的问题是,阻止这一缺席行动发生是否有任何途径?

我对此不理解。

问题很可能是,你不会消耗关键的中风事件,尽管案文区已经清除,但关键中风的正常处理最终会插入一条新线。

我建议使用文件Filter,而不是绕过关键的中风事件(它必然是便携式的)。 http://www.java2s.com/Code/Java/Swing-JFC/DocumentFilter:mapsdowncaseletterstouppercase.htm“rel=“nofollow noreferer”> 。 执行过滤器,以便在插入或替换插图时,用“”取代文件的全部内容。

然而,这种办法可以说明在关键板上打上新线与在案文区打上新线之间的区别。

在你澄清案文之前,你需要删除返回纽顿所遗留的新路线。 页: 1

您的案文:

yourkeyevent.consume();
yourTextObject.setText("");

相反,你也可以使用:

yourTextarea.setText(null);
yourTextarea.setCaretPosition(-1);

I solved your problem overriding the code in the method "public void keyTyped(KeyEvent e)" instead of "public void keyPressed(KeyEvent e)" and it works. Here the code:

package versione1;

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

public class WhiteEnter {
    final JFrame frame = new JFrame();
    private JTextArea area = new JTextArea();


    public static void main(String[]args) {

        WhiteEnter prova = new WhiteEnter();
        prova.run();
    }

    public void run(){
        area.setPreferredSize(new Dimension(200, 200));
        area.addKeyListener(new PressEnterKeyListener());

        frame.add(area);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    public class PressEnterKeyListener implements KeyListener{

        @Override
        public void keyPressed(KeyEvent e) {
        }

        @Override
        public void keyReleased(KeyEvent e) {
        }

        @Override
        public void keyTyped(KeyEvent e) {

            if (e.getKeyChar() == (KeyEvent.VK_ENTER)){
                try{
                    area.setText(null);
                    area.setCaretPosition(0);
                } catch(Exception ex){
                    ex.printStackTrace();
                }
            }
        }
    }
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签