Hallo Leute,
ich bin gerade dabei eine kleine GUI zu erstellen und bin auf ein "Problem" gestoßen. Ich habe erstemal eine einfache GUI mit 5 Buttons. Jetzt möchte ich, dass wenn ich einen jButton drücke und halte (nicht loslassen), dass während dieser Zeit etwas ausgeführt wird und wenn ich den Button wieder los lasse soll es aufhören. Zur Zeit habe ich nur das normale "wurde" gedrückt Event.
Oder muss ich da umdenken und ggf ein MouseEvent nehmen?
ich bin gerade dabei eine kleine GUI zu erstellen und bin auf ein "Problem" gestoßen. Ich habe erstemal eine einfache GUI mit 5 Buttons. Jetzt möchte ich, dass wenn ich einen jButton drücke und halte (nicht loslassen), dass während dieser Zeit etwas ausgeführt wird und wenn ich den Button wieder los lasse soll es aufhören. Zur Zeit habe ich nur das normale "wurde" gedrückt Event.
Oder muss ich da umdenken und ggf ein MouseEvent nehmen?
Java:
package gui_interface;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Layout extends JFrame implements ActionListener {
private char klickedButton = ' ';
private String buttonText = "current Button pressed: ";
private int key = 0;
JButton wButton = new JButton("W");
JButton aButton = new JButton("A");
JButton sButton = new JButton("S");
JButton dButton = new JButton("D");
JButton exitButton = new JButton("Exit");
JLabel activeButton = new JLabel(buttonText + klickedButton + key);
private void initGUI() {
JPanel p = (JPanel) this.getContentPane();
p.setLayout(null);
//button.setBounds(margin left, margin top, width, height )
activeButton.setBounds(90, 300, 590, 20);
p.add(activeButton);
wButton.setBounds(270, 20, 60, 60);
p.add(wButton);
wButton.addActionListener(this);
aButton.setBounds(120, 100, 60, 60);
p.add(aButton);
aButton.addActionListener(this);
sButton.setBounds(420, 100, 60, 60);
p.add(sButton);
sButton.addActionListener(this);
dButton.setBounds(270, 180, 60, 60);
p.add(dButton);
dButton.addActionListener(this);
exitButton.setBounds(530, 540, 60, 20);
p.add(exitButton);
exitButton.addActionListener(this);
}
private void setText(char button, String text){
activeButton.setText(buttonText + button + " " + key + " "+ text);
}
public Layout() {
this.setTitle("Buttontest");
this.setSize(600, 600);
initGUI();
}
@Override
public void actionPerformed(ActionEvent event) {
Object source = event.getSource();
if (source == wButton) {
setText('w',"");
key ++;
}
if (source == aButton) {
setText('a', "");
}
if (source == sButton) {
setText('s', "");
}
if (source == dButton) {
setText('d', "");
}
if (source == exitButton) {
// beenden
System.exit(0);
}
}
}