58,454
社区成员
发帖
与我相关
我的任务
分享
import java.awt.FlowLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Test implements MouseListener{
public boolean isButtonPressed = false;
public boolean isButtonReleased = false;
public boolean isThreadStarted = false;
public JFrame jf;
public JLabel jl;
public JButton jb;
public static void main(String[] args) {
Test test = new Test();
test.go();
}
public void go(){
jf = new JFrame();
jl = new JLabel("jl");
jb = new JButton("jb");
jf.setLayout(new FlowLayout());
jf.add(jl);
jf.add(jb);
jf.setVisible(true);
jf.setSize(300, 100);
jb.addMouseListener(this);
}
class MyThread extends Thread {
public void run(){
System.out.println(isButtonReleased);
while (!isButtonReleased) {
jl.setText(jl.getText() + "a");
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("mousePressed");
isButtonPressed = true;
isButtonReleased = false;
if(!isThreadStarted) {
new MyThread().start();
isThreadStarted = true;
}
}
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("mouseReleased");
isButtonReleased = true;
isThreadStarted = false;
}
}
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
public class InteractionDemo {
private Display display = null;
private Shell shell = null;
private Label label = null;
private Button button = null;
private java.util.Timer t;
public InteractionDemo() {
display = Display.getDefault();
shell = new Shell( display );
shell.setText("Demo");
shell.setLayout( new FillLayout() );
label = new Label(shell,SWT.BORDER);
label.setText("Label Demo");
button = new Button(shell,SWT.NONE);
button.setText("click me");
button.addMouseListener( new MouseListener() {
public void mouseDoubleClick(MouseEvent arg0) {
}
public void mouseDown(MouseEvent arg0) {
t = new java.util.Timer(true);
t.schedule(new java.util.TimerTask() {
public void run() {
display.asyncExec( new Runnable() {
public void run() {
label.setText( label.getText()+" a" );
}
} );
}
}, 100,500);
}
public void mouseUp(MouseEvent arg0) {
t.cancel();
}
} );
shell.setSize(500, 80);
shell.open();
while( !shell.isDisposed() ){
if( !display.readAndDispatch() )
display.sleep();
}
display.dispose();
}
/**
* @param args
*/
public static void main(String[] args) {
new InteractionDemo();
}
}
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
public class InteractionDemo {
private Display display = null;
private Shell shell = null;
private Label label = null;
private Button button = null;
public InteractionDemo() {
display = Display.getDefault();
shell = new Shell( display );
shell.setText("Demo");
shell.setLayout( new FillLayout() );
label = new Label(shell,SWT.BORDER);
label.setText("Label Demo");
button = new Button(shell,SWT.NONE);
button.setText("click me");
final ActionThread actionThread = new ActionThread();
actionThread.start();
button.addMouseListener( new MouseListener() {
public void mouseDoubleClick(MouseEvent arg0) {
}
public void mouseDown(MouseEvent arg0) {
actionThread.setClicked( true );
}
public void mouseUp(MouseEvent arg0) {
actionThread.setClicked( false );
}
} );
shell.pack();
shell.open();
while( !shell.isDisposed() ){
if( !display.readAndDispatch() )
display.sleep();
}
display.dispose();
}
class ActionThread extends Thread {
private boolean isClicked = false;
public void setClicked( boolean isClicked ) {
this.isClicked = isClicked;
}
public void run() {
while( true ) {
try {
Thread.sleep(1000);
} catch(InterruptedException e) {
e.printStackTrace();
}
if( !display.isDisposed() )
display.asyncExec( new Runnable() {
public void run() {
if( isClicked )
label.setText( label.getText()+" a" );
}
} );
else
break;
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
new InteractionDemo();
}
}
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
public class test extends JFrame{
static java.util.Timer t;
public test(){
Container c = this.getContentPane();
JLabel jl = new JLabel("sss");
jl.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
t = new java.util.Timer(true);
t.schedule(new TimerTask() {
int i=0;
public void run() {
System.out.println(i++);
}
}, 300,50);
}
public void mouseReleased(MouseEvent e){
t.cancel();
}
});
c.add(jl);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300,300);
this.setVisible(true);
}
public static void main(String args[]){
new test();
}
}
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class a extends Thread{
private int i=0;
private boolean b=false;
public a(){
start();
}
public void run(){
while(true){
try{
synchronized(this){
if(!b)
wait();
}
}catch(Exception e2){}
System.out.println(i++);
}
}
public void kill(){
b = false;
}
public void wake(){
b = true;
interrupt();
}
}
public class test extends JFrame{
static a t = new a();
public test(){
Container c = this.getContentPane();
JLabel jl = new JLabel("sss");
jl.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
t.wake();
}
public void mouseReleased(MouseEvent e){
t.kill();
}
});
c.add(jl);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300,300);
this.setVisible(true);
}
public static void main(String args[]){
new test();
}
}