I
ivanstojkovic1000
Gast
Ich weiss nicht wo ich die Fehler mache....
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class myConstruction implements ActionListener {
JFrame myFrame;
JPanel myPanel;
JTextField myTextField;
JLabel myLabel1;
JButton myPrintButton;
public myConstruction() {
//create the Window
myFrame = new JFrame("Ausdruck der Name");
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setSize(new Dimension(120,40));
//create the Panel
myPanel = new JPanel(new GridLayout(2,2));
//adding the elements to the Panel
addElements();
//setting default button
myFrame.getRootPane().setDefaultButton(myPrintButton);
//add the Panel to Window
myFrame.getContentPane().add(myPanel, BorderLayout.CENTER);
//show the window
myFrame.pack();
myFrame.setVisible(true);
}
public void addElements() {
//create Elements
myTextField = new JTextField(2);
myLabel1 = new JLabel("Name: ", SwingConstants.LEFT);
myPrintButton = new JButton("ausdrucken");
//listen to events from ausdrucken button
myPrintButton.addActionListener(this);
//add the Elements to the Window
myFrame.add(myTextField);
myFrame.add(myLabel1);
myFrame.add(myPrintButton);
//setting the border for labels
myLabel1.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
}
//action performed
public void actionPerformed(ActionEvent event) {
//printing the name that was in input
String myText = (String)(myTextField.getText());
myLabel1.setText("Gruss " + myText);
}
private static void createAndShowGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
myConstruction myConstr = new myConstruction();
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}