Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
|
coding:java [2025/05/07 08:14] |
coding:java [2025/05/07 08:14] (aktuell) |
||
|---|---|---|---|
| Zeile 1: | Zeile 1: | ||
| + | Java ist eine objektorientierte Programmiersprache und gleichzeitig eine Plattform, die von Sun Microsystems (heute Teil von Oracle) in den 1990er Jahren entwickelt wurde. Sie zeichnet sich durch Plattformunabhängigkeit, | ||
| + | Build tools | ||
| + | |||
| + | * gradle | ||
| + | * maven | ||
| + | * ant | ||
| + | |||
| + | [[http:// | ||
| + | |||
| + | =====Aufbau===== | ||
| + | |||
| + | <code java> | ||
| + | public class HelloWorld { | ||
| + | public static void main(String[] args) { | ||
| + | System.out.println(" | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | javac HelloWorld.java | ||
| + | java HelloWorld | ||
| + | </ | ||
| + | |||
| + | =====Jar===== | ||
| + | |||
| + | Eine JAR-Datei (.jar) ist ein ZIP-komprimiertes Archiv, das eine oder mehrere .class-Dateien, | ||
| + | |||
| + | ====Aufbau==== | ||
| + | |||
| + | <box green> | ||
| + | |||
| + | < | ||
| + | MyApp.jar | ||
| + | ├── META-INF/ | ||
| + | │ | ||
| + | ├── com/ | ||
| + | │ | ||
| + | │ | ||
| + | ├── config.properties | ||
| + | └── logo.png | ||
| + | </ | ||
| + | |||
| + | manifest.mf | ||
| + | < | ||
| + | Main-Class: Main | ||
| + | Class-Path: MyProg.jar | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | javac *.java // create class files | ||
| + | jar -cmvf manifest.mf MyProg.jar *.class // create .jar, add all class files and the manifest | ||
| + | java -jar MyProg.jar | ||
| + | |||
| + | java -cp .; | ||
| + | </ | ||
| + | |||
| + | [[https:// | ||
| + | |||
| + | =====JFrame===== | ||
| + | |||
| + | <code java> | ||
| + | import javax.swing.*; | ||
| + | import java.awt.event.*; | ||
| + | |||
| + | public class Main extends JFrame { | ||
| + | | ||
| + | public Main() { | ||
| + | initComponents(); | ||
| + | } | ||
| + | | ||
| + | private void initComponents() { | ||
| + | |||
| + | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
| + | JPanel panel = new JPanel(); | ||
| + | JButton button = new JButton(" | ||
| + | button.addActionListener(new ActionListener() { | ||
| + | @Override | ||
| + | public void actionPerformed(ActionEvent e) { | ||
| + | System.out.println(" | ||
| + | } | ||
| + | }); | ||
| + | panel.add(button); | ||
| + | add(panel); | ||
| + | pack(); | ||
| + | setVisible(true); | ||
| + | |||
| + | } | ||
| + | |||
| + | public static void main(String args[]) { | ||
| + | |||
| + | java.awt.EventQueue.invokeLater(new Runnable() { | ||
| + | public void run() { | ||
| + | new Main().setVisible(true); | ||
| + | } | ||
| + | }); | ||
| + | |||
| + | } | ||
| + | | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | =====Layouts===== | ||
| + | |||
| + | In Java wird das Erstellen von Layouts üblicherweise mit Hilfe von sogenannten Layout-Managern (Layout Managers) erreicht. Layout-Manager sind Klassen, die dabei helfen, die Positionierung und Größenanpassung von Komponenten in einem Container zu verwalten. Sie ermöglichen es, dass die Benutzeroberfläche flexibel und automatisch angepasst wird, wenn sich die Größe des Fensters ändert oder Komponenten hinzugefügt oder entfernt werden. Es gibt verschiedene Layout-Manager, | ||
| + | |||
| + | * BorderLayout: | ||
| + | * FlowLayout: Dieser Layout-Manager platziert die Komponenten in der Reihenfolge, | ||
| + | * GridLayout: Dieser Layout-Manager platziert die Komponenten in einem gleichmäßigen Raster mit einer festen Anzahl von Zeilen und Spalten. | ||
| + | * GridBagLayout: | ||
| + | |||
| + | ====BorderLayout==== | ||
| + | |||
| + | <code java> | ||
| + | import javax.swing.*; | ||
| + | import java.awt.*; | ||
| + | |||
| + | public class Main { | ||
| + | |||
| + | public static void main(String[] args) { | ||
| + | |||
| + | // Erstellen des Hauptfensters (JFrame) | ||
| + | JFrame frame = new JFrame(" | ||
| + | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
| + | | ||
| + | // Erstellen der Komponenten | ||
| + | JButton button1 = new JButton(" | ||
| + | JButton button2 = new JButton(" | ||
| + | JLabel label = new JLabel(" | ||
| + | | ||
| + | // BorderLayout verwenden | ||
| + | frame.setLayout(new BorderLayout()); | ||
| + | | ||
| + | // Komponenten dem BorderLayout hinzufügen | ||
| + | frame.add(button1, | ||
| + | frame.add(button2, | ||
| + | frame.add(label, | ||
| + | | ||
| + | // FlowLayout für einen JPanel verwenden | ||
| + | JPanel panel = new JPanel(new FlowLayout()); | ||
| + | panel.add(new JButton(" | ||
| + | panel.add(new JButton(" | ||
| + | | ||
| + | // JPanel dem BorderLayout hinzufügen | ||
| + | frame.add(panel, | ||
| + | | ||
| + | // Größe des Fensters anpassen und sichtbar machen | ||
| + | frame.pack(); | ||
| + | frame.setVisible(true); | ||
| + | |||
| + | } | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | |||
| + | ====FlowLayout==== | ||
| + | |||
| + | <code java> | ||
| + | import javax.swing.*; | ||
| + | import java.awt.*; | ||
| + | |||
| + | public class Main { | ||
| + | |||
| + | public static void main(String[] args) { | ||
| + | |||
| + | // Erstellen des Hauptfensters (JFrame) | ||
| + | JFrame frame = new JFrame(" | ||
| + | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
| + | | ||
| + | // Erstellen des Panels mit FlowLayout | ||
| + | JPanel panel = new JPanel(new FlowLayout()); | ||
| + | | ||
| + | // Hinzufügen von Komponenten zum Panel | ||
| + | panel.add(new JButton(" | ||
| + | panel.add(new JButton(" | ||
| + | panel.add(new JButton(" | ||
| + | | ||
| + | // JPanel dem JFrame hinzufügen | ||
| + | frame.add(panel); | ||
| + | | ||
| + | // Größe des Fensters anpassen und sichtbar machen | ||
| + | frame.pack(); | ||
| + | frame.setVisible(true); | ||
| + | |||
| + | } | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | |||
| + | ====GridLayout==== | ||
| + | |||
| + | <code java> | ||
| + | import javax.swing.*; | ||
| + | import java.awt.*; | ||
| + | |||
| + | public class Main { | ||
| + | |||
| + | public static void main(String[] args) { | ||
| + | |||
| + | // Erstellen des Hauptfensters (JFrame) | ||
| + | JFrame frame = new JFrame(" | ||
| + | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
| + | | ||
| + | // Erstellen des Panels mit GridLayout | ||
| + | JPanel panel = new JPanel(new GridLayout(3, | ||
| + | | ||
| + | // Hinzufügen von Komponenten zum Panel | ||
| + | panel.add(new JButton(" | ||
| + | panel.add(new JButton(" | ||
| + | panel.add(new JButton(" | ||
| + | panel.add(new JButton(" | ||
| + | panel.add(new JButton(" | ||
| + | panel.add(new JButton(" | ||
| + | panel.add(new JButton(" | ||
| + | panel.add(new JButton(" | ||
| + | panel.add(new JButton(" | ||
| + | | ||
| + | // JPanel dem JFrame hinzufügen | ||
| + | frame.add(panel); | ||
| + | | ||
| + | // Größe des Fensters anpassen und sichtbar machen | ||
| + | frame.pack(); | ||
| + | frame.setVisible(true); | ||
| + | |||
| + | } | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | |||
| + | ====GridBagLayout==== | ||
| + | |||
| + | <code java> | ||
| + | import javax.swing.*; | ||
| + | import java.awt.*; | ||
| + | |||
| + | public class Main { | ||
| + | |||
| + | public static void main(String[] args) { | ||
| + | |||
| + | // Erstellen des Hauptfensters (JFrame) | ||
| + | JFrame frame = new JFrame(" | ||
| + | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
| + | |||
| + | // Erstellen des Panels mit GridBagLayout | ||
| + | JPanel panel = new JPanel(new GridBagLayout()); | ||
| + | GridBagConstraints gbc = new GridBagConstraints(); | ||
| + | |||
| + | // Hinzufügen von Komponenten zum Panel | ||
| + | gbc.gridx = 0; | ||
| + | gbc.gridy = 0; | ||
| + | gbc.insets = new Insets(5, 5, 5, 5); // Abstand um die Komponenten | ||
| + | panel.add(new JButton(" | ||
| + | |||
| + | gbc.gridx = 1; | ||
| + | gbc.gridy = 0; | ||
| + | panel.add(new JButton(" | ||
| + | |||
| + | gbc.gridx = 0; | ||
| + | gbc.gridy = 1; | ||
| + | gbc.gridwidth = 2; // Komponente erstreckt sich über zwei Spalten | ||
| + | panel.add(new JButton(" | ||
| + | |||
| + | gbc.gridx = 0; | ||
| + | gbc.gridy = 2; | ||
| + | gbc.gridwidth = 1; // Zurücksetzen auf Standard | ||
| + | panel.add(new JButton(" | ||
| + | |||
| + | gbc.gridx = 1; | ||
| + | gbc.gridy = 2; | ||
| + | panel.add(new JButton(" | ||
| + | |||
| + | // JPanel dem JFrame hinzufügen | ||
| + | frame.add(panel); | ||
| + | |||
| + | // Größe des Fensters anpassen und sichtbar machen | ||
| + | frame.pack(); | ||
| + | frame.setVisible(true); | ||
| + | |||
| + | } | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | =====Threads===== | ||
| + | |||
| + | Threads ermöglichen die gleichzeitige oder nebenläufige Ausführung von Aufgaben und verbessern so die Leistung und Reaktionsfähigkeit von Programmen. Sie können verwendet werden, um mehrere Operationen gleichzeitig auszuführen, | ||
| + | |||
| + | In Java können Threads auf zwei Arten erstellt werden: | ||
| + | |||
| + | * Durch Vererbung der Klasse Thread: Man erstellt eine eigene Klasse, die von der Klasse Thread erbt und die run()-Methode überschreibt. Die run()-Methode enthält den Code, der im Thread ausgeführt werden soll. Anschließend kann man eine Instanz dieser Klasse erstellen und den Thread mit der Methode start() starten. | ||
| + | * Durch Implementierung des Runnable-Interfaces: | ||
| + | |||
| + | Threads in Java bieten verschiedene Funktionen zur Synchronisation und Koordination, | ||
| + | |||
| + | <code java> | ||
| + | public class MyThread extends Thread { | ||
| + | |||
| + | @Override | ||
| + | public void run() { | ||
| + | |||
| + | // Code, der im Thread ausgeführt werden soll | ||
| + | for (int i = 1; i <= 10; i++) { | ||
| + | |||
| + | System.out.println(" | ||
| + | try { | ||
| + | Thread.sleep(1000); | ||
| + | } catch (InterruptedException e) { | ||
| + | e.printStackTrace(); | ||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | public static void main(String[] args) { | ||
| + | |||
| + | MyThread myThread = new MyThread(); | ||
| + | |||
| + | myThread.start(); | ||
| + | |||
| + | // Code, der parallel zum Thread ausgeführt wird | ||
| + | for (int i = 1; i <= 5; i++) { | ||
| + | |||
| + | System.out.println(" | ||
| + | try { | ||
| + | Thread.sleep(1000); | ||
| + | } catch (InterruptedException e) { | ||
| + | e.printStackTrace(); | ||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | =====Runnable===== | ||
| + | |||
| + | Ein Runnable ist eine abstrakte Darstellung einer ausführbaren Aufgabe, die in einem Thread ausgeführt werden kann. Es definiert die Methode run(), die den Code enthält, der im Thread ausgeführt werden soll. | ||
| + | |||
| + | <code java> | ||
| + | class MyRunnable implements Runnable { | ||
| + | public void run() { | ||
| + | while(true) { | ||
| + | int i = 1; | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | public class Main { | ||
| + | public static void main(String[] args) { | ||
| + | // Erstelle ein Runnable-Objekt | ||
| + | Runnable myRunnable = new MyRunnable(); | ||
| + | | ||
| + | // Erstelle einen neuen Thread mit dem Runnable-Objekt | ||
| + | Thread thread = new Thread(myRunnable); | ||
| + | | ||
| + | // Starte den Thread | ||
| + | thread.start(); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | =====Code===== | ||
| + | |||
| + | Server | ||
| + | |||
| + | <code java> | ||
| + | import java.io.*; | ||
| + | import java.net.*; | ||
| + | import java.*; | ||
| + | import javax.swing.*; | ||
| + | import java.util.ArrayList; | ||
| + | import java.util.Arrays; | ||
| + | import java.util.List; | ||
| + | import java.util.Iterator; | ||
| + | |||
| + | public class Server extends Thread implements Runnable { | ||
| + | | ||
| + | public ClientThread clients[]; | ||
| + | public ServerSocket server = null; | ||
| + | public Thread thread = null; | ||
| + | public int clientCount = 0; | ||
| + | public int port; | ||
| + | |||
| + | public Server(int port) { | ||
| + | this.port = port; | ||
| + | int maxclients = 100; | ||
| + | clients = new ClientThread[maxclients]; | ||
| + | try{ | ||
| + | server = new ServerSocket(port); | ||
| + | start(); | ||
| + | } catch(IOException ioe) { } | ||
| + | } | ||
| + | | ||
| + | public void run() { | ||
| + | while (thread != null){ | ||
| + | try { | ||
| + | addThread(server.accept()); | ||
| + | } catch(Exception ioe){ } | ||
| + | } | ||
| + | if (thread == null){ | ||
| + | thread = new Thread(this); | ||
| + | thread.start(); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | public void stopen() { | ||
| + | for(int i = 0; i < clientCount; | ||
| + | try { server.close(); | ||
| + | } | ||
| + | | ||
| + | private void addThread(Socket socket) { | ||
| + | if (clientCount < clients.length) { | ||
| + | clients[clientCount] = new ClientThread(this, | ||
| + | try{ | ||
| + | clients[clientCount].open(); | ||
| + | clients[clientCount].start(); | ||
| + | clientCount++; | ||
| + | } catch(IOException ioe) { } | ||
| + | } else { } | ||
| + | } | ||
| + | | ||
| + | public synchronized void handle(int ID, String msg) { | ||
| + | System.out.println(msg); | ||
| + | } | ||
| + | | ||
| + | public void Announce(int ID, String content) { | ||
| + | for(int i = 0; i < clientCount; | ||
| + | if (clients[i].ID != ID){ | ||
| + | clients[i].send(content); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | | ||
| + | private int findClient(int ID) { | ||
| + | for (int i = 0; i < clientCount; | ||
| + | if (clients[i].ID == ID) return i; | ||
| + | } | ||
| + | return -1; | ||
| + | } | ||
| + | | ||
| + | public ClientThread findUserThread(String usr) { | ||
| + | for(int i = 0; i < clientCount; | ||
| + | // | ||
| + | } | ||
| + | return null; | ||
| + | } | ||
| + | |||
| + | @SuppressWarnings(" | ||
| + | public synchronized void remove(int ID) { | ||
| + | int pos = findClient(ID); | ||
| + | if (pos >= 0){ | ||
| + | ClientThread toTerminate = clients[pos]; | ||
| + | if (pos < clientCount-1) { | ||
| + | for (int i = pos+1; i < clientCount; | ||
| + | clients[i-1] = clients[i]; | ||
| + | } | ||
| + | } | ||
| + | clientCount--; | ||
| + | try { | ||
| + | toTerminate.close(); | ||
| + | } catch(IOException ioe) { | ||
| + | } | ||
| + | toTerminate.stop(); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | } | ||
| + | | ||
| + | class ClientThread extends Thread { | ||
| + | |||
| + | public Server server = null; | ||
| + | public Socket socket = null; | ||
| + | | ||
| + | public int ID = -1; | ||
| + | public BufferedReader r = null; | ||
| + | public PrintWriter w = null; | ||
| + | |||
| + | public ClientThread(Server _server, Socket _socket) { | ||
| + | super(); | ||
| + | server = _server; | ||
| + | socket = _socket; | ||
| + | ID = socket.getPort(); | ||
| + | } | ||
| + | | ||
| + | @SuppressWarnings(" | ||
| + | public void run() { | ||
| + | while (true){ | ||
| + | try { | ||
| + | server.handle(ID, | ||
| + | } catch(Exception ioe) { | ||
| + | server.remove(ID); | ||
| + | stop(); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | public void open() throws IOException { | ||
| + | r = new BufferedReader(new InputStreamReader(socket.getInputStream())); | ||
| + | w = new PrintWriter(socket.getOutputStream()); | ||
| + | w.flush(); | ||
| + | } | ||
| + | |||
| + | public void close() throws IOException { | ||
| + | if (socket != null) socket.close(); | ||
| + | if (r != null) r.close(); | ||
| + | if (w != null) w.close(); | ||
| + | } | ||
| + | | ||
| + | public void send(String msg) { | ||
| + | w.write(msg); | ||
| + | w.flush(); | ||
| + | } | ||
| + | |||
| + | | ||
| + | } | ||
| + | </ | ||