- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在为其中包含车辆对象的陈列室创建 GUI 时遇到问题。我正在使用 JFrame
并获得了基本轮廓,其中包含下一个、上一个按钮以及边缘的销售按钮(使用 Compass 布局)。我已向其中每个添加了 actionListeners
,但我不知道如何调用 Vehicle
或 Showroom
类中的相应方法。我应该在 JFrame
类中有一个 main 方法吗?
我还需要在中央面板中显示当前车辆的详细信息,但不明白如何做到这一点?
下面是一些代码:
public class VehicleJFrame extends JFrame implements ActionListener
{
private JButton previousButton;
private JButton sellButton;
private JButton nextButton;
private JPanel centerPanel;
public VehicleJFrame()
{
super("Vehicle GUI");
setSize(800, 400);
setLocation(400, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
createGUI();
setVisible(true);
}
public void createGUI()
{
previousButton = new JButton();
sellButton = new JButton();
nextButton = new JButton();
centerPanel = new JPanel();
//Add code for center panel to display the current vehicle
previousButton = new JButton("Previous");
previousButton.addActionListener(this);
sellButton = new JButton("Sell");
sellButton.addActionListener(this);
nextButton = new JButton("Next");
nextButton.addActionListener(this);
getContentPane().setLayout(new BorderLayout(10, 10));
getContentPane().setBackground(Color.RED);
getContentPane().add(previousButton, BorderLayout.WEST);
getContentPane().add(sellButton, BorderLayout.SOUTH);
getContentPane().add(nextButton, BorderLayout.EAST);
}
@Override
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == previousButton)
{
//What goes here?
}
else if (e.getSource() == sellButton)
{
}
else if (e.getSource() == nextButton)
{
}
}
}
public class Vehicle
{
//**********Declarations**********
private String manufacturer;
private String model;
private String customerName = null;
private String VIN;
private String dateOfManufacture;
private String dateOfSale = null;
private Boolean sold = false;
private char taxBand;
private int costOfVehicle;
//**********Constructor**********
public Vehicle(String man, String mod, String VIN, String dateOfMan, char taxBand, int costOfVehicle)
{
this.manufacturer = man;
this.model = mod;
this.VIN = VIN;
this.dateOfManufacture = dateOfMan;
this.taxBand = taxBand;
this.costOfVehicle = costOfVehicle;
}
//**********toString() method for Vehicle information**********
@Override
public String toString()
{
return "Vehicle\n{\n" + " manufacturer = " + manufacturer
+ "\n model = " + model
+ "\n customerName = " + customerName
+ "\n VIN = " + VIN
+ "\n dateOfManufacture = " + dateOfManufacture
+ "\n dateOfSale = " + dateOfSale
+ "\n sold = " + sold
+ "\n taxBand = " + taxBand
+ "\n CO2 Emmissions = " + getCO2Group()
+ "\n costOfVehicle = " + costOfVehicle + "\n" + '}';
}
public String buyVehicle(String customerName, String dateOfSale)
{
this.customerName = customerName;
this.dateOfSale = dateOfSale;
sold = true;
return sold.toString();
}
public class Showroom
{
private ArrayList<Vehicle> vehicleArrayList;
private String showroomName;
private int currentPos;
public Showroom(String name)
{
showroomName = name;
vehicleArrayList = new ArrayList();
currentPos = 0;
}
public boolean addVehicle(Vehicle newVehicle)
{
vehicleArrayList.add(currentPos, newVehicle);
return true;
}
public Vehicle getCurrentVehicle()
{
return vehicleArrayList.get(currentPos);
}
public boolean next()
{
if (vehicleArrayList.size() - 1 > currentPos)
{
currentPos++;
return true;
}
else
{
return false;
}
}
public boolean previous()
{
if (currentPos > 0)
{
currentPos--;
return true;
}
else
{
return false;
}
}
最佳答案
我使用了 JOptionPane 并进行了一些细微的更改来演示在框架中显示数据的方式。我添加了一个 JTextPane 来显示数据,以及一种通过您制作的 GUI 中的按钮添加汽车的方法。要从这里继续,我建议在添加更多汽车后,使用上一个和下一个按钮来迭代陈列室中的汽车数组列表。看看是否有帮助:
public class VehicleJFrame extends JFrame implements ActionListener
{
private JButton previousButton;
private JButton addButton; //NEW - For adding vehicles
private JButton sellButton;
private JButton nextButton;
private JPanel centerPanel;
private JTextPane displayPane;
private Showroom theShowRoom;
public VehicleJFrame()
{
super("Vehicle GUI");
setSize(800, 400);
setLocation(400, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
createGUI();
setVisible(true);
}
public void createGUI()
{
previousButton = new JButton();
sellButton = new JButton();
nextButton = new JButton();
centerPanel = new JPanel();
//Add code for center panel to display the current vehicle
previousButton = new JButton("Previous");
previousButton.addActionListener(this);
sellButton = new JButton("Sell");
addButton = new JButton("Add");
sellButton.addActionListener(this);
nextButton = new JButton("Next ");
nextButton.addActionListener(this);
addButton.addActionListener(this);
displayPane = new JTextPane();//NEW - For displaying
displayPane.setEditable(false);//NEW - Only for display so no editing
theShowRoom = new Showroom("Showroom 1");//NEW - To store our vehicles in
getContentPane().setLayout(new BorderLayout(10, 10));
getContentPane().add(displayPane, BorderLayout.CENTER);//NEW - The place to display cars in the middle
getContentPane().add(previousButton, BorderLayout.WEST);
getContentPane().add(sellButton, BorderLayout.SOUTH);
getContentPane().add(addButton, BorderLayout.NORTH);//NEW - The add button
getContentPane().add(nextButton, BorderLayout.EAST);
}
@Override
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == previousButton)
{
}
else if (e.getSource() == sellButton)
{
}
else if (e.getSource() == nextButton)
{
}
else if (e.getSource() == addButton){
Vehicle v = new Vehicle(JOptionPane.showInputDialog(null, "Manafacturer"),
JOptionPane.showInputDialog(null, "Model"),
JOptionPane.showInputDialog(null, "Vin"),
JOptionPane.showInputDialog(null, "Manafacture date"),
JOptionPane.showInputDialog(null, "Tax band").charAt(0),
Integer.valueOf(JOptionPane.showInputDialog(null, "cost")));
theShowRoom.addVehicle(v);//Add to showroom
displayPane.setText(theShowRoom.getCurrentVehicle().toString());
}
}
public static void main(String[] args){ //To test the program
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new VehicleJFrame();
}
});
}}
我添加了一个 main 方法,因此您应该能够与其他类一起运行它。祝你好运!
关于java - 为车辆陈列室实现 GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19952965/
我希望向订单输入系统添加一项功能,以实时显示订单位置与指定送货车之间的距离(以公里为单位)。 货车有一个 GPS 跟踪系统,来自 High Point GPS , 带有查询司机位置的API,以LAT/
我正在尝试为 C 中的以下内容动态分配空间,我想知道是否有人可以解释如何操作?字符* (*车辆)[][5]; 这 5 个元素是车辆的详细信息,第一个数组用于不同的车辆。所以我正在尝试为不同的车辆动态分
我有难得的机会在几个小时内与负责为美国交通部实现车辆 2 车辆通信的人员和其他 2 人会面。 您有什么问题要问他吗? 我知道这有点不正常,但这是一个“反向”线程,我觉得他对我想与这个社区分享的主题有一
我对 GraphHopper 路由引擎稳定版本 0.5 有疑问 使用vehicle=car时我可以获取路线,但使用自行车和步行则无法获取路线。 Here is an example在 GraphHop
有谁知道以 PHP、Perl 或 Python(或任何其他语言,我可以轻松转换代码)作为开源/免费软件提供的 ISO 3779 车辆 VIN 解码器库? 即使只是解码 WMI 部分(前 3 个位置)也
我正在尝试使用 L5 分页,一切看起来都很好,直到我点击第二页链接。当我点击它时出现此错误: Laravel 5 Pagination undefined variable vehicles in v
我是一名优秀的程序员,十分优秀!