P
PlzHelpMe
Gast
hi. Ich habe ein Lauflicht und moechte nun, dass es erst startet, wenn ich auf die linke(oder rechte, ist egal) Maustaste klicke. Wie mache ich das???
Lauflicht.addMouseListener(this(){
public void mouseReleased(MouseEvent e)
{
System.out.println("Mouse Released ");
}
public void mousePressed(MouseEvent e)
{
System.out.println("Mouse Pressed ");
}
public void mouseExited(MouseEvent e)
{
System.out.println("Mouse Exited ");
}
public void mouseEntered(MouseEvent e)
{
System.out.println("Mouse Entered ");
}
public void mouseClicked(MouseEvent e)
{
System.out.println("Mouse clicked ");
}
});
Dann bitte der Instanz den Listener hinzufügen.Anonymous hat gesagt.:Lauflicht ist die Klasse...
import java.awt.*;
import java.awt.event.*;
import java.util.EventObject.*;
public class LED
extends Frame
implements Runnable, MouseListener
{
//Konstanten
private static final int NUMLEDS = 100;
private static final int SLEEP = 17;
private static final int LEDSIZE = 70;
private static final Color ONCOLOR = new Color(0,0,0);
private static final Color OFFCOLOR = new Color(255,255,255);
private static final Color TextColor = new Color(0,0,0);
//private static final Color SCHRIFT = new Color
public int Zaehler = 0;
//Instanzvariablen
private Thread th;
private int switched;
private int dx;
public static void main(String args[])
{
LED frame = new LED();
frame.setSize(1000,1000);
frame.setVisible(true);
frame.startAnimation();
button.addMouseListener(new MouseListener());
//addMouseListener(this);
}
public LED()
{
super("LED");
setBackground(Color.lightGray);
//WindowListener
addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent event)
{
if (th != null) {
th.stop();
th = null;
}
setVisible(false);
dispose();
System.exit(0);
}
}
);
setLayout(new FlowLayout());
Button button = new Button("LOS");
add(button);
}
button.addMouseListener(new MouseListener(){
public void mouseReleased(MouseEvent e)
{
System.out.println("Mouse Released ");
}
public void mousePressed(MouseEvent e)
{
System.out.println("Mouse Pressed ");
}
public void mouseExited(MouseEvent e)
{
System.out.println("Mouse Exited ");
}
public void mouseEntered(MouseEvent e)
{
System.out.println("Mouse Entered ");
}
public void mouseClicked(MouseEvent e)
{
System.out.println("Mouse clicked ");
}
});
public void startAnimation()
{
th = new Thread(this);
th.start();
}
public void run()
{
switched = -1;
dx = 1;
while (true) {
repaint();
try {
Thread.sleep(SLEEP);
} catch (InterruptedException e){
//nichts
}
switched += dx;
if (switched < 0 || switched > NUMLEDS - 1) {
dx = 1;
switched = 0;
}
}
}
public void paint( Graphics g ) {
g.setFont(new java.awt.Font("SansSerif",1,100));
int y = 3;
int x = 0;
for( int i = 0; i < NUMLEDS; i++ ) {
g.setColor( i == switched ? ONCOLOR : OFFCOLOR );
if ( i % 10 == 0 ) {
y++;
x=0;
}
x++;
if (i==NUMLEDS-1){
g.setColor(TextColor);
g.drawString(" "+Zaehler/100, 100, 150);
g.setColor(OFFCOLOR);
Zaehler++;
}
g.fillOval( x * ( LEDSIZE ), y * 70, LEDSIZE, LEDSIZE );
}
}
}
this.addMouseListener( new MouseAdapter() {
public void mouseClicked( MouseEvent event ) {
startAnimation();
}
});
Für was? MouseListener? KeyListener? die if-Abfrage ???Anonymous hat gesagt.:Kannst du mir dazu n Code-Beispiel geben?
Sky hat gesagt.:Das ist ein Forum und kein Code-Generator... woran scheitert es???
schade es scheitert daran, dass ich nicht weiss woran...