我真的不确定如何说出这个问题。但我要在这里尽我所能。忍受我,如果你可以:)使用对象值返回对象名称
我有一个数据库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