Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejava
titleSwing exampleSwingeksempel
linenumberstrue
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;

public class GUIExample extends JFrame implements ActionListener  {
	
	JTextArea info = new JTextArea("Hello World!");
	JButton btn = new JButton("Does nothing");
	
	public GUIExample (String title) {
		
		setTitle(title);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		btn.addActionListener(this);
		
		this.createGUI();
	}
	
	private void createGUI() {
		setLayout(new BorderLayout());
		
		add(info, BorderLayout.NORTH);
		add(btn, BorderLayout.SOUTH);
		
	}
	
	public void actionPerformed(ActionEvent e) {
		
		if (e.getSource() == btn) {
			JOptionPane.showMessageDialog(null, "Did nothing!");
		}
	}
	
	public static void main(String[] args) {
		GUIExample gui = new GUIExample("My first swingcode");
		
		gui.pack();
		gui.setVisible(true);
	}
}

...