// ActionDemo3.java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ActionDemo3 extends JApplet implements ActionListener { private Container container; private JTextField enter;

kitewormsΛογισμικό & κατασκευή λογ/κού

3 Νοε 2013 (πριν από 3 χρόνια και 10 μήνες)

97 εμφανίσεις

一、事件监听


//

ActionDemo3
.java

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class ActionDemo3 extends JApplet implements ActionListener {


private Container container;


private JTextField enter;


private JTextArea area;



private int n;


public void init() {


container = getContentPane();


container.setLayout(new FlowLayout());


enter = new JTextField(10);


enter.addActionListener(this);


container.add(enter);


area = new JTex
tArea(8,30);


container.add(area);


}


public void actionPerformed(ActionEvent event){


n = Integer.parseInt(event.getActionCommand());


String s = getMul(n);


area.setText(s);


}


public String getMul(int n)
{


String output = "";


for(int i=1; i<=n; i++){


for(int j=1; j<=i; j++)


output += j + "*" + i + "=" + i*j + " ";


output += "
\
n";


}


return output;


}

}


//ActionDemo1.java

import ja
va.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class ActionDemo1 extends JApplet {


private Container container;


private JTextField enter;


private JTextArea area;


private int n;


private ActionHander hander = new ActionDe
mo1.ActionHander();


public void init() {


container = getContentPane();


container.setLayout(new FlowLayout());


enter = new JTextField(10);


enter.addActionListener(hander);


container.add(enter);


area = new
JTextArea(8,30);


container.add(area);


}


private class ActionHander implements ActionListener {


public void actionPerformed(ActionEvent event){


n = Integer.parseInt(event.getActionCommand());


String s
= getMul(n);


area.setText(s);


}


}


public String getMul(int n){


String output = "";


for(int i=1; i<=n; i++){


for(int j=1; j<=i; j++)


output += j + "*" + i + "=" + i*j + " ";



output += "
\
n";


}


return output;


}

}


//ActionDemo2.java

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class ActionDemo2 extends JApplet {


private Container container;


private JTextField enter;


p
rivate JTextArea area;


private int n;


public void init() {


container = getContentPane();


container.setLayout(new FlowLayout());


enter = new JTextField(10);


enter.addActionListener(new ActionListener() {


p
ublic void actionPerformed(ActionEvent event){


n = Integer.parseInt(event.getActionCommand());


String s = getMul(n);


area.setText(s);


}


}


);


container.add(enter);


a
rea = new JTextArea(8,30);


container.add(area);


}


public String getMul(int n){


String output = "";


for(int i=1; i<=n; i++){


for(int j=1; j<=i; j++)


output += j + "*" + i + "=" + i*j +
" ";


output += "
\
n";


}


return output;


}

}



//ActionDemoA.java

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class ActionDemoA extends JFrame implements ActionListener {


private Container con
tainer;


private JTextField enter;


private JTextArea area;


private int n;


public ActionDemoA() {


super("ActionDemo");


container = getContentPane();


container.setLayout(new FlowLayout());


enter = new JTextField
(10);


enter.addActionListener(this);


container.add(enter);


area = new JTextArea(8,30);


container.add(area);


setSize(400,
22
0);


show();


}


public void actionPerformed(ActionEvent event){


n = Int
eger.parseInt(event.getActionCommand());


String s = getMul(n);


area.setText(s);


}


public static void main(String args[]){


ActionDemoA application = new ActionDemoA();


application.setDefaultCloseOperation(JFrame.EXIT_
ON_CLOSE);


}


public String getMul(int n){


String output = "";


for(int i=1; i<=n; i++){


for(int j=1; j<=i; j++)


output += j + "*" + i + "=" + i*j + " ";


output += "
\
n";


}


retur
n output;


}

}





二、多线程:
缓冲区同步的生产者
/
消费者问题

//

SharedData
.java

public class SharedData {


private final int size = 3;


private int data[] = {
-
1,
-
1,
-
1,
-
1,
-
1,
-
1,
-
1,
-
1,
-
1,
-
1},rp=0,wp=0;


private boolean writeable = true,

readable = false;


publi
c synchronized void writeData(int d) {


while(!writeable){


try{


wait();


}


catch(InterruptedException ep){


ep.printStackTrace();


}


}


data[wp] = d;



wp = (wp + 1) % size;


readable = true;


if(wp==rp)


writeable = false;


System.err.println("Write dada " + d);


notify();


}


public synchronized int readData() {


while(!readable){


try{



wait();


}


catch(InterruptedException ep){


ep.printStackTrace();


}


}


int vd=data[rp];


writeable = true;


rp = (rp + 1) % size;


if(wp==rp)


rea
dable = false;


System.err.println("Read dada "+vd);


notify();


return vd;


}

}



