returning values in a applet?
Anyone know how I can go about doing this inside a action li开发者_JAVA技巧stener? I have the following
private void (java.awt.event.ItemEvent evt) {
if (studentComboBox.getSelectedItem().equals("Student 1")){
jlabel1=return parameter ="i"
String i = getParameter("student2");
But im sure return parameter is not correct
Return allows you to specify a value to return. It doesn't allow you to do an assignment; it's up to the code that called your method to determine how to act.
In any case, your method studentComboBoxItemStateChanged
is a void
method which means it can't return a value (regular return
works, it just leaves the method).
It's up to you and what you need. The best solution in your case is probably to set some variable or call some function with a parameter in each of the if
s.
EDIT If you're trying to set the value of jlabel1 to "h"
, "i"
, "j"
if (studentComboBox.getSelectedItem().equals("Student 1")) { jlabel1.setValue("h"); }
....
But it looks like you're trying to use the variables h
, i
, and j
. Remember that "h"
is different than h
.
if (studentComboBox.getSelectedItem().equals("Student 1")) { jlabel1.setValue(h); }
....
But to do that, h
, i
, and j
will need to be defined as instance variables, outside of the method.
The best solution in this case, assuming the label should hold the value of getParameter("student1"), is to do just that:
if (studentComboBox.getSelectedItem().equals("Student 1")) { jlabel1.setValue(getParameter("student1")); }
....
As somebody else mentioned, a map would be very well-suited for this
Map<String, String> prettyNamesToParameterNames= ...;
private void StudentComboBoxItemStateChanged(java.awt.event.ItemEvent evt) {
String paramName=prettyNamestoParameterNames.get(studentComboBox.getSelectedItem());
label1.setValue(getParameter(paramName));
}
So, when the user selects a different value in the studentComboBox
, you want the jlabel1
to change its text? If so, this should do it:
String h = getParameter("student1");
String i = getParameter("student2");
String j = getParameter("student3");
private void studentComboBoxItemStateChanged(java.awt.event.ItemEvent evt) {
if (studentComboBox.getSelectedItem().equals("Student 1")){
jlabel1.setText(h);
} else if (studentComboBox.getSelectedItem().equals("Student 2")){
jlabel1.setText(i);
} else if (studentComboBox.getSelectedItem().equals("Student 3")){
jlabel1.setText(j);
}
}
Notice that studentComboBoxItemStateChanged
does not need to return anything. You just simply call methods on the jlabel1
object itself.
It looks like you are trying to set a variable based on a selected item in a JComboBox. To do that your syntax is wrong, you don't need to use the return keyword, just set the variable you want.
If you need to return a value, then just return the value you want to return and change your method signature to specify your return type.
Lastly, instead of using a set of messy if/else statements use a Map. It will be much clearer, much more efficient and much easier to change later. Also, does your code account for null being selected.
精彩评论