Tugas Pertemuan 14 PBO

Nama  : Moh. Rizky Rahmadian Makkani

NRP    : 5025231035

Kelas : PBO A

Tugas Pertemuan 14 : PBO


1. Membuat frame windows user login dan password.

Code LoginWindow.java :

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginWindow {
public static void main(String[] args) {
// Membuat frame
JFrame frame = new JFrame("Login");
frame.setSize(300, 150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(3, 2, 5, 5));

// Komponen GUI
JLabel usernameLabel = new JLabel("Username:");
JTextField usernameField = new JTextField();
JLabel passwordLabel = new JLabel("Password:");
JPasswordField passwordField = new JPasswordField();
JButton loginButton = new JButton("Login");
JButton cancelButton = new JButton("Cancel");

// Tambahkan komponen ke frame
frame.add(usernameLabel);
frame.add(usernameField);
frame.add(passwordLabel);
frame.add(passwordField);
frame.add(loginButton);
frame.add(cancelButton);

// Event handling untuk tombol login
loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String username = usernameField.getText();
String password = new String(passwordField.getPassword());

// Validasi login
if (username.equals("admin") && password.equals("12345")) {
JOptionPane.showMessageDialog(frame, "Login Berhasil!");
frame.dispose(); // Tutup jendela login
ImageViewer.showImageViewer(); // Panggil aplikasi Image Viewer
} else {
JOptionPane.showMessageDialog(frame, "Login Gagal. Periksa username atau password.");
}
}
});

// Event handling untuk tombol cancel
cancelButton.addActionListener(e -> frame.dispose());

// Tampilkan frame
frame.setLocationRelativeTo(null); // Pusatkan layar
frame.setVisible(true);
}
}

Output :

Output ketika salah memasukkan Username/Password :


Output ketika Username/Password yang dimasukkan benar :


2. Implementasikan aplikasi image viewer.

Code ImageViewer.java :

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 showImageViewer() {
// Membuat frame
JFrame frame = new JFrame("Image Viewer");
frame.setSize(600, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());

// Komponen GUI
JLabel imageLabel = new JLabel("No Image Selected", SwingConstants.CENTER);
imageLabel.setFont(new Font("Arial", Font.BOLD, 16));
imageLabel.setHorizontalAlignment(SwingConstants.CENTER);
JButton openButton = new JButton("Open Image");

// Tambahkan komponen ke frame
frame.add(imageLabel, BorderLayout.CENTER);
frame.add(openButton, BorderLayout.SOUTH);

// Event handling untuk tombol "Open Image"
openButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Dialog file chooser
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.setFileFilter(new javax.swing.filechooser.FileNameExtensionFilter(
"Image Files", "jpg", "jpeg", "png", "gif"));

int result = fileChooser.showOpenDialog(frame);
if (result == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
ImageIcon imageIcon = new ImageIcon(selectedFile.getAbsolutePath());
Image scaledImage = imageIcon.getImage().getScaledInstance(
imageLabel.getWidth(), imageLabel.getHeight(), Image.SCALE_SMOOTH);
imageLabel.setIcon(new ImageIcon(scaledImage));
imageLabel.setText(""); // Hapus teks placeholder
}
}
});

// Tampilkan frame
frame.setLocationRelativeTo(null); // Pusatkan layar
frame.setVisible(true);
}
}

Output :



Komentar

Postingan populer dari blog ini

Tugas 2 PBO : Konsep OOP

Tugas 1 PWEB E