Medientechnik Übung – Java Swing Heute • (GUI-)Programmieren mit Eclipse • Java Swing – Fenster erstellen – GUI-Komponenten hinzufügen – Layout-Manager nutzen Links http://download.oracle.com/javase/tutorial/uiswing/ http://openbook.galileocomputing.de/javainsel/ http://zetcode.com/tutorials/javaswingtutorial/ Eclipse • Programmierwerkzeug (ursprünglich integrierte Entwicklungsumgebung für Java, mittlerweile auch für viele andere Programmiersprachen) • Vorteile: – Verschiedene Views – Syntaxhighlighting – Code Completion – Code-Folding – … • Shortcuts: – Strg+Leertaste: Code Completion – Strg+Linksklick auf Funktionsaufruf: Sprung zur Deklaration der Funktion – Cursor über Variable: alle Vorkommen der Variable werden farbig unterlegt Eclipse • Eclipse starten und Workspace festlegen • Neues Java-Projekt anlegen • 2 neue Klassen im default-package erstellen: – Yaca.java – View.java Java – main-Methode public class Yaca { public static void main(String[] args) { View yacaView = new View(); yacaView.setVisible(true); } } Yaca.java Swing - JFrame import javax.swing.*; public class View extends JFrame { public View() { this.setTitle(“YetAnotherCalculator”); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } View.java Swing - JPanel import javax.swing.*; public class View extends JFrame { public View() { this.setTitle(“YetAnotherCalculator”); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel contentAll = new JPanel(); contentAll.setLayout(new BorderLayout()); } } View.java Swing – JButton import java.awt.*; import javax.swing.*; public class View extends JFrame { public View() { […] JButton start = new JButton("Start"); JButton clear = new JButton(“Clear"); contentAll.add(start, BorderLayout.SOUTH); contentAll.add(clear, BorderLayout.SOUTH); this.setContentPane(contentAll); this.pack(); } } View.java Swing – JButton import java.awt.*; import javax.swing.*; public class View extends JFrame { public View() { […] JButton start = new JButton("Start"); JButton clear = new JButton(“Clear"); JPanel contentButtons = new JPanel(); contentButtons.setLayout(new FlowLayout()); contentButtons.add(start); contentButtons.add(clear); contentAll.add(contentButtons, BorderLayout.SOUTH); this.setContentPane(contentAll); this.pack(); } } View.java Swing – JTextField & JComboBox import java.awt.*; import javax.swing.*; public class View extends JFrame { public View() { […] JTextField firstInput JTextField secondInput JTextField result String[] methods JComboBox methodBox = = = = new JTextField(5); new JTextField(5); new JTextField(5); {"+", "-", "*", "/"}; = new JComboBox(methods); this.setContentPane(contentAll); this.pack(); } } View.java Swing – JTextField & JComboBox import java.awt.*; import javax.swing.*; public class View extends JFrame { public View() { […] JPanel contentInput = new JPanel(new FlowLayout()); contentInput.add(firstInput); contentInput.add(methodBox); contentInput.add(secondInput); contentInput.add(new JLabel("=")); contentInput.add(result); contentAll.add(contentInput, BorderLayout.CENTER); this.setContentPane(contentAll); this.pack(); } } View.java Swing – default-Werte ändern import java.awt.*; import javax.swing.*; public class View extends JFrame { public View() { […] firstInput.setText("0"); secondInput.setText("0"); result.setText("-"); result.setEditable(false); methodBox.setSelectedIndex(2); this.setContentPane(contentAll); this.pack(); } } View.java Swing – Window-Position import java.awt.*; import javax.swing.*; public class View extends JFrame { public View() { […] this.setContentPane(contentAll); this.pack(); this.setLocation(100, 100); } } View.java