Пятница, 2025-02-07, 23:43:51
Главная Регистрация RSS
Приветствую Вас, Гость
[ Новые сообщения · Участники · Правила форума · Поиск · RSS ]
  • Страница 1 из 1
  • 1
24.10 потоки
CHerryДата: Понедельник, 2011-10-24, 19:55:40 | Сообщение # 1
Генералиссимус
Группа: Администраторы
Сообщений: 141
Репутация: 3732
Статус: Offline
Code
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
     
public class jResponsiveUIwThread extends JFrame {
    private boolean stop = false;
    private JTextField tfCount;
    private int countValue = 1;
     
    public jResponsiveUIwThread() {
       Container cp = getContentPane();
       cp.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
       cp.add(new JLabel("Counter"));
       tfCount = new JTextField("" + countValue, 10);
       tfCount.setEditable(false);
       cp.add(tfCount);
     
       JButton btnStart = new JButton("Start Counting");
       cp.add(btnStart);
       btnStart.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent evt) {
             stop = false;
             Thread t = new Thread() {
                public void run() {
                   for (int i = 0; i < 100000; i++) {
                      if (stop) break;
                      tfCount.setText("" + countValue);
                      countValue++;
                      // suspend this thread via sleep() and yield control to other threads
                      try {
                         sleep(10);
                      } catch (InterruptedException ex) {}
                   }
                }
             };
             t.start();
          }
       });
       JButton btnStop = new JButton("Stop Counting");
       cp.add(btnStop);
       btnStop.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent evt) { stop = true; }
       });
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       setTitle("Counter");
       setSize(300, 120);
       setVisible(true);
    }
     
    public static void main(String[] args) {
       new jResponsiveUIwThread();
    }
}

 
CHerryДата: Понедельник, 2011-10-24, 20:18:25 | Сообщение # 2
Генералиссимус
Группа: Администраторы
Сообщений: 141
Репутация: 3732
Статус: Offline
Code
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
     
public class jResponsiveUIwEventDispatcher extends JFrame {
    private boolean stop = false;
    private JTextField tfCount;
    private int countValue = 1;
     
    // Create the GUI and show it. For thread safety, this method should
    // be invoked from the event-dispatching thread instead of the main thread
    private void createAndShowGUI() {
       Container cp = getContentPane();
       cp.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
       cp.add(new JLabel("Counter"));
       tfCount = new JTextField("" + countValue, 10);
       tfCount.setEditable(false);
       cp.add(tfCount);
     
       JButton btnStart = new JButton("Start Counting");
       cp.add(btnStart);
       btnStart.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent evt) {
             stop = false;
             // start a new thread to do the counting
             Thread t = new Thread() {
                public void run() {
                   while (!stop) {
                      tfCount.setText("" + countValue);
                      countValue++;
                      // suspend itself and yield control to other threads
                      try {
                         sleep(10);
                      } catch (InterruptedException ex) {}
                   }
                }
             };
             t.start();
          }
       });
       JButton btnStop = new JButton("Stop Counting");
       cp.add(btnStop);
       btnStop.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent evt) {
             stop = true;
          }
       });
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       setSize(300, 120);
       setTitle("Counter");
       setVisible(true);   // show and realize the window
    }
     
    public static void main(String[] args) {
       // The main thread schedules a job for the event-dispatching thread to create the GUI
       javax.swing.SwingUtilities.invokeLater(new Runnable() {
          public void run() {
             new jResponsiveUIwEventDispatcher().createAndShowGUI();
          }
       });
    }
}
 
DimonДата: Понедельник, 2011-10-24, 22:03:40 | Сообщение # 3
Рядовой
Группа: Проверенные
Сообщений: 11
Репутация: 0
Статус: Offline
А вот то, что я успел сделать на занятии, на основе примера про многопоточный таймер.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class jResponsiveUIwEventDispatcher extends JFrame {
private boolean stop1 = false;
private boolean stop2 = false;
private JTextField tfCount1,tfCount2 ;
private int countValue1 = 1;
private int countValue2 = 1;

// Create the GUI and show it. For thread safety, this method should
// be invoked from the event-dispatching thread instead of the main thread
private void createAndShowGUI() {
Container cp = getContentPane();
cp.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
cp.add(new JLabel("Counter1"));
cp.add(new JLabel("Counter2"));
tfCount1 = new JTextField("" + countValue1, 10);
tfCount1.setLocation(10, 10);
tfCount2 = new JTextField("" + countValue2, 10);
tfCount1.setEditable(false);
tfCount2.setEditable(false);
cp.add(tfCount1);
cp.add(tfCount2);

final JButton btnStart1 = new JButton("Start Counting1");
final JButton btnStart2 = new JButton("Start Counting2");

cp.add(btnStart1);
cp.add(btnStart2);


btnStart1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
stop1 = false;
// start a new thread to do the counting
Thread t1 = new Thread() {
public void run() {
btnStart1.setEnabled(false);
while (!stop1) {
tfCount1.setText("" + countValue1);
countValue1++;
// suspend itself and yield control to other threads
try {
sleep(1000);
} catch (InterruptedException ex) {}
}
}
};
t1.start();
}
});


btnStart2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
stop2 = false;
// start a new thread to do the counting
Thread t2 = new Thread() {
public void run() {
btnStart2.setEnabled(false);
while (!stop2) {
tfCount2.setText("" + countValue2);
countValue2++;
// suspend itself and yield control to other threads
try {
sleep(1000);
} catch (InterruptedException ex) {}
}
}
};
t2.start();
}
});



JButton btnStop1 = new JButton("Stop Counting1");
JButton btnStop2 = new JButton("Stop Counting2");

cp.add(btnStop1);
cp.add(btnStop2);

btnStop1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
stop1 = true;
btnStart1.setEnabled(true);
}
});

btnStop2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
stop2 = true;
btnStart2.setEnabled(true);
}
});

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 120);
setTitle("Counter");
setVisible(true); // show and realize the window
}

public static void main(String[] args) {
// The main thread schedules a job for the event-dispatching thread to create the GUI
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new jResponsiveUIwEventDispatcher().createAndShowGUI();
}
});
}
}
 
  • Страница 1 из 1
  • 1
Поиск: