我想要创建的程序有问题。 我是编程的初学者,所以能帮助我吗?
好吧,我想采用ComboBox
方法中的void
内容,并将其用于其他类。
这是ComboBox: ** ** **
JComboBox ActionComboBox = new JComboBox();
ActionComboBox.addItem("Text");
ActionComboBox.addItem("text2");
ActionComboBox.addItem("text3");
ActionComboBox.setToolTipText("");
ActionComboBox.setBounds(253, 96, 103, 20);
frame.getContentPane().add(ActionComboBox);
** ** **
我想使用ComboBox
方法中void
的内容,在另一个类中的方法中使用该代码来执行此操作:
(我也是导入(类的名称))
** **
private String Action()
{
String actionBox = ActionComboBox.getSelectedItem();
return actionBox;
}
** **
嗯,程序说:: ActionComboBox
无法解决!
作为一个错误。
我该怎么办?
谢谢
答案 0 :(得分:1)
要完成任务,您需要按照以下步骤操作。
首先,将全局下面的代码行设置为包含类:
JComboBox ActionComboBox = new JComboBox();
其次,为getter
:
ActionComboBox
方法
public JComboBox getActionComboBox(){
return ActionComboBox;
}
然后在其他课程中,您可以使用getter
方法访问对ActionComboBox
的引用。
例如:
String actionBox = ActionComboBox.getSelectedItem();
会变成这样:
String actionBox = someInstanceName.getActionComboBox().getSelectedItem().toString();