//

Producer
.java

public class Producer implements Runnable {


SharedData sharedData;


public Producer(SharedData sd) {


sharedData = sd;


}


public void run(){


for(int i=1;i<=10;i++){


sharedData.writeData(i);


}


}

}


//

Consummer
.java

public class Consummer implements Runnable {


SharedData sharedData;


public Consummer(SharedData sd) {


s
haredData = sd;


}


public void run(){


int i;


do {


i = sharedData.readData();


}while (i!=10);


}

}


//

ProducerAndConsummer
.java

public class ProducerAndConsummer {


public static void main(String[] args) {



SharedData sharedData = new SharedData();


Producer producer = new Producer(sharedData);


Consummer consummer = new Consummer(sharedData);


Thread th1 = new Thread(producer);


Thread th2 = new Thread(consummer);


t
h1.start();


th2.start();


System.exit(0);


}

}





多线程:挂起和激活

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class DispChar extends JApplet implements ActionListener {


private final int SIZE = 3;


private JLabel label[];


private JCheckBox checkBox[];


private boolean
suspend[];


private Thread thread[];


private Container container;


private String latters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";


public DispChar() {


label = new JLabel[SIZE];


checkBox = new JCheckBox[SIZE];


suspend = new boo
lean[SIZE];


thread = new Thread[SIZE];


}


public void init(){


setSize(400,200);


container = getContentPane();


container.setLayout(new GridLayout(3,2,5,5));


for(int i=0;i<label.length;i++){


label[i]

= new JLabel();


label[i].setBackground(Color.green);


label[i].setOpaque(true);


container.add(label[i]);


checkBox[i] = new JCheckBox("Suspend");


checkBox[i].addActionListener(this);


co
ntainer.add(checkBox[i]);


}


}


public void start(){


for(int i=0;i<thread.length;i++){


thread[i] = new Thread(new RunnableObject(),"Thread "+(i+1));


thread[i].start();


}


}


public synchronized vo
id stop(){


for(int i=0;i<thread.length;i++){


thread[i] = null;


}


notifyAll();


}


public int getIndex(Thread current){


for(int i=0;i<thread.length;i++)


if(thread[i]==current)


ret
urn i;


return
-
1;


}


public synchronized void actionPerformed(ActionEvent event){


for(int i=0;i<checkBox.length;i++){


if(event.getSource()==checkBox[i]){


suspend[i] = !suspend[i];


label[i].
setBackground(suspend[i]?Color.red:Color.green);


}


}


notifyAll();


}


private class RunnableObject implements Runnable {


public void run() {


final Thread current = Thread.currentThread();


fi
nal int index = getIndex(current);


while(thread[index]==current){


try{


Thread.sleep((int)(1000*Math.random()));


synchronized(DispChar.this){


while(suspend[index] &&

thread[index]==current){


DispChar.this.wait();


}


}


}


catch(InterruptedException exception){


System.err.println(current.getName()+"
Interrupted !");


}


SwingUtilities.invokeLater( new Runnable() {


public void run(){


label[index].setText(current.getName()+" :
"+latters.charAt((int)(Math.random()*26)));



}


}


);


}


System.err.println(current.getName()+" Terminating !");


}


}

}


透明窗体

import java.awt.*;

import javax.swing.*;

public class FormDemo extends JFrame{


JPanel jp;


Ima
ge Background;


Point p0;


Point p1;


int startx,starty,endx,endy;


public FormDemo() {


jp = new JPanel();


Toolkit tk =Toolkit.getDefaultToolkit();


Dimension dm = tk.getScreenSize();


this.getContentPane().add(jp)
;


try {


Robot r = new Robot();


Background = r.createScreenCapture(new Rectangle(0,0,(int)dm.getWidth(),
(int)dm.getHeight()));


} catch(Exception e) {


//e.printStackTrace();


}


this.resize((int)dm.getWidth(), (int)dm.getHeight()+31);


//this.setSize(300,300);


this.setLocation(0,
-
30);


this.setVisible(true);


}


public void paint(Graphics g) {


p0 = this.getLocationOnScreen();


p1
= new Point(
-
p0.x,
-
p0.y);


//draw background


g.drawImage(Background, p1.x, p1.y, null);


//draw Rect


int width=startx>endx?startx
-
endx:endx
-
startx;


int height=starty>endy?starty
-
endy:endy
-
starty;


if(endy>start
y)


g.drawRect(startx+2,starty+31,width
-
4,height
-
31);


else


g.drawRect(endx,endy,width,height+31);


}


public static void main(String args[])


{


new FormDemo();


}

}