J帧。在另一个 class 中使用 getSelectedItem() 从 ComboBox 获取值
Jframe. Get value from ComboBox, in another class, using getSelectedItem()
我在 class 中有主要的 jframe 代码:
@SuppressWarnings("serial")
public class CreateBuildAndPr extends JFrame {
....some code...
private JComboBox comboBoxClients = new JComboBox();
private JComboBox comboBoxBranch = new JComboBox();
....some code...
public String getClient(){
String getClient = comboBoxClients.getSelectedItem().toString();
//System.out.printf("\nClient: \n" + getClient);
return getClient;
}
/**
* Create the frame.
*/
public CreateBuildAndPr() {
lblCreateBuildAnd.setFont(new Font("Tahoma", Font.BOLD, 11));
comboBoxBranch.setModel(new DefaultComboBoxModel(new String[] {"1a", "2a", "3a", "4a"}));
comboBoxClients.setModel(new DefaultComboBoxModel(new String[] {"1", "2", "3"}));
textFieldInfo.setColumns(10);
btnCreateBuild.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
CreateNewBuildAndPr callSe = new CreateNewBuildAndPr();
callSe.newBuild();
}
});
initGUI();
}
因此,当我在 CreateBuildAndPr class 中调用此方法 getClient() 时,从 ComboBox 中选择的值是正确的。让我们说:“2”。
但是当我从其他 class 调用时,return 结果总是“1”。
这是另一个 class:
public class CreateNewBuildAndPr extends ConnectionsUrlAndDb {
@Test
public void newBuild() {
CreateBuildAndPr createBuildAndPr = new CreateBuildAndPr();
System.out.printf("\n\nSelenium: " +createBuildAndPr.getClient());
String info = createBuildAndPr.getInfo();
System.out.printf("\n\nSelenium: " +info);
String branch = createBuildAndPr.getBranch();
System.out.printf("\n\nSelenium: " +branch);
... more code .... }
如何更正其他 class 中的 getSelectedItem?
这一行
CreateBuildAndPr createBuildAndPr = new CreateBuildAndPr();
创建一个 class 的新对象。您想要的是使用 JComboBox
及其选定值来引用现有的。解决方案是将组合框(或者甚至是对包含它的框架的引用)作为参数传递给 CreateNewBuildAndPr
对象。
例如修改您的 CreateNewBuildAndPr
class 以包含一个变量
private JComboBox clientCombo;
并在 class 中定义一个新的构造函数为
public CreateNewBuildAndPr (JComboBox clientCombo)
{
this.clientCombo = clientCombo
}
并在您的 JFrame
ActionListener
中将组合框作为变量传递
CreateNewBuildAndPr callSe = new CreateNewBuildAndPr(comboBoxClients);
这将允许您在 CreateNewBuildAndPr
class.
中引用此组合框
clientCombo.getSelectedItem()...
这里我只是用单曲JComboBox
作为例子。如果您传递了整个 GUI 对象,您可以访问它并使用它的方法,例如您似乎拥有的 getInfo()
。
我在 class 中有主要的 jframe 代码:
@SuppressWarnings("serial")
public class CreateBuildAndPr extends JFrame {
....some code...
private JComboBox comboBoxClients = new JComboBox();
private JComboBox comboBoxBranch = new JComboBox();
....some code...
public String getClient(){
String getClient = comboBoxClients.getSelectedItem().toString();
//System.out.printf("\nClient: \n" + getClient);
return getClient;
}
/**
* Create the frame.
*/
public CreateBuildAndPr() {
lblCreateBuildAnd.setFont(new Font("Tahoma", Font.BOLD, 11));
comboBoxBranch.setModel(new DefaultComboBoxModel(new String[] {"1a", "2a", "3a", "4a"}));
comboBoxClients.setModel(new DefaultComboBoxModel(new String[] {"1", "2", "3"}));
textFieldInfo.setColumns(10);
btnCreateBuild.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
CreateNewBuildAndPr callSe = new CreateNewBuildAndPr();
callSe.newBuild();
}
});
initGUI();
}
因此,当我在 CreateBuildAndPr class 中调用此方法 getClient() 时,从 ComboBox 中选择的值是正确的。让我们说:“2”。 但是当我从其他 class 调用时,return 结果总是“1”。 这是另一个 class:
public class CreateNewBuildAndPr extends ConnectionsUrlAndDb {
@Test
public void newBuild() {
CreateBuildAndPr createBuildAndPr = new CreateBuildAndPr();
System.out.printf("\n\nSelenium: " +createBuildAndPr.getClient());
String info = createBuildAndPr.getInfo();
System.out.printf("\n\nSelenium: " +info);
String branch = createBuildAndPr.getBranch();
System.out.printf("\n\nSelenium: " +branch);
... more code .... }
如何更正其他 class 中的 getSelectedItem?
这一行
CreateBuildAndPr createBuildAndPr = new CreateBuildAndPr();
创建一个 class 的新对象。您想要的是使用 JComboBox
及其选定值来引用现有的。解决方案是将组合框(或者甚至是对包含它的框架的引用)作为参数传递给 CreateNewBuildAndPr
对象。
例如修改您的 CreateNewBuildAndPr
class 以包含一个变量
private JComboBox clientCombo;
并在 class 中定义一个新的构造函数为
public CreateNewBuildAndPr (JComboBox clientCombo)
{
this.clientCombo = clientCombo
}
并在您的 JFrame
ActionListener
中将组合框作为变量传递
CreateNewBuildAndPr callSe = new CreateNewBuildAndPr(comboBoxClients);
这将允许您在 CreateNewBuildAndPr
class.
clientCombo.getSelectedItem()...
这里我只是用单曲JComboBox
作为例子。如果您传递了整个 GUI 对象,您可以访问它并使用它的方法,例如您似乎拥有的 getInfo()
。