I have two windows. The first is a Login window and the second is the Main Menu window. I also have a class called Car. What I'm trying to do is to pass a value from an array in the Login window to the Main Window. So that when the user clicks the BuyCar button the price would show. I'm fairly new to Java Swing, so any help would be appreciated.
I'm getting a "The constructor in Main Window() is undefined" in this bit of code in the Main Window, but when I try to fix it new MainWindow(c);, I'm getting an error.
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread: creating and showing this application's GUI.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new MainWindow(); //problem here
}
What am I doing wrong? Thanks!
CAR
public class Car {
String make;
String model;
float price;
String color;
int year;
int payterms;
int purchasetype; //1 – Buy, 2-Finance, 3-Lease
int months; //months
float payamount; // monthly payment amount
Car ()
{
make = "";
model = "";
price = 0.00f;
color = "";
year = 0;
purchasetype = 0; //1 – Buy, 2-Finance, 3-Lease
months= 0; //months
payamount = 0f; // monthly payment amount
}
Car (String m, String m2, float p, String c, int y, int pt, int m3, float pa)
{ make = m;
model = m2;
price = p;
color = c;
year = y;
purchasetype = pt;
payterms = m3;
payamount = pa;
}
float buyCar(int pt, int terms)
{
purchasetype = pt;
months = terms;
if (pt == 2 || pt == 3)
{ payamount = price/months; }
return payamount;
}
}
LOGIN WINDOW
package pkgLogin;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LoginWindow extends JFrame {
Car c;
LoginWindow() {
c = new Car("Cadillac", "ATS", 120000.00f, "red", 2012, 0, 0, 0.00f);
//Create a new frame container
setTitle("Login Form");
//Give the frame an initial size and center the window
setSize(400, 220);
setLocationRelativeTo(null);
//Terminate the program when the user closes the application
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Make two buttons
JButton okButton = new JButton ("OK");
okButton.setFont(new Font("Helvetica", Font.BOLD, 12));
okButton.setOpaque(true);
okButton.setPreferredSize(new Dimension(85, 22));
okButton.setBackground(new Color(255, 250, 240));
okButton.setForeground(new Color(255, 160, 122));
JButton cancelButton = new JButton ("Cancel");
cancelButton.setFont(new Font("Helvetica", Font.BOLD, 12));
cancelButton.setOpaque(true);
cancelButton.setPreferredSize(new Dimension(85, 22));
cancelButton.setBackground(new Color(255, 250, 240));
cancelButton.setForeground(new Color(255, 160, 122));
//Create text based label
JLabel userLabel = new JLabel("Username:");
userLabel.setFont(new Font("Helvetica", Font.BOLD, 12));
userLabel.setForeground(new Color(255, 250, 240));
JLabel passwordLabel = new JLabel("Password:");
passwordLabel.setFont(new Font("Helvetica", Font.BOLD, 12));
passwordLabel.setForeground(new Color(255, 250, 240));
JLabel messageLabel = new JLabel("Click HERE if you've lost your card");
messageLabel.setFont(new Font("Helvetica", Font.BOLD, 11));
messageLabel.setForeground(new Color(255, 250, 240));
//Create field label
JTextField userField = new JTextField(10);
userField.setBackground(new Color(255, 255, 255));
JPasswordField passwordField = new JPasswordField(10);
passwordField.setEchoChar('*');
//Add listener to CANCEL button
cancelButton.addActionListener (new ActionListener () {
public void actionPerformed(ActionEvent e){
//Execute when button is pressed
JOptionPane.showMessageDialog(null, "Thank you. Goodbye!", "Inane warning",JOptionPane.WARNING_MESSAGE);
System.exit(0);
}});
//Add listener to OK button
okButton.addActionListener (new ActionListener () {
public void actionPerformed(ActionEvent e){
//Execute when button is pressed
userField.requestFocusInWindow();
String typeduser = userField.getText();
String enteredpass = new String (passwordField.getPassword());
//Check password length
if (enteredpass.length() != 8 )
{
JOptionPane.showMessageDialog(null, "Your password is either too long, or too short! "
+ "\nPassword should be 8 characters long!", "Inane warning",JOptionPane.WARNING_MESSAGE);
System.exit(0);
}
//Check to see that user has filled in all information
if (enteredpass.equals("") || typeduser.equals(""))
{ JOptionPane.showMessageDialog(null, "Information is missing! Try Again!",
"Inane warning",JOptionPane.WARNING_MESSAGE);
System.exit(0);
}
//Validate
if (typeduser.equals("jenaya") && enteredpass.equals("hellojen"))
{ JOptionPane.showMessageDialog(null, "Welcome!",
"Welcome",JOptionPane.INFORMATION_MESSAGE);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
setVisible(false);
new MainWindow(c);
}
});
}
else
{
JOptionPane.showMessageDialog(null, "Incorrect Login!", "Inane warning",JOptionPane.WARNING_MESSAGE);
}
}});
//Add the panel to the content page
JPanel panel = new JPanel();
add(panel);
panel.setBackground(new Color(255, 160, 122));
//Display the frame
setVisible(true);
//Set the layout
SpringLayout layout = new SpringLayout();
panel.setLayout(layout);
//Add the label to the content pane
panel.add(userLabel);
panel.add(userField);
//Add the text field to the content pane
panel.add(passwordLabel);
panel.add(passwordField);
panel.add(messageLabel);
//Add buttons to the content
panel.add(okButton);
panel.add(cancelButton);
//Adjust constraints for the label.
layout.putConstraint(SpringLayout.WEST, userLabel, 85, SpringLayout.WEST, panel); //right
layout.putConstraint(SpringLayout.NORTH, userLabel, 25, SpringLayout.NORTH, panel); //top
layout.putConstraint(SpringLayout.WEST, passwordLabel, 85, SpringLayout.WEST, panel);
layout.putConstraint(SpringLayout.NORTH, passwordLabel, 55, SpringLayout.NORTH, panel);
//Adjust constraints for the text field so it's at (<label's right edge>).
layout.putConstraint(SpringLayout.WEST, userField, 10, SpringLayout.EAST, userLabel);
layout.putConstraint(SpringLayout.NORTH, userField, 25, SpringLayout.NORTH, panel);
layout.putConstraint(SpringLayout.WEST, passwordField, 10, SpringLayout.EAST, passwordLabel);
layout.putConstraint(SpringLayout.NORTH, passwordField, 55, SpringLayout.NORTH, panel);
//Adjust constraints for the buttons.
layout.putConstraint(SpringLayout.WEST, okButton, 85, SpringLayout.WEST, panel);
layout.putConstraint(SpringLayout.NORTH, okButton, 90, SpringLayout.NORTH, panel);
layout.putConstraint(SpringLayout.WEST, cancelButton, 195, SpringLayout.WEST, panel);
layout.putConstraint(SpringLayout.NORTH, cancelButton, 90, SpringLayout.NORTH, panel);
//Adjust constraints for the labels.
layout.putConstraint(SpringLayout.WEST, messageLabel, 91, SpringLayout.WEST, panel);
layout.putConstraint(SpringLayout.NORTH, messageLabel, 145, SpringLayout.NORTH, panel);
}
public static void main(String[] args) {
new LoginWindow();
}
}
MAIN MENU WINDOW
public class MainWindow extends JFrame{
MainWindow(Car c) {
//Create a new frame container
setTitle("Main Window Form");
//Give the frame an initial size and center the window
setSize(400, 400);
setLocationRelativeTo(null);
//Terminate the program when the user closes the application
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImageIcon image = new ImageIcon("G:/JSProject/images.jpg");
JLabel imagelabel = new JLabel(image);
imagelabel.setPreferredSize(new Dimension(264, 191));
JLabel messageLabel = new JLabel("This car is worth $200,000.00");
messageLabel.setFont(new Font("Helvetica", Font.BOLD, 11));
messageLabel.setForeground(new Color(255, 250, 240));
JButton okButton = new JButton ("OK");
okButton.setFont(new Font("Helvetica", Font.BOLD, 12));
okButton.setOpaque(true);
okButton.setPreferredSize(new Dimension(85, 22));
okButton.setBackground(new Color(255, 250, 240));
okButton.setForeground(new Color(255, 160, 122));
JButton cancelButton = new JButton ("Cancel");
cancelButton.setFont(new Font("Helvetica", Font.BOLD, 12));
cancelButton.setOpaque(true);
cancelButton.setPreferredSize(new Dimension(85, 22));
cancelButton.setBackground(new Color(255, 250, 240));
cancelButton.setForeground(new Color(255, 160, 122));
JRadioButton buyButton = new JRadioButton("Buy");
buyButton.setMnemonic(KeyEvent.VK_B);
buyButton.setActionCommand("Buy");
buyButton.setSelected(true);
JRadioButton leaseButton = new JRadioButton("Lease");
leaseButton.setMnemonic(KeyEvent.VK_B);
leaseButton.setActionCommand("Lease");
JRadioButton financeButton = new JRadioButton("Finance");
financeButton.setMnemonic(KeyEvent.VK_B);
financeButton.setActionCommand("Finance");
ButtonGroup bG = new ButtonGroup();
bG.add(buyButton);
bG.add(leaseButton);
bG.add(financeButton);
buyButton.setSize(100,200);
leaseButton.setSize(100,200);
financeButton.setSize(100,200);
//Add listener
cancelButton.addActionListener (new ActionListener () {
public void actionPerformed(ActionEvent e){
//Execute when button is pressed
JOptionPane.showMessageDialog(null, "Thank you. Goodbye!", "Program Exit",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}});
//Add listener to OK button
okButton.addActionListener (new ActionListener () {
public void actionPerformed(ActionEvent e){
//Execute when button is selected
if (buyButton.isSelected())
{
//setVisible(false);
JOptionPane.showMessageDialog(null, "The price of the car is "+c.price, "Program Exit",JOptionPane.INFORMATION_MESSAGE);
//new BuyCarWindow(c).setVisible(true);
}
else if (leaseButton.isSelected())
{
setVisible(false);
//LeaseWindow.setVisible(true);
}
else if (financeButton.isSelected())
{
setVisible(false);
//FinanceWindow.setVisible(true);
}
}});
//Create text based label
JLabel userLabel = new JLabel("Main Window");
userLabel.setFont(new Font("Helvetica", Font.BOLD, 12));
userLabel.setForeground(new Color(255, 250, 240));
//Add the panel to the content page
JPanel panel = new JPanel();
add(panel);
panel.setBackground(new Color(255, 160, 122));
//Display the frame
setVisible(true);
//Set the layout
SpringLayout layout = new SpringLayout();
panel.setLayout(layout);
//Add the label to the content pane
panel.add(userLabel);
//Add buttons to the content
panel.add(imagelabel);
panel.add(messageLabel);
panel.add(okButton);
panel.add(cancelButton);
panel.add(buyButton);
panel.add(leaseButton);
panel.add(financeButton);
//this.pack();
//Adjust constraints for the label.
layout.putConstraint(SpringLayout.WEST, userLabel, 85, SpringLayout.WEST, panel); //right
layout.putConstraint(SpringLayout.NORTH, userLabel, 25, SpringLayout.NORTH, panel); //top
layout.putConstraint(SpringLayout.WEST, okButton, 85, SpringLayout.WEST, panel);
layout.putConstraint(SpringLayout.NORTH, okButton, 90, SpringLayout.NORTH, panel);
layout.putConstraint(SpringLayout.WEST, cancelButton, 180, SpringLayout.WEST, panel);
layout.putConstraint(SpringLayout.NORTH, cancelButton, 90, SpringLayout.NORTH, panel);
layout.putConstraint(SpringLayout.WEST, buyButton, 100, SpringLayout.WEST, panel);
layout.putConstraint(SpringLayout.NORTH, buyButton, 50, SpringLayout.NORTH, panel);
layout.putConstraint(SpringLayout.WEST, leaseButton, 150, SpringLayout.WEST, panel);
layout.putConstraint(SpringLayout.NORTH, leaseButton, 50, SpringLayout.NORTH, panel);
layout.putConstraint(SpringLayout.WEST, financeButton, 220, SpringLayout.WEST, panel);
layout.putConstraint(SpringLayout.NORTH, financeButton, 50, SpringLayout.NORTH, panel);
//Adjust constraints for the labels.
layout.putConstraint(SpringLayout.WEST, messageLabel, 91, SpringLayout.WEST, panel);
layout.putConstraint(SpringLayout.NORTH, messageLabel, 145, SpringLayout.NORTH, panel);
layout.putConstraint(SpringLayout.WEST, imagelabel, 91, SpringLayout.WEST, panel);
layout.putConstraint(SpringLayout.NORTH, imagelabel, 145, SpringLayout.NORTH, panel);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread: creating and showing this application's GUI.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new MainWindow();
}
});
}
}