0

I am trying to find a sulotion to translate the void keypressed from c# to java without any succes yet anyone a sulotion?

I want when i press the key 13(enter) that Private void doen() activates once.

import java.awt.event.*;
import javax.swing.JTextField;
import javax.swing.*;
import java.awt.*;
import java.awt.Toolkit;
import java.util.Timer;
import java.util.TimerTask;

public class Paneel extends JPanel {

    private static final long serialVersionUID = 1L;
    String text;
    String AccountName = "default";
    String autosavecheck = "";
    String iss;
    JProgressBar monsterbar, progressbar;
    JButton sendknop, clearknop, creditsknop, saveknop, loadknop, restartknop,
            disableautosaveknop;
    JTextArea commandstextbox, dialoogtextbox;
    JTextField naamtextbox, invoertextbox;
    JOptionPane resetdialog;
    Toolkit toolkit;
    Timer timer;

    public Paneel() {

        setLayout(null);
// --------------------------------
        dialoogtextbox = new JTextArea();
        dialoogtextbox.setFont(new Font("sansserif", Font.BOLD, 12));
        dialoogtextbox.setBounds(12, 12, 838, 207);
        dialoogtextbox.list();

        invoertextbox = new JTextField(12);
        invoertextbox.setBounds(12, 330, 982, 20);
        invoertextbox.setEnabled(false);
        commandstextbox = new JTextArea();
        commandstextbox.setBounds(856, 28, 138, 191);
        naamtextbox = new JTextField(12);
        naamtextbox.setBounds(772, 263, 220, 20);

        toolkit = Toolkit.getDefaultToolkit();
        timer1 = new Timer();
        toolkit = Toolkit.getDefaultToolkit();
        autosave = new Timer();
        toolkit = Toolkit.getDefaultToolkit();
        monstertimer = new Timer();
        toolkit = Toolkit.getDefaultToolkit();
        autodisabletimer = new Timer();

        sendknop = new JButton("Send");
        sendknop.setBounds(12, 260, 75, 23);
        sendknop.addActionListener(new sendknopHandler());

        add(sendknop);
    }

    private void keypressed() {
        if (e.KeyChar == (char) Keys.Return) {
            doen();
        }
    }

    private void doen() {
        text = invoertextbox.getText();
        invoertextbox.setText("");
    }
}
4

7 回答 7

2

As you can, there are a number of approaches you could try. You didn't really specify on which component you were interested in monitoring, so we've thrown a few different suggestions at you...

In the following example of demonstrated key bindings and the default button of the RootPane.

Unfortunately, the JTextArea consumes the enter key before the root pane is notified, meaning that it won't fire. This is a general issue with text fields as they respond to the enter key action.

The other problem you will face is the fact that I've overridden the default behavior of the JtextField's enter key, meaning it will no longer insert new lines.

public class TestPane extends JPanel {

    private JTextArea textArea;
    private JButton doneButton;

    public TestPane() {

        textArea = new JTextArea(10, 50);
        doneButton = new JButton("Done");

        setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 1;
        gbc.weighty = 1;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(4, 4, 4, 4);

        add(new JScrollPane(textArea), gbc);

        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(4, 4, 4, 4);

        add(doneButton, gbc);

        InputMap inputMap = textArea.getInputMap(JComponent.WHEN_FOCUSED);
        ActionMap actionMap = textArea.getActionMap();

        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "enter");
        actionMap.put("enter", new AbstractAction() {

            @Override
            public void actionPerformed(ActionEvent e) {

                System.out.println("I'm done here");

            }
        });

        doneButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("All the way out...");
            }
        });

    }

    @Override
    public void addNotify() {

        super.addNotify();

        // This is the button that will be activate by "default", depending on
        // what that means for the individual platforms...
        SwingUtilities.getRootPane(this).setDefaultButton(doneButton);

    }

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {

                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception exp) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setSize(400, 400);
                frame.setLocationRelativeTo(null);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.setVisible(true);

            }
        });

    }

}
于 2012-09-24T09:21:26.517 回答
2

好吧,如果您想在JButton按下 a 后调用该方法,则必须将 an 添加ActionListenerJButton并从内部调用该方法,actionPerformed(ActionEvent ae)如下所示:

jBtn.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("called");
        //call method here
    }
    });

或者,如果您想在JPanel使用KeyBindings上按下某个键后调用该方法,而不是KeyListener像这样:

JPanel panel=...;
...    

panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0), "send");
panel.getActionMap().put("send", new MyAction());

...

class MyAction extends AbstractAction {
@Override
public void actionPerformed(ActionEvent e) {
    System.out.println("called");
    //call method here
}

}
于 2012-09-24T08:49:03.933 回答
1

首先:正确设置格式,请参阅插入代码片段的说明。帮助我们这些阅读它的人:)

我不完全确定你在这里问什么,但我猜你正试图让你的程序响应用户点击你的任何按钮。这发生在您应该为按钮设置的各种 ActionListener 中。例如,在您的情况下, sendknopHandler 应该包含处理用户按下此特定按钮时发生的事情的逻辑。在这个类中,您必须过滤掉动作的来源(即按下的按钮)、动作是什么以及您想要如何响应。

于 2012-09-24T08:19:02.610 回答
1

了解所有关于听众和更具体的关键听众

于 2012-09-24T08:19:47.930 回答
1

“key 13”是一个按钮还是当用户键入“13”时?

如果它是一个按钮,只需使用

button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
                           your statements;
                    }
}

否则使用关键侦听器 Peter 响应。

于 2012-09-24T08:36:29.737 回答
1

a)在代码行之后public Paneel() {丢失 ,同样的问题是,}add(sendknop);public class Paneel extends JPanel {

b)从此代码和这种形式中,您永远不会听到任何事件,因为代码不可运行,您的问题是关于 Java Basic,而不是关于 Swing GUI,也不是关于侦听器

c)要监听ENTER键,您必须将ActionListener添加到JButton

d) 因为Swing GUI必须使用Swing Timer而不是util.Timer,否则您会遇到Swing 中的 Concurency问题

e) 对于 Swing GUI,不要使用 KeyListener,有KeyBindings并输出到Swing Action

f )否则Swing JComponents得到Focus in the WindowKeyListener

于 2012-09-24T08:38:19.760 回答
0

invoertextbox通过将以下行添加到代码的初始化部分,为您添加一个关键侦听器:

invoertextbox.addKeyListener(this);

然后通过添加扩展您的类来实现KeyListener接口:

import java.awt.event.KeyListener;

public class Paneel extends JPanel implements KeyListener {

Paneel并在您的类中实现接口的以下方法:

@Override
public void keyTyped(KeyEvent e) {
}

@Override
public void keyPressed(KeyEvent e) {
  if (e.getKeyCode()==KeyEvent.VK_ENTER) {
    doen();
  }
}

@Override
public void keyReleased(KeyEvent e) {
}

private void doen() {
  text = invoertextbox.getText();
  invoertextbox.setText("");
}

考虑使用keyTyped(KeyEvent)而不是keyPressed(KeyEvent).

“按键类型”事件是更高级别的,通常不依赖于平台或键盘布局。它们是在输入 Unicode 字符时生成的,并且是了解字符输入的首选方式。在最简单的情况下,按键类型的事件由单个按键产生(例如,'a')[…](JavaDoc for KeyEvent

于 2012-09-24T08:45:58.160 回答