示例#1
0
 public void actionPerformed(ActionEvent e) {
   if (e.getSource() == b1) {
     System.out.println("You Clicked Click me");
     System.out.println("And the textarea has: " + text.getText());
     x = x + 10;
     y = y + 10;
     canvas.update(canvas.getGraphics());
   } else if (e.getSource() == labeler) {
     label.setText(text.getText());
     // pane.add(new JLabel(text.getText()));
     text.setText("");
   } else if (e.getSource() == b2) {
     System.out.println("Shutting down");
     System.exit(0);
   }
 }
 // Konstruktordefinition
 public InnereKlassemitmainMethode() {
   super("Programmaufruf über eine innere Klasse");
   setBounds(10, 10, 250, 150);
   getContentPane().setLayout(new BorderLayout());
   malflaeche = new Canvas();
   malflaeche.setBackground(Color.pink);
   malflaeche.setSize(100, 100);
   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   malflaeche.addMouseMotionListener(this);
   status = new JLabel(" ");
   getContentPane().add(status, BorderLayout.SOUTH);
   // Eine lokale Referenz vom Typ des Interface Action zeigt auf
   // ein Objekt vom Typ der inneren Klasse SelectColorAction
   Action select = new SelectColorAction();
   // Zu einer JToolBar-Komponente wird eine JButton-Komponente,
   // welche die vorher definierte Aktion ausführt, hinzugefügt
   JToolBar toolbar = new JToolBar("Farbe wählen");
   JButton b = toolbar.add(select);
   getContentPane().add(toolbar, BorderLayout.WEST);
   getContentPane().add(malflaeche, BorderLayout.EAST);
   setVisible(true);
   // Den Graphikkontext des Canvas ermitteln
   g = malflaeche.getGraphics();
 }