000
25.05.2004, 15:44 Uhr
kaihua
|
Hier are zwei Java Progrmme :
1.Programm
Code: |
package counter;
import java.awt.event.*; import java.awt.*; import javax.swing.*;
class CounterPanel extends Canvas implements ActionListener, MouseListener { private Timer timer; private int number = 0; CounterPanel() { addMouseListener(this); // Timer(int delay, ActionListener a), delay in ms timer = new Timer(500, this); timer.setInitialDelay(0); timer.start(); }
public void mousePressed(MouseEvent me) { if (timer.isRunning()) timer.stop(); else timer.start(); } public void mouseClicked(MouseEvent me){} public void mouseReleased(MouseEvent me){} public void mouseEntered(MouseEvent me){} public void mouseExited(MouseEvent me){}
public void paint(Graphics g) { g.drawString("Counter: " + number + "S", 20, 40); }
public void actionPerformed(ActionEvent e) { number++; repaint(); } }
class CreateAndShowGUI extends JFrame { public CreateAndShowGUI() { //Make sure we have nice window decorations. setDefaultLookAndFeelDecorated(true);
//Create and set up the window setTitle("Counter"); setSize(300,200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = getContentPane(); contentPane.add(new CounterPanel()); // setVisible(true); } } public class Counter { public static void main(String [] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { new CreateAndShowGUI().show(); } }); } }
|
2.Programm
Code: |
package counter;
import java.awt.event.*; import java.awt.*; import javax.swing.*;
// Im Vergleich zum ersten Programm ist hier extends JPanel anstaat // extends Canvas, das Ergebnis ist dann anders: man kann // das number nicht genau sehen, weil es überlappt. // Jetzt die Frage ist, wenn ich unbedingt extends JPanel will, // was soll ich machen, damit die number auch klar sehbar? class CounterPanel extends JPanel implements ActionListener, MouseListener { private Timer timer; private int number = 0; CounterPanel() { addMouseListener(this); // Timer(int delay, ActionListener a), delay in ms timer = new Timer(500, this); timer.setInitialDelay(0); timer.start(); }
public void mousePressed(MouseEvent me) { if (timer.isRunning()) timer.stop(); else timer.start(); } public void mouseClicked(MouseEvent me){} public void mouseReleased(MouseEvent me){} public void mouseEntered(MouseEvent me){} public void mouseExited(MouseEvent me){}
public void paint(Graphics g) { g.drawString("Counter: " + number + "S", 20, 40); }
public void actionPerformed(ActionEvent e) { number++; repaint(); } }
class CreateAndShowGUI extends JFrame { public CreateAndShowGUI() { //Make sure we have nice window decorations. setDefaultLookAndFeelDecorated(true);
//Create and set up the window setTitle("Counter"); setSize(300,200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = getContentPane(); contentPane.add(new CounterPanel()); // setVisible(true); } } public class Counter { public static void main(String [] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { new CreateAndShowGUI().show(); } }); } }
|
|