Hello,
May I know why the frame pops up double when I call it?
Thank You!
Login.java (line 63);
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class Login{
JFrame frame = new JFrame();
JButton loginButton = new JButton("Login");
JButton resetButton = new JButton("Reset");
JButton registerAccount = new JButton("Sign Up");
JTextField userIDField = new JTextField();
JTextField userPasswordField = new JTextField();
JLabel userIDLabel = new JLabel("Customer ID:");
JLabel userPasswordLabel = new JLabel("Password:");
JLabel messageLabel = new JLabel();
JLabel title = new JLabel("Platinum Screen Cinema");
JLabel title2 = new JLabel("Customer Login");
Login(){
title.setFont(new Font("Arial", Font.PLAIN, 30));
title.setBounds(75, 50, 350, 30);
title2.setFont(new Font("Arial", Font.PLAIN, 30));
title2.setBounds(140, 10, 300, 30);
userIDLabel.setBounds(50, 100, 75, 25);
userPasswordLabel.setBounds(50, 150, 75, 25);
messageLabel.setBounds(125,250,250,35);
messageLabel.setFont(new Font(null,Font.ITALIC,25));
userIDField.setBounds(125, 100, 300, 25);
userPasswordField.setBounds(125, 150, 300, 25);
loginButton.setBounds(125, 200, 100, 25);
loginButton.setFocusable(false);
loginButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt) {
try {
String lokasi = "customer.txt";
String username = userIDField.getText();
String password = userPasswordField.getText();
FileReader fr = new FileReader(lokasi);
BufferedReader br = new BufferedReader(fr);
String line;
String user, pass;
boolean isLoginSuccess = false;
while ((line = br.readLine()) != null) {
String[] splitString = line.split("\\s+");
user = splitString.length >1? splitString[0] : " ";
pass = splitString.length >1? splitString[1] : " ";
if (user.equals(username) && pass.equals(password)) {
isLoginSuccess = true;
frame.dispose();
new Booking().setVisible(true);
}
}
if (!isLoginSuccess) {
JOptionPane.showMessageDialog(null, "USERNAME/PASSWORD WRONG", "WARNING!!", JOptionPane.WARNING_MESSAGE);
}
fr.close();
} catch (Exception e) {
e.printStackTrace();
}
}
});
resetButton.setBounds(325, 200, 100, 25);
resetButton.setFocusable(false);
resetButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == resetButton) {
userIDField.setText("");
userPasswordField.setText("");
}
}
});
registerAccount.setBounds(225, 200, 100, 25);
registerAccount.setFocusable(false);
registerAccount.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource()== registerAccount) {
frame.dispose();
new Register();
}
}
});
frame.add(title);
frame.add(title2);
frame.add(userIDLabel);
frame.add(userPasswordLabel);
frame.add(messageLabel);
frame.add(registerAccount);
frame.add(userIDField);
frame.add(userPasswordField);
frame.add(loginButton);
frame.add(resetButton);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(520, 420);
frame.setLayout(null);
frame.setVisible(true);
}
}
Booking.java (line 378):
import java.awt.event.ActionEvent;
import java.io.FileWriter;
import javax.swing.JComboBox;
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template
*/
/**
*
* @author User
*/
public class Booking extends javax.swing.JFrame {
/**
* Creates new form Booking
*/
public Booking() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jComboBox1 = new javax.swing.JComboBox<>();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jPanel1 = new javax.swing.JPanel();
jLabel6 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
jLabel8 = new javax.swing.JLabel();
jLabel9 = new javax.swing.JLabel();
jLabel10 = new javax.swing.JLabel();
jLabel11 = new javax.swing.JLabel();
Hall = new javax.swing.JTextField();
Date = new javax.swing.JTextField();
Time = new javax.swing.JTextField();
Seats = new javax.swing.JTextField();
tTicket = new javax.swing.JTextField();
tPrice = new javax.swing.JTextField();
jComboBox2 = new javax.swing.JComboBox<>();
jComboBox3 = new javax.swing.JComboBox<>();
jButton1 = new javax.swing.JButton();
jLabel12 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setFont(new java.awt.Font("Segoe UI", 0, 18)); // NOI18N
jLabel1.setText("Platinum Screen Cinema");
jLabel2.setText("Now Showing :");
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Jurassic World: Dominion", "Top Gun: Maverick", "Doctor Strange In The Multiverse Of Madness", "Doraemon The Movie: Nobita's Little Star Wars 2021" }));
jComboBox1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jComboBox1ActionPerformed(evt);
}
});
jLabel4.setText("Booking Details :");
jLabel5.setText("Number of Ticket :");
jLabel3.setText("Seats Area:");
jPanel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
jLabel6.setText("Hall:");
jLabel7.setText("Date:");
jLabel8.setText("Time:");
jLabel9.setText("Total Ticket:");
jLabel10.setText("Seats Area:");
jLabel11.setText("Total Price:");
Hall.setEditable(false);
Date.setEditable(false);
Time.setEditable(false);
Seats.setEditable(false);
tTicket.setEditable(false);
tPrice.setEditable(false);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel9)
.addComponent(jLabel11))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(tPrice, javax.swing.GroupLayout.DEFAULT_SIZE, 152, Short.MAX_VALUE)
.addComponent(tTicket)))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel6)
.addComponent(jLabel7)
.addComponent(jLabel8)
.addComponent(jLabel10))
.addGap(24, 24, 24)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(Date, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(Time, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(Seats)
.addComponent(Hall))))
.addGap(6, 6, 6))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Hall, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel6))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(Date, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel7))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 13, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel8)
.addComponent(Time, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel10)
.addComponent(Seats, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(21, 21, 21)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel9)
.addComponent(tTicket, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel11)
.addComponent(tPrice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(14, 14, 14))
);
jComboBox2.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }));
jComboBox2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jComboBox2ActionPerformed(evt);
}
});
jComboBox3.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Right", "Center", "Left" }));
jComboBox3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jComboBox3ActionPerformed(evt);
}
});
jButton1.setText("Book Now!");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(24, 24, 24)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2)
.addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jComboBox2, javax.swing.GroupLayout.Alignment.LEADING, 0, 100, Short.MAX_VALUE)
.addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(5, 5, 5))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jButton1)
.addGap(111, 111, 111))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jLabel12)
.addGap(139, 139, 139)))))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 12, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(15, 15, 15))
.addGroup(layout.createSequentialGroup()
.addGap(89, 89, 89)
.addComponent(jLabel4)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
.addGroup(layout.createSequentialGroup()
.addGap(200, 200, 200)
.addComponent(jLabel1)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel2)
.addGap(7, 7, 7)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addGap(11, 11, 11)
.addComponent(jLabel5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(34, 34, 34)
.addComponent(jLabel3)
.addGap(18, 18, 18)
.addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(13, 13, 13)
.addComponent(jButton1)
.addGap(18, 18, 18)
.addComponent(jLabel12)))
.addContainerGap(58, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (jComboBox1.getSelectedIndex() == 0) {
Hall.setText("1");
Date.setText("25/6/2022");
Time.setText("9:00 AM");
}
if (jComboBox1.getSelectedIndex() == 1) {
Hall.setText("2");
Date.setText("30/6/2022");
Time.setText("8:00 PM");
}
if (jComboBox1.getSelectedIndex() == 2) {
Hall.setText("3");
Date.setText("26/6/2022");
Time.setText("10:00 PM");
}
if (jComboBox1.getSelectedIndex() == 3) {
Hall.setText("4");
Date.setText("27/6/2022");
Time.setText("5:00 PM");
}
}
private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {
if (jComboBox2.getSelectedIndex() == 0) {
tTicket.setText("1");
tPrice.setText("8 Ringgits");
}
if (jComboBox2.getSelectedIndex() == 1) {
tTicket.setText("2");
tPrice.setText("16 Ringgits");
}
if (jComboBox2.getSelectedIndex() == 2) {
tTicket.setText("3");
tPrice.setText("24 Ringgits");
}
if (jComboBox2.getSelectedIndex() == 3) {
tTicket.setText("4");
tPrice.setText("32 Ringgits");
}
if (jComboBox2.getSelectedIndex() == 4) {
tTicket.setText("5");
tPrice.setText("40 Ringgits");
}
if (jComboBox2.getSelectedIndex() == 5) {
tTicket.setText("6");
tPrice.setText("48 Ringgits");
}
if (jComboBox2.getSelectedIndex() == 6) {
tTicket.setText("7");
tPrice.setText("56 Ringgits");
}
if (jComboBox2.getSelectedIndex() == 7) {
tTicket.setText("8");
tPrice.setText("64 Ringgits");
}
if (jComboBox2.getSelectedIndex() == 8) {
tTicket.setText("9");
tPrice.setText("72 Ringgits");
}
if (jComboBox2.getSelectedIndex() == 9) {
tTicket.setText("10");
tPrice.setText("80 Ringgits");
}
}
private void jComboBox3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (jComboBox3.getSelectedIndex() == 0) {
Seats.setText("Right");
}
if (jComboBox3.getSelectedIndex() == 1) {
Seats.setText("Center");
}
if (jComboBox3.getSelectedIndex() == 2) {
Seats.setText("Left");
}
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try {
FileWriter fw2 = new FileWriter("bookinglist.txt", true);
fw2.write(Hall.getText()+ "\t" + "/ " + Date.getText()+ "\t" + "/ " + Time.getText() + "\t" + "/ " + Seats.getText() + "\t" + "/ " + tTicket.getText() + "\t" + "/ " + tPrice.getText()+ "\n");
fw2.close();
}catch(Exception ex) {}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Booking.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Booking.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Booking.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Booking.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Booking().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JTextField Date;
private javax.swing.JTextField Hall;
private javax.swing.JTextField Seats;
private javax.swing.JTextField Time;
private javax.swing.JButton jButton1;
private javax.swing.JComboBox<String> jComboBox1;
private javax.swing.JComboBox<String> jComboBox2;
private javax.swing.JComboBox<String> jComboBox3;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel10;
private javax.swing.JLabel jLabel11;
private javax.swing.JLabel jLabel12;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
private javax.swing.JPanel jPanel1;
private javax.swing.JTextField tPrice;
private javax.swing.JTextField tTicket;
// End of variables declaration
}