
利用wait() notify() 写了一个。
import javaawtBorderLayout;
import javaawtColor;
import javaawtContainer;
import javaawtFlowLayout;
import javaawteventActionEvent;
import javaawteventActionListener;
import javaxswingJButton;
import javaxswingJFrame;
import javaxswingJPanel;
import javaxswingJTextField;
class ThreadClass extends JTextField implements Runnable {
//--- flag 控制线程状态 ---
private boolean flag = true;
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
thisflag = flag;
}
//--- 通过累计计数模拟线程运行---
int times = 0;
//--- 设置文本框主要是显示线程状态 ---
public ThreadClass(String s) {
super(s);
setBackground(ColorORANGE);//---橘黄
setEditable(false);
setHorizontalAlignment(JTextFieldCENTER);
}
@Override
public void run() {
process();
}
private synchronized void process() {
while (true) {//无限循环
if (flag == true) {
thissetText("This is the " + (times++)
+ "th times running \n");
try {
Threadsleep(100);
} catch (InterruptedException ie) {
ieprintStackTrace();
}
notify();//--- 唤醒另一线程 --- ,本代码其实没有其他等待线程,这个可不用 ---
} else {
thissetText(" When times = " + times + "\n The thread is paused!");
try {
wait();//--- 进入等待
} catch (InterruptedException ie) {
ieprintStackTrace();
}
}
}
}
}
public class ThreadControll extends JFrame {
ThreadClass a = new ThreadClass("请按开始按钮!");
Thread startThread = null;
public ThreadControll(String s) {
super(s);
final Container c = getContentPane();
csetLayout(new BorderLayout());
JPanel jp = new JPanel(new FlowLayout());
JButton button_start = new JButton("开始");
JButton button_pause = new JButton("暂停");
JButton button_resume = new JButton("恢复");
jpadd(button_start);
jpadd(button_pause);
jpadd(button_resume);
//--- 开始按钮只执行一次 ---
button_startaddActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
if (null == startThread) {
startThread = new Thread(a);
startThreadstart();
}
}
});
//--- 暂停按钮 ,按下后如果没有按继续,就不改变标志 ---
button_pauseaddActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (aisFlag() != false) {
asetFlag(false);
}
}
});
//--- 恢复按钮,这个最关键,主要是用notify()让线程继续执行,---
button_resumeaddActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (aisFlag() == false) {
asetFlag(true);
synchronized (a) {//--- 如果不放在同步快里,无法实现唤醒 ---
anotify();//--- 这个代码执行完后,a对象锁就释放了 所以process() 方法里德 notify()可不用---
}
}
}
});
cadd(jp,BorderLayoutNORTH);
cadd(a, BorderLayoutCENTER);
}
//--- 入口 ---
public static void main(String[] args) {
ThreadControll tc = new ThreadControll("test");
tcsetSize(400, 200);
tcsetVisible(true);
tcsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
}
}
你建立了JPanel,但是没有把JPanel添加到JFrame中。
在HeadLine构造函数中添加:
thisadd(pane);
thissetVisible(true);
就ok了。
程序有点简陋,不过功能倒是全部实现了,你自己看吧
有些参数你自己可以调整
//这个程序哎,以后有机会再改进吧,至少坐标的移动我写的太烦了,可以直接指定的,擦
import javaawtBorderLayout;
import javaawtColor;
import javaawtEventQueue;
import javaawtGraphics;
import javaawtGraphics2D;
import javaawtgeomLine2D;
import javaawtgeomPoint2D;
import javaxswingJComponent;
import javaxswingJFrame;
public class JFrame1 extends JFrame {
private JPanel1 jp;
public static void main(String[] args) {
EventQueueinvokeLater(new Runnable() {
public void run() {
try {
JFrame1 frame = new JFrame1();
} catch (Exception e) {
eprintStackTrace();
}
}
});
}
public JFrame1() {
jp = new JPanel1();
jpsetBackground(ColorBLACK);
setDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
getContentPane()setLayout(new BorderLayout(0, 0));
thissetBackground(ColorBLACK);
thissetContentPane(jp);
thissetSize(800, 600);
thissetLocationRelativeTo(null);
thissetVisible(true);
}
}
class JPanel1 extends JComponent{
int width,height;
int zx,zy;
int unit = 100; //单位长度包含的像素,也可考虑分别设置x,y轴的单位长度像素
public void paintComponent(Graphics g){
gsetColor(ColorBLACK);
width = thisgetWidth();
height = thisgetHeight();
// zx = 100;
// zy = 200;
zx = width/2; //新坐标系原点坐标位置,可以改变
zy = height/2;
gsetColor(ColorBLACK);
thispaintAxis(g);
thispaintMethod(g);
thispaintMethod1(g);
thispaintMethod2(g);
}
private void paintMethod(Graphics g1){
Point2D temp1,temp2;
double x,y;
Graphics2D g = (Graphics2D)g1;
gsetColor(ColorGREEN);
x = -10zx/unit;
y = Mathpow(x, 3) ;
temp1 = new Point2DDouble(thisalterX(x unit), thisalterY(y unit));
for(int i = 0 ; i < width; i++){
x += 10/unit;
y = Mathpow(x, 3) ;
if ( Mathabs(y) < zy){
temp2 = new Point2DDouble(thisalterX(x unit), thisalterY(y unit));
gdraw(new Line2DDouble(temp1, temp2));
temp1 = temp2;
}
}
}
private void paintMethod1(Graphics g1){
Point2D temp1,temp2;
double x,y;
Graphics2D g = (Graphics2D)g1;
gsetColor(ColorBLUE);
x = -10zx/unit;
y = Mathpow(MathE, x) ;
temp1 = new Point2DDouble(thisalterX(x unit), thisalterY(y unit));
for(int i = 0 ; i < width; i++){
x += 10/unit;
y = Mathpow(MathE, x) ;
if ( Mathabs(y) < zy){
temp2 = new Point2DDouble(thisalterX(x unit), thisalterY(y unit));
gdraw(new Line2DDouble(temp1, temp2));
temp1 = temp2;
}
}
}
private void paintMethod2(Graphics g1){
Point2D temp1,temp2;
double x,y;
Graphics2D g = (Graphics2D)g1;
gsetColor(ColorPINK);
x = -10zx/unit;
y = Mathsin(MathPI x);
temp1 = new Point2DDouble(thisalterX(x unit), thisalterY(y unit));
for(int i = 0 ; i < width; i++){
x += 10/unit;
y = Mathsin(MathPI x);
if ( Mathabs(y) < zy){
temp2 = new Point2DDouble(thisalterX(x unit), thisalterY(y unit));
gdraw(new Line2DDouble(temp1, temp2));
temp1 = temp2;
}
}
}
//画坐标轴,可以通过zx,zy的处置来改变原点位置;
private void paintAxis(Graphics g){
gsetColor(ColorCYAN);
gdrawLine(0, zy, width, zy);
gdrawLine(zx, 0, zx, height);
gdrawString("0",zx + 2,zy +12); //画原点数字
for(int i = 1; iunit < width; i++){
gdrawLine(zx + iunit, zy - 10, zx + iunit, zy);//x 正向
gdrawLine(zx - iunit, zy - 10, zx - iunit, zy);//x 负向
gdrawString(StringvalueOf(i), zx + iunit -3, zy + 12); // x轴数字
gdrawString(StringvalueOf(i -1), zx - iunit -8, zy + 12); // x轴数字
gdrawLine(zx , zy + iunit, zx + 10, zy + iunit);//y 负向
gdrawLine(zx , zy - iunit, zx + 10, zy - iunit);//y 正向
gdrawString(StringvalueOf(i), zx -12 , zy - iunit + 5); // y轴数字
gdrawString(StringvalueOf(i -1), zx -12 , zy + iunit + 5); // y轴数字
}
}
private double alterX(double x){ //新坐标对应的原坐标
return x + zx;
}
private double alterY(double y){ //新坐标对应的原坐标
return -1 ( y - zy);
}
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)