我遇到問題,而且是堆棧。如果任何人有空餘的時間,這將是偉大的。我試圖傳遞其他類中的getters的值,但它沒有傳遞變量的值。例如,在我的類的方法toString的椅子我希望打印我在Class FurnitureItem中分配的值。 Chair是FurnitureItem的子類。我使用setter方法設置值,當我嘗試檢索它們時,它將打印舊值。 所有這些都在按下addChair按鈕時發生。主要方法的類是RunFurniture,面板和程序的GUI在PanelFurniture中。當程序運行並且按下addChair按鈕時,我們輸入和idNum,它是char型的int類型,或者是數量型的int型,我們選擇armrest或不布爾型。該程序正在編譯和運行,並沒有做我期待它做的事情。我正在使用eclipse。這是代碼。從其他方法傳遞值
/** This is the driver class of the program.
* Here is the main method with the JFrame.
* class name RunFurniture.class
* @author Kiril Anastasov
* @date 18/03/2012
*/
import java.awt.*;
import javax.swing.*;
public class RunFurniture
{
/**
* @param args
*/
public static void main(String[] args)
{
JFrame application = new JFrame();
PanelFurniture panel = new PanelFurniture();
application.add(panel);
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.setSize(650,650);
application.setLocationByPlatform(true);
application.setVisible(true);
}
}
這裏是程序的GUI。
/** Here is the GUI of the program.
* class name PanelFurniture.class
* @author Kiril Anastasov
* @date 18/03/2012
*/
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class PanelFurniture extends JPanel implements ActionListener
{
//FurnitureItem my = new FurnitureItem(""); the class is abstract so you cannot make an object from it.
JButton center, east;
JButton[] commandButtons = {
new JButton(" Add Chair"),
new JButton(" Add Table"),
new JButton(" Add Desk "),
new JButton(" Clear All "),
new JButton("Total Price "),
new JButton(" Save "),
new JButton(" Load "),
new JButton("Summary ")
};
JLabel desks;
JLabel[] chairsAndTables = {
new JLabel("Possition 1"),
new JLabel("Possition 2"),
new JLabel("Possition 3"),
new JLabel("Possition 4"),
new JLabel("Possition 5"),
new JLabel("Possition 6")};
protected ImageIcon[] image = { new ImageIcon("Pictures/Chair1.png"),
new ImageIcon("Pictures/Chair2.png"),
new ImageIcon("Pictures/Desk1.png"),
new ImageIcon("Pictures/Desk2.png"),
new ImageIcon("Pictures/Desk3.png"),
new ImageIcon("Pictures/Desk4.png"),
new ImageIcon("Pictures/Table1.png"),
new ImageIcon("Pictures/Table2.png")};
JPanel centerPanel, westPanel, eastPanel, leftPanel, rightPanel;
PanelFurniture()
{
this.setLayout(new BorderLayout());
westPanel = new JPanel();
westPanel.setLayout(new BoxLayout(westPanel, BoxLayout.PAGE_AXIS));
for(int i=0; i<commandButtons.length; i++)
{
westPanel.add(commandButtons[i]);
commandButtons[i].addActionListener(this);
}
this.add(westPanel, BorderLayout.WEST);
// split the panel by 2
centerPanel = new JPanel(new GridLayout(1,2));
// split the left half for the chairs and the tables
leftPanel = new JPanel(new GridLayout(3,2));
for(int j=0; j<chairsAndTables.length; j++)
{
leftPanel.add(chairsAndTables[j]);
}
centerPanel.add(leftPanel);
//split the right part of the middle for the desks
rightPanel = new JPanel(new GridLayout(3,1));
desks = new JLabel(image[2]);
rightPanel.add(desks);
desks = new JLabel("2");
rightPanel.add(desks);
desks = new JLabel("3");
rightPanel.add(desks);
centerPanel.add(rightPanel);
this.add(centerPanel, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource() == commandButtons[0])
{
Chair myChair = new Chair(); // create an object of type Chair
myChair.setIdNum(0); // ask the user for the Id of the chair
System.out.println(myChair.getIdNum()); // print the id of the chair
myChair.setTypeOfWood('w');
System.out.println(myChair.getTypeOfWood());
myChair.setQuantity(0);
System.out.println(myChair.getQuantity());
myChair.setArmRest(false);
System.out.println(myChair.getArmRest());
System.out.println(myChair.toString());
if(myChair.getArmRest() == true)
{
chairsAndTables[0].setIcon(image[1]);
}
else
{
chairsAndTables[0].setIcon(image[0]);
}
}
if(ae.getSource() == commandButtons[1])
{
Table myTable = new Table();
myTable.setIdNum(0);
System.out.println(myTable.getIdNum());
myTable.setTypeOfWood('o');
System.out.println(myTable.getTypeOfWood());
myTable.setQuantity(2);
System.out.println(myTable.getQuantity());
System.out.println(myTable.toString());
System.out.println(myTable.getIdNum());
}
}
}
這是類FurnitureItem這是抽象的,超的椅子
import java.io.Serializable;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import java.util.Scanner;
/** Here is the super class for Chair, Desk, Table
* class name FurnitureItem.class
* I have inlcuded the methods setTypeOfWood setIdNum and getIdNum.
* @author Kiril Anastasov
* @date 18/03/2012
*/
abstract class FurnitureItem implements Serializable
{
private int idNum;
private char typeOfWood;
protected double itemPrice;
private int quantity;
protected ImageIcon[] image; /*= { new ImageIcon("Pictures/Chair1.png"),
new ImageIcon("Pictures/Chair2.png"),
new ImageIcon("Pictures/Desk1.png"),
new ImageIcon("Pictures/Desk2.png"),
new ImageIcon("Pictures/Desk3.png"),
new ImageIcon("Pictures/Desk4.png"),
new ImageIcon("Pictures/Table1.png"),
new ImageIcon("Pictures/Table2.png")};
*/
//default constructor
public FurnitureItem()
{
idNum = 0;
typeOfWood = 'o' ; //oak or walnut
itemPrice = 0;
quantity = 0;
image = null;
}
//parameterized constructor
public FurnitureItem(int id, char tw, int qty, ImageIcon[] img)
{
idNum = id;
typeOfWood = tw;
itemPrice = 0;
quantity = qty;
image = img;
}
//mutator method for the id num
public void setIdNum(int id)
{
String prompt = "Please enter furniture's id number: \n" +
"furniture's id must be numbers only";
String tablesIdNumber = JOptionPane.showInputDialog(null, prompt);
Scanner input = new Scanner(tablesIdNumber);
id = input.nextInt();
idNum = id;
}
//accesor method for the id
public int getIdNum()
{
return idNum;
}
public double getItemPrice()
{
return itemPrice;
}
public double getTotalPrice()
{
return itemPrice * 0.03; // incomplete
}
//mutator method for type of wood
public void setTypeOfWood(char tw)
{
String prompt = "Please enter type of wood: \n" +
"type of wood can be oak or walnut";
boolean validFlag;
do
{
String chairsTypeOfWood = JOptionPane.showInputDialog(null, prompt);
prompt = "Invalid data, please re-enter type of wood o for oak and w for walnut";
Scanner input = new Scanner(chairsTypeOfWood);
validFlag = true;
tw = input.nextLine().charAt(0);
if(tw != 'o' && tw != 'w')
{
validFlag = false;
}
}while(!validFlag);
typeOfWood = tw;
}
//accesor method for type of wood
public char getTypeOfWood()
{
return typeOfWood;
}
//mutator method for quantity
public void setQuantity(int qty)
{
String prompt = "Please enter quantity. \n" +
"How many furnitures would you like";
boolean validFlag;
do
{
String chairsQuantity = JOptionPane.showInputDialog(null, prompt);
prompt = "Invalid data, the quantity of the chairs must be a possitive number, please re-enter";
Scanner input = new Scanner(chairsQuantity);
validFlag = true;
qty = input.nextInt();
if(qty < 0)
{
validFlag = false;
}
}while(!validFlag);
quantity = qty;
}
// accesor method for quantity
public int getQuantity()
{
return quantity;
}
public ImageIcon[] getImage()
{
return image;
}
public String toString()
{
return "The id is: " + idNum + " and the qunatity is: " + quantity +
"the type of wood is:" + typeOfWood +" and the price of the item is: " + itemPrice;
}
public int calcUnits() // make it abstract
{
return 5;
}
}
這是類主席這是FurnitureItem的子類。
import javax.swing.ImageIcon;
import java.util.Scanner;
import javax.swing.JOptionPane;
/** Here is the sub class for FurnitureItem
* class name Chair.class
* @author Kiril Anastasov
* @date 18/03/2012
*/
public class Chair extends FurnitureItem
{
// private int idNum;
// private char typeOfWood;
// private int quantity;
private boolean armRest;
// Chair useChair = new Chair();
//default constructor
public Chair()
{
armRest = false;
image = null;
itemPrice = 0.0;
// idNum = 0;
// typeOfWood = 0;
// quantity = 0;
}
//parameterized constructor
public Chair(int id, char tw, int qty, ImageIcon[] img, boolean a)
{
// idNum = id;
// typeOfWood = tw;
// quantity = qty;
image = null;
itemPrice = 0.0;
armRest = a;
}
public void setArmRest(boolean a)
{
String[] withArmRest = {"Yes please", "No thank you"};
String prompt = "Would you like an armrest for the chair";
int option = JOptionPane.showOptionDialog(null,
prompt,
"ArmRest",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
withArmRest,
withArmRest[0]);
if(option == 0)
{
a = true;
}
else
{
a = false;
}
armRest = a;
//System.out.println(useChair.toString());
}
public boolean getArmRest()
{
return armRest;
}
public String toString()
{
Chair myChair = new Chair();
return "The id is: " + myChair.getIdNum() + " and the price of the item is: " + myChair.getTotalPrice() +
" Is there an armrest ? ==> " + getArmRest() ;
}
public int calsUnits()
{
return getQuantity();
}
}