我真的不確定如何說出這個問題。但我要在這裏盡我所能。忍受我,如果你可以:)使用對象值返回對象名稱
我有一個數據庫3表(我現在正在處理)。幸運的是,它們都有相同數量的列。我試圖用一個「彈出」的形式輸入值。 (不知道如何做到這一點,但使用this link here IM作爲指導方針,並希望它的工作原理)
這裏是我迄今該方法編寫的代碼..
public form(int option, String val1, String val2, String val3, String val4, String val5)
{
val1 = null;
val2 = null;
val3 = null;
val4 = null;
val5 = null;
JTextField val1Field = new JTextField(20);
JTextField val2Field = new JTextField(20);
JTextField val3Field = new JTextField(20);
JTextField val4Field = new JTextField(20);
JTextField val5Field = new JTextField(20);
String name;
String lbl1 = null;
String lbl2 = null;
String lbl3 = null;
String lbl4 = null;
String lbl5 = null;
switch(option)
{
case 1: //if customer
name = "Customer Information";
lbl1 = "Customer No:";
lbl2 = "Customer Name:";
lbl3 = "Company Name:";
lbl4 = "Contact Number: ";
lbl5 = "Discount Rate:";
case 2: //if item
name = "Item Information";
lbl1 = "Item No:";
lbl2 = "Item Name:";
lbl3 = "Cost Price:";
lbl4 = "Selling Price: ";
lbl5 = "Stock:";
case 3: //if user
name = "Staff Information";
lbl1 = "Staff ID:";
lbl2 = "Full Name:";
lbl3 = "Username:";
lbl4 = "Password: ";
lbl5 = "adminusercheck:";
default:
JOptionPane.showMessageDialog(alphaPOS,
"Something went wrong! Try again!",
"ERROR",
JOptionPane.ERROR_MESSAGE);
}
JPanel formPanel = new JPanel();
formPanel.add(new JLabel(lbl1));
formPanel.add(val1Field);
formPanel.add(Box.createHorizontalStrut(15)); // a spacer
formPanel.add(new JLabel(lbl2));
formPanel.add(val2Field);
formPanel.add(Box.createHorizontalStrut(15)); // a spacer
formPanel.add(new JLabel(lbl3));
formPanel.add(val3Field);
formPanel.add(Box.createHorizontalStrut(15)); // a spacer
formPanel.add(new JLabel(lbl4));
formPanel.add(val4Field);
formPanel.add(Box.createHorizontalStrut(15)); // a spacer
formPanel.add(new JLabel(lbl5));
formPanel.add(val5Field);
formPanel.add(Box.createHorizontalStrut(15)); // a spacer
int result = JOptionPane.showConfirmDialog(null, formPanel,
name, JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION)
{
val1 = val1Field.getText();
val2 = val2Field.getText();
val3 = val3Field.getText();
val4 = val4Field.getText();
val5 = val5Field.getText();
}
return(option, val1, val2, val3, val4, val5);
}
現在..它花了我一段時間才意識到我不能返回這樣的值,而是我可以改爲返回對象。我爲每個這些「表」(項目,客戶和員工)制定了一個課程。 但是..事情是在上面的方法中,我需要使用開關,以便我可以根據表的類型製作標籤。
所以我的問題是,有沒有辦法將對象及其名稱傳入方法?或者我有這一切錯誤?
任何幫助,非常感謝。
這是爲了成爲一種方法還是構造函數? – 2014-10-28 15:55:02
方法。構造函數是在別處寫的。 – Tsar 2014-10-28 15:57:25
Java沒有「輸出」參數。所以如果你的意圖是調用者會傳遞類似'val1','val2'等字符串變量,並且該方法將填充它們的值,那麼你不能用Java在其他語言中這樣做(如C++或PHP或Ada)。 – ajb 2014-10-28 15:58:55