From 5ebc47cc35e0a48b3979e6c541da6958cbdecab9 Mon Sep 17 00:00:00 2001 From: rattatwinko Date: Wed, 28 May 2025 17:29:50 +0200 Subject: [PATCH] learned some more --- src/main/java/org/jview/Main.java | 5 ++ .../org/jview/SwingExamples/FileChooser.java | 56 +++++++++++++++++++ .../org/jview/SwingExamples/InputExample.java | 32 +++++++++++ .../{ => SwingExamples}/MyFirstFrame.java | 4 +- .../java/org/jview/SwingExamples/counter.java | 44 +++++++++++++++ .../java/org/jview/SwingExamples/ctf.java | 42 ++++++++++++++ .../java/org/jview/SwingExamples/todo.java | 54 ++++++++++++++++++ 7 files changed, 235 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/jview/SwingExamples/FileChooser.java create mode 100644 src/main/java/org/jview/SwingExamples/InputExample.java rename src/main/java/org/jview/{ => SwingExamples}/MyFirstFrame.java (89%) create mode 100644 src/main/java/org/jview/SwingExamples/counter.java create mode 100644 src/main/java/org/jview/SwingExamples/ctf.java create mode 100644 src/main/java/org/jview/SwingExamples/todo.java diff --git a/src/main/java/org/jview/Main.java b/src/main/java/org/jview/Main.java index d04cb01..ad5b9ea 100644 --- a/src/main/java/org/jview/Main.java +++ b/src/main/java/org/jview/Main.java @@ -4,5 +4,10 @@ import javax.swing.*; class Main { public static void main(String[] args) { + System.out.println( + "Please check the SwingExamples Directory for more Examples \n" + + "This is only a learning Repository! \n" + + "This is not a Full Scale Application like the other Repos! \n" + ); } } \ No newline at end of file diff --git a/src/main/java/org/jview/SwingExamples/FileChooser.java b/src/main/java/org/jview/SwingExamples/FileChooser.java new file mode 100644 index 0000000..5a5f442 --- /dev/null +++ b/src/main/java/org/jview/SwingExamples/FileChooser.java @@ -0,0 +1,56 @@ +package org.jview.SwingExamples; + +import javax.swing.*; +import java.io.File; + +public class FileChooser { + public static void main(String[] args) { + JFrame frame = new JFrame(); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setTitle("FileChooser"); + frame.setSize(400, 150); + frame.setLocationRelativeTo(null); + + /* + Here we show the File Chooser, but instead of letting you select only Files + we let you select only Directories! + */ + JButton directoryButton = new JButton("Choose Directory"); + directoryButton.addActionListener(e -> { + JFileChooser fileChooser = new JFileChooser(); + fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); + fileChooser.setAcceptAllFileFilterUsed(false); + + if (fileChooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) { + File file = fileChooser.getSelectedFile(); + System.out.println(file.getAbsolutePath()); // Output the Path! + JOptionPane.showMessageDialog(frame, "Selected Directory:\n" + file.getAbsolutePath()); + } + }); + + /* + Add a Lambda Function to show the File Chooser, + Also set it to FILES_ONLY. + This tells Java Swing to only let you select Files, not Folders! + */ + JButton fileButton = new JButton("Choose File"); + fileButton.addActionListener(e -> { + JFileChooser fileChooser = new JFileChooser(); + fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); + + if (fileChooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) { + File file = fileChooser.getSelectedFile(); + System.out.println(file.getAbsolutePath()); + JOptionPane.showMessageDialog(frame, "Selected File:\n" + file.getAbsolutePath()); + } + }); + + // Make everything Display + JPanel panel = new JPanel(); + panel.add(directoryButton); + panel.add(fileButton); + frame.add(panel); + + frame.setVisible(true); + } +} diff --git a/src/main/java/org/jview/SwingExamples/InputExample.java b/src/main/java/org/jview/SwingExamples/InputExample.java new file mode 100644 index 0000000..a16bef0 --- /dev/null +++ b/src/main/java/org/jview/SwingExamples/InputExample.java @@ -0,0 +1,32 @@ +package org.jview.SwingExamples; + +import javax.swing.*; + +public class InputExample { + public static void main(String[] args) { + // Initialize Swing with JFrame + JFrame frame = new JFrame("Input Example"); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + // Add Input / Button / The Inputs Output + JTextField input = new JTextField(15); + JButton button = new JButton("Enter"); + JLabel label = new JLabel(); + + // Create a Lambda to output the Text entered + button.addActionListener(e -> { + String str = input.getText(); + label.setText(str); + }); + + // Make everything visible + JPanel panel = new JPanel(); + panel.add(input); + panel.add(button); + panel.add(label); + frame.add(panel); + frame.setVisible(true); + + frame.setSize(100, 100); + } +} diff --git a/src/main/java/org/jview/MyFirstFrame.java b/src/main/java/org/jview/SwingExamples/MyFirstFrame.java similarity index 89% rename from src/main/java/org/jview/MyFirstFrame.java rename to src/main/java/org/jview/SwingExamples/MyFirstFrame.java index aeec2aa..052a021 100644 --- a/src/main/java/org/jview/MyFirstFrame.java +++ b/src/main/java/org/jview/SwingExamples/MyFirstFrame.java @@ -1,6 +1,5 @@ -package org.jview; +package org.jview.SwingExamples; import javax.swing.*; // Swing components -import java.awt.*; // Layouts and more import java.awt.event.*; // Event handling public class MyFirstFrame { @@ -21,6 +20,7 @@ public class MyFirstFrame { button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("Hello World"); + JOptionPane.showMessageDialog(frame, "Hello World"); } }); diff --git a/src/main/java/org/jview/SwingExamples/counter.java b/src/main/java/org/jview/SwingExamples/counter.java new file mode 100644 index 0000000..697a7ec --- /dev/null +++ b/src/main/java/org/jview/SwingExamples/counter.java @@ -0,0 +1,44 @@ +package org.jview.SwingExamples; + +import javax.swing.*; + +public class counter { + public static void main(String[] args) { + JFrame frame = new JFrame(); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setTitle("Counter"); + frame.setSize(500, 500); + frame.setLocationRelativeTo(null); + + int[] count = {0}; + JButton button = new JButton("Increment"); + JButton button2 = new JButton("Decrement"); + JLabel label = new JLabel("Count: 0"); + + button.addActionListener(e -> { + count[0]++; + label.setText("Count: " + count[0]); + }); + + button2.addActionListener(e -> { + count[0]--; + label.setText("Count: " + count[0]); + }); + + JPanel panel = new JPanel(); + panel.add(button); + panel.add(button2); + + JPanel labelPanel = new JPanel(); + labelPanel.add(label); + + JPanel mainPanel = new JPanel(); + mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS)); + mainPanel.add(labelPanel); + mainPanel.add(panel); + + frame.add(mainPanel); + + frame.setVisible(true); + } +} diff --git a/src/main/java/org/jview/SwingExamples/ctf.java b/src/main/java/org/jview/SwingExamples/ctf.java new file mode 100644 index 0000000..eabe3b7 --- /dev/null +++ b/src/main/java/org/jview/SwingExamples/ctf.java @@ -0,0 +1,42 @@ +package org.jview.SwingExamples; + +import javax.swing.*; + +public class ctf { + public static void main(String[] args) { + JFrame jFrame = new JFrame(); + jFrame.setTitle("Celsius to Fahrenheit"); + jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + jFrame.setSize(250, 200); + + JTextField textField = new JTextField(20); + JButton button = new JButton("Calculate"); + JLabel label = new JLabel(); + + button.addActionListener(e -> { + try { + double celsuis = Double.parseDouble(textField.getText()); + double fahrenheit = celsuis * 1.8 + 32; + label.setText(String.format("%.2f", fahrenheit)); + } catch (NumberFormatException ex) { + JOptionPane.showMessageDialog(jFrame, ex.getMessage() + " is not a number"); + }}); + + JPanel mainPanel = new JPanel(); + mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS)); + + JPanel inputPanel = new JPanel(); + inputPanel.add(textField); + inputPanel.add(button); + + JPanel labelPanel = new JPanel(); + labelPanel.add(label); + + mainPanel.add(inputPanel); + mainPanel.add(labelPanel); + jFrame.add(mainPanel); + + // This is always required to show the window + jFrame.setVisible(true); + } +} diff --git a/src/main/java/org/jview/SwingExamples/todo.java b/src/main/java/org/jview/SwingExamples/todo.java new file mode 100644 index 0000000..4d0d4cf --- /dev/null +++ b/src/main/java/org/jview/SwingExamples/todo.java @@ -0,0 +1,54 @@ +package org.jview.SwingExamples; + +import javax.swing.*; + +public class todo { + public static void main(String[] args) { + // Add the Swing BoilerPlate + JFrame frame = new JFrame(); + frame.setTitle("Simple To-Do List"); + frame.setSize(500, 500); + frame.setLocationRelativeTo(null); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + // Initialize a new List + DefaultListModel listModel = new DefaultListModel<>(); + JList list = new JList<>(listModel); + + // Initialize a new TextFiled and Button + final JPanel inputPanel = getJPanel(listModel); + + JScrollPane listScrollPane = new JScrollPane(list); + + JPanel mainPanel = new JPanel(); + mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS)); + + mainPanel.add(inputPanel); + mainPanel.add(listScrollPane); + + frame.add(mainPanel); + frame.setVisible(true); + } + + private static JPanel getJPanel(DefaultListModel listModel) { + JTextField textField = new JTextField(15); + JButton addButton = new JButton("Add"); + + /* + Add a Lambda Function that Adds the Text in the Panel to the List, + after that it sets the Text to an empty string! + */ + addButton.addActionListener(e -> { + String text = textField.getText().trim(); + if (!text.isEmpty()) { + listModel.addElement(text); + textField.setText(""); + } + }); + + JPanel inputPanel = new JPanel(); + inputPanel.add(textField); + inputPanel.add(addButton); + return inputPanel; + } +}