Grafiken, Threads und GUIs
Aus ZUM-Unterrichten
< Java
Dieses Kapitel ist ein Exkurskapitel. Es zeigt, wie man mit Hilfe von Java GUIs (Graphical User Interface, Grafische Benutzeroberflächen programmieren kann.
In der ersten Version wurden Java-Applets vorgestellt, die aber künftig aus den Standard entfernt werden.
ToDo (weitere ToDos)
Tutorial und Beipiele einer GUI, die mit Hilfe von Netbeans entworden wurden. --Matthias Scharwies (Diskussion) 06:37, 19. Aug. 2019 (CEST)
Zähler ohne GUI
Quelltext zum Beispiel Zähler mit GUI (liegt als Bildschirmfilm vor) - unter Netbeans entworfen.
Die Klasse Zaehler ohne GUI.
public class Zaehler {
private int zaehler;
public Zaehler() {
zaehler = 0;
}
public int getZaehler() {
return zaehler;
}
public void erhoeheZaehler(){
zaehler++;
}
}
Zähler mit GUI
GUI zum Zähler, benutzt die Klasse Zaehler.
/*
* ZaehlerGUI.java
*
* Created on 9. Januar 2007, 09:46
*/
/**
*
* @author ugh
*/
public class ZaehlerGUI extends javax.swing.JFrame {
private Zaehler z;
/** Creates new form ZaehlerGUI */
public ZaehlerGUI() {
initComponents();
z = new Zaehler();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
button1 = new java.awt.Button();
textField1 = new java.awt.TextField();
label1 = new java.awt.Label();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
button1.setLabel("Erh\u00f6hen um 1");
button1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
button1ActionPerformed(evt);
}
});
textField1.setText("0");
label1.setText("Ein Z\u00e4hler als Beispiel f\u00fcr den Entwurf eines GUI");
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(100, 100, 100)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
.add(org.jdesktop.layout.GroupLayout.LEADING, textField1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(org.jdesktop.layout.GroupLayout.LEADING, button1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.add(layout.createSequentialGroup()
.addContainerGap()
.add(label1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 270, Short.MAX_VALUE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.add(label1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 29, Short.MAX_VALUE)
.add(textField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(button1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(70, 70, 70))
);
pack();
}// </editor-fold>
private void button1ActionPerformed(java.awt.event.ActionEvent evt) {
z.erhoeheZaehler();
textField1.setText(""+z.getZaehler());
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ZaehlerGUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private java.awt.Button button1;
private java.awt.Label label1;
private java.awt.TextField textField1;
// End of variables declaration
}