Code:
package swings_project;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class klick_mich_2_mit_info extends JApplet
{
private JButton b1 = new JButton("Button 1"), b2 = new JButton("Button 2");
private JTextField txt = new JTextField(10);
class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String name = ((JButton) e.getSource()).getText();
txt.setText(name);
}
}
private ButtonListener bl = new ButtonListener();
public void init()
{
b1.addActionListener(bl);
b2.addActionListener(bl);
Container cp = getContentPane();
cp.setLayout(new FlowLayout());
cp.add(b1);
cp.add(b2);
cp.add(txt);
}
public static void main(String[] args)
{
run(new klick_mich_2_mit_info(), 200, 100);
}
public static void run(JApplet applet, int width, int height)
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(applet);
frame.setSize(width, height);
applet.init();
applet.start();
frame.setVisible(true);
}
}
ich schaue mir gerade ein Bsp an zu Swings, und manches verstehe ich hier nicht.
1.
Code:
private JTextField txt = new JTextField(10);
Diese 10 am Ende bedeutet, dass dieser Text maximal aus 10 Zeichen bestehen kann?
2.
Code:
class ButtonListener implements ActionListener
Dann erbt diese Klasse ActionListener. In der API steht zu ActionListener folgendes:
The listener interface for receiving action events. The class that is interested in processing an action event implements this interface, and the object created with that class is registered with a component, using the component's addActionListener method. When the action event occurs, that object's actionPerformed method is invoked.
Könnte jemand bitte das auf Deutsch übersetzen und so einfach wie es geht erklären? ^^ Also mit ganz einfachen Wörtern. Habe das mit einem Online Übersetzer übersetzt und nichts verstanden.
3.
Code:
public void actionPerformed(ActionEvent e)
Diese Methode gehört zu ActionListener oder? Wenn ich API richtig verstehe, dann ist das auch die einzige, muss ich wissen was diese Methode macht? Wenn ja dann bitte erklären :bae:
in der API steht
und was heißt das auf Deutsch?Invoked when an action occurs
4.
Code:
public void init()
5.
Code:
Container cp = getContentPane();
In der API steht :
A generic Abstract Window Toolkit(AWT) container object is a component that can contain other AWT components.
Components added to a container are tracked in a list. The order of the list will define the components' front-to-back stacking order within the container. If no index is specified when adding a component to a container, it will be added to the end of the list (and hence to the bottom of the stacking order).
Habe das natürlich auch nicht verstanden :cry: . Bitte erklären
Ok das wars fürs Erste :bae:
Danke im Voraus