Belajar GUI
Nama: Azka Rizqullah Ramadhani
NRP: 5025231148
Kelas: Pemrograman Berbasis Objek (A)
1. Membuat Form login dengan GUI
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class LoginFrame {
public static void main(String[] args) {
JFrame frame = new JFrame("Login Form");
frame.setSize(350, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
JPanel panel = new JPanel();
frame.add(panel);
placeComponents(panel);
frame.setVisible(true);
}
private static void placeComponents(JPanel panel) {
panel.setLayout(null);
JLabel userLabel = new JLabel("Username:");
userLabel.setBounds(10, 20, 80, 25);
panel.add(userLabel);
JTextField userText = new JTextField(20);
userText.setBounds(100, 20, 165, 25);
panel.add(userText);
JLabel passwordLabel = new JLabel("Password:");
passwordLabel.setBounds(10, 50, 80, 25);
panel.add(passwordLabel);
JPasswordField passwordText = new JPasswordField(20);
passwordText.setBounds(100, 50, 165, 25);
panel.add(passwordText);
JButton loginButton = new JButton("Login");
loginButton.setBounds(10, 80, 80, 25);
panel.add(loginButton);
JButton exitButton = new JButton("Exit");
exitButton.setBounds(180, 80, 80, 25);
panel.add(exitButton);
loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String username = userText.getText();
String password = new String(passwordText.getPassword());
if ("admin".equals(username) && "password123".equals(password)) {
JOptionPane.showMessageDialog(panel, "Login Berhasil!");
} else {
JOptionPane.showMessageDialog(panel, "Username atau Password salah!", "Error", JOptionPane.ERROR_MESSAGE);
}
}
});
exitButton.addActionListener(e -> System.exit(0));
}
}
2. Membuat Image Viewer dengan GUI
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
public class ImageViewer {
public static void main(String[] args) {
JFrame frame = new JFrame("Image Viewer");
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
frame.add(panel);
JLabel imageLabel = new JLabel("", SwingConstants.CENTER);
panel.add(imageLabel, BorderLayout.CENTER);
JButton loadButton = new JButton("Load Image");
panel.add(loadButton, BorderLayout.SOUTH);
loadButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.setDialogTitle("Select an Image");
fileChooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
@Override
public boolean accept(File f) {
if (f.isDirectory()) return true;
String name = f.getName().toLowerCase();
return name.endsWith(".jpg") || name.endsWith(".png") || name.endsWith(".gif");
}
@Override
public String getDescription() {
return "Image Files (*.jpg, *.png, *.gif)";
}
});
int result = fileChooser.showOpenDialog(frame);
if (result == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
ImageIcon imageIcon = new ImageIcon(selectedFile.getAbsolutePath());
Image image = imageIcon.getImage();
Image resizedImage = image.getScaledInstance(imageLabel.getWidth(), imageLabel.getHeight(), Image.SCALE_SMOOTH);
imageLabel.setIcon(new ImageIcon(resizedImage));
}
}
});
frame.setVisible(true);
}
}
Komentar
Posting Komentar