我有两个窗户。第一个是登录窗口,第二个是主菜单窗口。我还有一个名为Car的课程。我尝试做的是将Login窗口中的数组值传递给主窗口。因此,当用户点击BuyCar按钮时,价格会显示。我对Java Swing很新,所以任何帮助都会受到赞赏。
我得到一个"主窗口()中的构造函数未定义"在主窗口的这段代码中,但当我尝试修复它时新的MainWindow(c); ,我收到了错误。
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
}
我做错了什么?谢谢!
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;
}
}
登录窗口
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();
}
}
主菜单窗口
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();
}
});
}
}
答案 0 :(得分:0)
您的MainWindow构造函数需要将Car对象作为参数传递给它。
new MainWindow(c);
但是,除非您还定义变量c。
,否则这还不够Car c = new Car();
new MainWindow(c);