这是你的程序,它工作正常,我不知道你为什么会收到错误,即使你做了pat_Order final(Private),我只是使用 pat_Order 的静态方法。为了表明这一点,您已经从组合框中选择了一些东西,我添加了对话框,它显示了您选择的东西,,。:) 如果您对我的程序还有任何疑问,请随时问我,导入 java.awt.event.ItemListener;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class comboBoxProblem extends JFrame{
static JPanel myPanel = new JPanel();
static String pat_order;
public static void main(String [] args)
{
new comboBoxProblem().show();
}
public comboBoxProblem()
{
setTitle("Combo");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
myPanel.setLayout(new GridBagLayout());
myPanel.setBorder(BorderFactory.createTitledBorder("Button's"));
final JComboBox jc = new JComboBox();
jc.addItem("ARR");
jc.addItem("SRR");
gbc.gridx = 0;
gbc.gridy = 1;
jc.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent ie)
{
String order = (String) jc.getSelectedItem();
pat_order = order;
JOptionPane.showConfirmDialog(null, pat_order, " Message Dialog Box", JOptionPane.DEFAULT_OPTION );
}
});
myPanel.add(jc, gbc);
gbc.gridx = 1;
gbc.gridy = 0;
getContentPane().add(myPanel, gbc);
pack();
}
}