GUI项目帮助

时间:2015-05-06 03:07:40

标签: java user-interface

我正在制作一个我将要展示的项目。到目前为止,我一直表现不错,但我遇到了一些我无法弄清楚的问题。我正在构建一个基本的GUI。有4个部分可供选择。最后一个菜单选项不起作用。下拉菜单有效,但我无法选择一个选项。有一个黑色小箭头表示子菜单? 我无法找到原因。

GUI也只能一次保存一个选择。我需要它来保存所有选择并在最后添加所有内容。实现这一目标的最佳方法是什么?如果有人对如何改进我的项目有任何意见,欢迎所有建议!谢谢大家的时间和精力。非常感谢。

package guiproject;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class Guiproject implements ActionListener{

JLabel jlabel;

Guiproject(){
JFrame frame = new JFrame("Vehicle Choices");//create new JFrame     container            
frame.setLayout(new FlowLayout()); //specify flowlayout for the layout manager
frame.setSize(570,400); // gives the frame its size
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Terminates the program when user closes the application

jlabel = new JLabel();//creates a menu to display menu selections
JMenuBar menu = new JMenuBar();//creates menu bar

JMenu car = new JMenu("Choose Car");//creates field menu items.

JMenuItem sx240 = new JMenuItem("Nissan 240sx = $8,000$");//creates field menu items.
JMenuItem bmw = new JMenuItem(" BMW M5 = $15,000 ");//creates field menu items.
JMenuItem V8 = new JMenuItem(" Corvette = $22,000 ");//creates field menu items.
JMenuItem sky = new JMenuItem(" Skyline = $28,000 ");//creates field menu items.
JMenuItem ford = new JMenuItem(" Shelby Mustang = $25,500 ");//creates field menu items.

car.add(sx240); //add menu items to menu
car.add(bmw);//add menu items to menu
car.add(V8);//add menu items to menu
car.add(sky);//add menu items to menu
car.add(ford);//add menu items to menu
menu.add(car);

JMenu color = new JMenu("Choose Paint Color"); //creates the color selection option

JMenuItem red = new JMenuItem(" Cherry Red = $800.00 "); //craeted color choices
JMenuItem matte = new JMenuItem(" Matte Black = $1,700");//craeted color choices
JMenuItem pink = new JMenuItem(" Hot Pink = $975.00 ");//craeted color choices
JMenuItem purp = new JMenuItem (" Purple = $825.00 ");//craeted color choices
JMenuItem green = new JMenuItem(" Forest Green = $600.00");//craeted  color choices

color.add(red); //adds choices to the menu
color.add(matte);//adds choices to the menu
color.add(pink);//adds choices to the menu
color.add(purp);//adds choices to the menu
color.add(green);//adds choices to the menu
menu.add(color);

JMenu drop = new JMenu("Choose Suspension type"); //creates option for  suspension

JMenuItem stock = new JMenuItem(" Keep it Stock = $0.0 ");//creates menu choice
JMenuItem spring = new JMenuItem(" Basic Springs and Shocks = $ 150.00   ");//creates menu choice
JMenuItem coil = new JMenuItem(" Coilovers = $1,600 ");//creates menu choice
JMenuItem air = new JMenuItem(" Air Suspension = $ 3,000 ");//creates menu choice
JMenuItem low = new JMenuItem(" Lowering Springs = $575.00 ");//creates menu choice

drop.add(stock);//adds choice to the menu
drop.add(spring);//adds choice to the menu
drop.add(coil);//adds choice to the menu
drop.add(air);//adds choice to the menu
drop.add(low);//adds choice to the menu
menu.add(drop);//adds choice to the menu

JMenu engine = new JMenu("Choose performance parts"); //creates option for menu

JMenuItem stock1 = new JMenu(" Keep It Stock = $0.0 "); //creates menu choice
JMenuItem cam = new JMenu(" Upgrade Camshafts $475.00 ");//creates menu choice
JMenuItem turbo = new JMenu(" Turbo = $1,250.00 ");//creates menu choice
JMenuItem sup = new JMenu(" Supercharger = $2,800.00 ");//creates menu choice
JMenuItem twin = new JMenu(" Twin Turbo = $2,200.00 ");//creates menu choice

engine.add(stock1);//adds choice to the menu
engine.add(cam);//adds choice to the menu
engine.add(turbo);//adds choice to the menu
engine.add(sup);//adds choice to the menu
engine.add(twin);//adds choice to the menu
menu.add(engine);//adds choice to the menu

sx240.addActionListener(this); //adds action listener to menu items
bmw.addActionListener(this);//adds action listener to menu items
V8.addActionListener(this);//adds action listener to menu items
sky.addActionListener(this);//adds action listener to menu items
ford.addActionListener(this);//adds action listener to menu items

red.addActionListener(this);//adds action listener to menu items
matte.addActionListener(this);//adds action listener to menu items
pink.addActionListener(this);//adds action listener to menu items
purp.addActionListener(this);//adds action listener to menu items
green.addActionListener(this);//adds action listener to menu items

stock.addActionListener(this);//adds action listener to menu items
spring.addActionListener(this);//adds action listener to menu items
coil.addActionListener(this);//adds action listener to menu items
air.addActionListener(this);//adds action listener to menu items
low.addActionListener(this);//adds action listener to menu items

stock1.addActionListener(this);//adds action listener to menu items
cam.addActionListener(this);//adds action listener to menu items
turbo.addActionListener(this);//adds action listener to menu items
sup.addActionListener(this);//adds action listener to menu items
twin.addActionListener(this);//adds action listener to menu items

frame.add(jlabel); //adds label to content pane
frame.setJMenuBar(menu);//adds menu bar to frame
frame.setVisible(true);//displays frame
}
public void actionPerformed(ActionEvent ae){// handles menu item action events
String string = ae.getActionCommand();// gets action command to menu section
String string1 = ae.getActionCommand();
if(string.equals("Exit"))System.exit(0);//Exits the program when user chooses          exit.
jlabel.setText(string+ " Selected ");//displays selected choice
jlabel.setText(string1+ " selected ");
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new Guiproject();     
}   

});    
}    
}

1 个答案:

答案 0 :(得分:1)

JMenuItem stock1 = new JMenu(" Keep It Stock = $0.0 "); //creates menu choice
JMenuItem cam = new JMenu(" Upgrade Camshafts $475.00 ");//creates menu choice
JMenuItem turbo = new JMenu(" Turbo = $1,250.00 ");//creates menu choice
JMenuItem sup = new JMenu(" Supercharger = $2,800.00 ");//creates menu choice
JMenuItem twin = new JMenu(" Twin Turbo = $2,200.00 ");//creates menu choice

您可以将对象定义为JMenuItem,但要创建JMenu。由于JMenu延伸JMenuItem,这是合法的,但不是您想要的。

你想:

JMenuItem stock1 = new JMenuItem(" Keep It Stock = $0.0 "); //creates menu choice
JMenuItem cam = new JMenuItem(" Upgrade Camshafts $475.00 ");//creates menu choice
JMenuItem turbo = new JMenuItem(" Turbo = $1,250.00 ");//creates menu choice
JMenuItem sup = new JMenuItem(" Supercharger = $2,800.00 ");//creates menu choice
JMenuItem twin = new JMenuItem(" Twin Turbo = $2,200.00 ");//creates menu choice