当我运行程序

时间:2015-11-03 20:17:18

标签: java arrays eclipse user-interface jframe

在这里需要一些帮助我正在开发一个项目而且我只是开始使用eclipse而且我无法让gui在屏幕上弹出我运行程序时我只是想看看它到目前为止看起来有什么东西有答案生病显示下面的代码所有帮助赞赏它分为3个类也有一些方法没有完成但所有调用和创建所以没有错误它说 ////////////////////////////////////////////////// ///////////////////

public class Car {
String carReg ="";  
String make;
String model;
String price;

public Car(String carReg, String make, String model, String price) 
{
    this.carReg=carReg;
    this.make=make;
    this.model=model;
    this.price=price;
}
/*
 getters and setters
 */
public String getcarReg() {
    return carReg;
}
public void setcarReg(String reg) {
    carReg=reg;
}
public String getprice() {
    return price;
}
public void setprice(String pri) {
    price=pri;
}
public String getmodel() {
    return model;
}
public void setmodel(String mod) {
    model=mod;
}

public String getMake() {
    return make;
}
public void setMake(String mk) {
    make=mk;
}

}
/////////////////////////////////////

import javax.swing.*;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Menu {

    GarageManager gm = new GarageManager();
    private static Icon myIcon = new ImageIcon("..\\CarGarage\\src\\ie.wit.CarGarage\\cargarage.jpg");

    public Menu() {

        String option = "";

        do {
            JLabel addl = new JLabel("1.Add A Car To Stock");
             JLabel removel = new JLabel("2.Remove a Car From Stock");
             JLabel viewl = new JLabel("3.View All Car's In Stock");
             JLabel updatel = new JLabel("4.Update A Car In Stock");
             JLabel exitl = new JLabel("5.Exit System");
             JTextField userChoiceTextField = new JTextField();
             Object [] MainMenu = new Object[7];
                     MainMenu[0]= myIcon;
                     MainMenu[1]= addl; 
                     MainMenu[2]= removel;
                     MainMenu[3]= viewl;
                     MainMenu[4]= updatel;
                     MainMenu[5]= exitl;
                     MainMenu[6]= userChoiceTextField;

                     JOptionPane.showMessageDialog(null, MainMenu);
                     option = userChoiceTextField.getText();




                     switch(option)
                        {
                        case "1":
                            break;
                        case "2":
                            break;
                        case "3":
                            break;
                        case "4":
                            break;
                        case "5":
                            break;
                            default: break;
                        }

if(option =="1");
{
    JLabel carRegl = new JLabel("Please Enter Car Registration");
    JLabel makel = new JLabel("Please Enter Car Make");
    JLabel modell = new JLabel("Please Enter Car Model");
    JLabel pricel = new JLabel("Please Enter Car Price");
    /////////////////////////////////////////////////
    JTextField JcarReg= new JTextField();
    JTextField Jmake= new JTextField();
    JTextField Jmodel= new JTextField();
    JTextField Jprice= new JTextField();
    ////////////////////////////////////////////////
    Object [] addCarA = new Object[9];
    ////////////////////////////////////////////////
    addCarA[0] = myIcon;
    addCarA[1] = carRegl;
    addCarA[2] = JcarReg;
    addCarA[3] = makel;
    addCarA[4] = Jmake;
    addCarA[5] = modell;
    addCarA[6] = Jmodel;
    addCarA[7] = pricel;
    addCarA[8] = Jprice;

    JOptionPane.showMessageDialog(null,  addCarA);
    String carReg1 = JcarReg.getText();
    String make1 = Jmake.getText();
    String model1 = Jmodel.getText();
    String price1 = Jprice.getText();

    String allCars="Car Registration: "+carReg1+ "\n Make:"+make1+ "\n Model:"+model1+ "\n Price:"+price1;
    Car carNew = new Car(carReg1, make1, model1, price1);

    boolean carTest = gm.addCar(carNew);
    if ( carTest==true)
    {
        JOptionPane.showMessageDialog(null,  allCars);
    }
    else 
    {
        JOptionPane.showMessageDialog(null, "Car Wasnt Added to System");
    }
}
if(option =="2");
{
    String reg1 = JOptionPane.showInputDialog(null, "Enter Car Registration to Remove:");
    boolean test = gm.removes(reg1);
    if(test == true)
    {
        JOptionPane.showMessageDialog(null, "Car Has Been Removed");
    }
    else if(test == false) 
    {

    }
}
if(option =="3");
{

    String printAllCars = gm.view();
    JOptionPane.showMessageDialog(null, printAllCars);
}
if(option =="4");
{
    String reg1 = JOptionPane.showInputDialog(null, "Enter Car Registration");
    boolean carUpdate = gm.update();
    if (carUpdate == true)
    {
        JOptionPane.showMessageDialog(null, "Car Has Been Updated");
    }
    else if(carUpdate == false)
    {
        JOptionPane.showMessageDialog(null,  "Car With Registation" + reg1 + "Does Not Exist In The System");

    }
}
if(option =="5");
{
    System.exit(0);

}
        }while(option !="5");


    }
    public static void main(String [] args)
    {

    }







}
///////////////////////////////////////////
import java.util.*;

import javax.swing.Icon;
import javax.swing.ImageIcon;

public class GarageManager {

    private static Icon myIcon = new ImageIcon("..\\CarGarage\\src\\ie.wit.CarGarage\\cargarage.jpg");



    List<Car> carList = new ArrayList<Car>();

    public boolean addCar(Car newCar) 
    {
        boolean carTest = false;
        carList.add(newCar);
        carTest = true;
        return carTest;

    }

    public boolean removes(String reg)
    {
        boolean carRemove = false;
        return carRemove;
    }

    public String view()
    {
        String carDetails = "";
        return carDetails;
    }
    public boolean update()
    {
        boolean car1 = true;
        return car1;
    }

    public boolean shutdown()
    {
        boolean car = true;
        return car;
    }
}

2 个答案:

答案 0 :(得分:2)

您的问题是您从未实际实例化过GUI。你有所有代码,但它永远不会执行,因为你的main方法中没有任何东西。

在JAVA中,主要方法是最重要的方法,因为它是所有后续代码的参考起点。基于main方法中的代码(以public static void main(String [] args开头的那个))需要添加

public static void main(String[] args){
    new Menu();
}

然而,正如Issac上面所述,我强烈建议你看看有关创建GUI的教程,因为代码中有很多错误,而不是创建GUI。

答案 1 :(得分:0)

首先,主要方法是运行程序时运行的代码。如果主方法中没有任何内容,则不会发生任何事情。其次,你没有任何东西可以创建GUI。我建议你看看JPanel。如果您正在尝试制作GUI,您应该在Youtube上做一些教程。我真的不知道你的程序的要求或者你想要做什么,所以如果没有更多信息我就无法提供更多的帮助。