http://Bj83.de

Erste Schritte in Java

Umgebung / Komponenten:

  • Eclipse (ich bevorzuge die Android SDK da, da schon bissl was bei ist wie git usw..)
  • Java SDK (JDK Package)
  • GUI Designer (Unter Help -> Install new Software -> add new repo WindowBuilder – hab davon alles installiert wobei man sicher nicht alles braucht)

Erstes Fenster:

In Eclipse: File -> New -> Java Project

Project Name: “HelloWorld”

JRE: welche JDK Version ihr eben installiert habt, bei mir isses JavaSE 1.7 bzw JavaSE 1.6

-> Finish

im Package Explorer auf src rechtsklick -> New -> Other…

WindowBuilder -> Swing Designer -> Application Window

Name: HelloWorld (Wird der Appname mit den die Anwendung Startet)

-> Finish

private void initialize() {
	frame = new JFrame();
	frame.setBounds(100, 100, 450, 300);
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

bearbeiten in

private void initialize() {
	frame = new JFrame();
	frame.setBounds(100, 100, 450, 300);
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.getContentPane().setLayout(null);
}

Button und Textfeld hinzuf├╝gen

private void initialize() {
	frame = new JFrame();
	frame.setBounds(100, 100, 450, 300);
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.getContentPane().setLayout(null);

	textField = new JTextField();
	textField.setBounds(299, 173, 114, 19);
	frame.getContentPane().add(textField);
	textField.setColumns(10);

	JButton btnNewButton = new JButton("New button");
	btnNewButton.setBounds(41, 170, 117, 25);
	frame.getContentPane().add(btnNewButton);
}

Erste Klasse:

m Package Explorer auf src rechtsklick -> New -> Class

Name: “HWClass”

“Which method stubs would you like to create” -> keine

public class HWClass {
	public String Dummy(){
		return "Hallo Welt";
	}
}

Beides Verbinden:

Zeilen hinzuf├╝gen:

private void initialize() {
	frame = new JFrame();
	frame.setBounds(100, 100, 450, 300);
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.getContentPane().setLayout(null);

	textField = new JTextField();
	textField.setBounds(299, 173, 114, 19);
	frame.getContentPane().add(textField);
	textField.setColumns(10);

	JButton btnNewButton = new JButton("New button");
	btnNewButton.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
			HWClass hwc = new HWClass();
			textField.setText(hwc.Dummy());
		}
	});
	btnNewButton.setBounds(41, 170, 117, 25);
	frame.getContentPane().add(btnNewButton);
}

Nur so einfach moeglich wenn im gleichen Paket!

HelloWorld Java Source
HelloWorld Java Source
HelloWorld.zip
Version: 1.0
1.2 KiB
37 Downloads
Details

Leave a Reply

Your email address will not be published. Required fields are marked *

Follow

Get every new post on this blog delivered to your Inbox.

Join other followers: