learned some more
This commit is contained in:
@@ -4,5 +4,10 @@ import javax.swing.*;
|
|||||||
|
|
||||||
class Main {
|
class Main {
|
||||||
public static void main(String[] args) {
|
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"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
56
src/main/java/org/jview/SwingExamples/FileChooser.java
Normal file
56
src/main/java/org/jview/SwingExamples/FileChooser.java
Normal file
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
32
src/main/java/org/jview/SwingExamples/InputExample.java
Normal file
32
src/main/java/org/jview/SwingExamples/InputExample.java
Normal file
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
package org.jview;
|
package org.jview.SwingExamples;
|
||||||
import javax.swing.*; // Swing components
|
import javax.swing.*; // Swing components
|
||||||
import java.awt.*; // Layouts and more
|
|
||||||
import java.awt.event.*; // Event handling
|
import java.awt.event.*; // Event handling
|
||||||
|
|
||||||
public class MyFirstFrame {
|
public class MyFirstFrame {
|
||||||
@@ -21,6 +20,7 @@ public class MyFirstFrame {
|
|||||||
button.addActionListener(new ActionListener() {
|
button.addActionListener(new ActionListener() {
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
System.out.println("Hello World");
|
System.out.println("Hello World");
|
||||||
|
JOptionPane.showMessageDialog(frame, "Hello World");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
44
src/main/java/org/jview/SwingExamples/counter.java
Normal file
44
src/main/java/org/jview/SwingExamples/counter.java
Normal file
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
42
src/main/java/org/jview/SwingExamples/ctf.java
Normal file
42
src/main/java/org/jview/SwingExamples/ctf.java
Normal file
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
54
src/main/java/org/jview/SwingExamples/todo.java
Normal file
54
src/main/java/org/jview/SwingExamples/todo.java
Normal file
@@ -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<String> listModel = new DefaultListModel<>();
|
||||||
|
JList<String> 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<String> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user