【问题标题】:Cant Get Gui To Appear When I run Program [duplicate]运行程序时无法显示 Gui [重复]
【发布时间】:2015-11-04 04:29:09
【问题描述】:

需要一些帮助,我正在处理一个项目,我才开始使用 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;
    }
}

【问题讨论】:

  • 你有main方法吗?
  • 在公共字符串 args 中?我不是一个伟大的程序员,所以如果看起来很愚蠢,请原谅我的回答
  • 是的。这就是运行时自动运行的代码。
  • 我在最底部的菜单上有一个,但里面什么都没有

标签: java arrays eclipse user-interface jframe


【解决方案1】:

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

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

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

但是,正如 Issac 上面所说,我强烈建议您查看有关创建 GUI 的教程,因为在您的代码中除了没有创建 GUI 之外还有更多错误。

【讨论】:

  • 干杯我试试,我在这方面不太擅长,我还在上大学,我的智慧现在结束了这个程序,谢谢你的帮助:)
  • 只是觉得你成功了,非常感谢你没有意识到你帮助我在这里停留了几天哈哈:D
  • @aero123 如果有帮助,也许可以接受你的答案:)
【解决方案2】:

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

【讨论】:

  • 基本上所有我制作的只是一个简单的窗口,它添加了一辆车然后删除一辆车更新一辆车并退出顶部有一张小图片它只是一个基本的我还在学习这个代码
猜你喜欢
  • 1970-01-01
  • 2020-11-22
  • 2015-01-15
  • 1970-01-01
  • 1970-01-01
  • 2020-11-13
  • 2019-09-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多