Swing-从另一个框架使用jComboBox



My Program将允许用户在三种表单之间导航。主菜单、客户信息和数据库。

我需要主菜单对象可以与客户表单提交按钮一起使用。例如:

有一个组合框的选择,我想:

String variable = jComboMainMenu1.getSelectedItem().toString;

如何将对象导入客户表单以使用所选的值?

编辑:

**我得到的错误是组合框对jframe是私有的。没有办法改变这一点,因为netbeans不允许我这样做?

说//变量声明-不要修改。

**我试过了:

public字符串car=jComboBox1.getSelectedItem().toString();(如您所知,这将把组合框中的值存储在一个变量中)。

然而,swing似乎限制性很强,因为即使是我的框架jbutton也被限制为私有,这意味着该变量不能公开用于其他框架上的按钮。

令人沮丧的是,它限制了我作为程序员更改代码:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){<--不能更改为public。

    new CustomerForm().setVisible(true);
    setVisible(false);
    String car = jComboBox1.getSelectedItem().toString();
}                                        

// Variables declaration - do not modify                                                 <-- can't be changed either.
private javax.swing.JButton jButton1;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JComboBox jComboBox2;
private javax.swing.JComboBox jComboBox3;
private javax.swing.JComboBox jComboBox4;
private javax.swing.JComboBox jComboBox5;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JTextField jTextField1;
// End of variables declaration                   

**我尝试过在IDE中编辑模板,但是更改灰色区域的方法似乎被严格封装了。

这样的事情应该很简单,当然吗?:P

编辑:调整为组合框

如果你想从客户表单访问主表单的组合框,你可以传递对该组合框的引用;例如,当您创建客户表单时。(将主表单链接到客户表单的另一种方法是在主表单的组合框中添加一个动作监听器,该监听器调用客户表单中的一个方法,该方法将所选项目作为参数传递。)

例如(使用Java 8):

import java.awt.BorderLayout;
import javax.swing.*;
public class MainMenu {
    private CustomerForm customerForm;
    public static void main(String[] arguments) {
        SwingUtilities.invokeLater(() -> new MainMenu().createAndShowGui());
    }
    private void createAndShowGui() {
        JFrame frame = new JFrame("Stack Overflow");
        frame.setBounds(100, 100, 800, 600);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        JPanel panel = new JPanel(new BorderLayout());
        String[] comboBoxItems = {"one", "two", "three"};
        JComboBox<String> jComboBox1 = new JComboBox<>(comboBoxItems);
        panel.add(jComboBox1, BorderLayout.NORTH);
        JButton button = new JButton("Do it!");
        panel.add(button, BorderLayout.SOUTH);
        frame.getContentPane().add(panel);
        Database database = new Database();
        customerForm = new CustomerForm(database, jComboBox1);
        button.addActionListener(actionEvent -> customerForm.doSomething());
        frame.setVisible(true);
    }
}

class CustomerForm {
    private Database database;
    private JComboBox<String> comboBox;
    public CustomerForm(Database database, JComboBox<String> comboBox) {
        this.database = database;
        this.comboBox = comboBox;
    }
    public void doSomething() {
        System.out.println();
        System.out.println("CustomerForm.doSomething");
        System.out.println("database: " + database);
        System.out.println("comboBox.getSelectedItem().toString(): "
                           + comboBox.getSelectedItem().toString());
    }
}

class Database {
}

最新更新