Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将ActionListener添加到JRadioButton&;下拉框_Java_Swing_Jcombobox_Jradiobutton_Itemlistener - Fatal编程技术网

Java 将ActionListener添加到JRadioButton&;下拉框

Java 将ActionListener添加到JRadioButton&;下拉框,java,swing,jcombobox,jradiobutton,itemlistener,Java,Swing,Jcombobox,Jradiobutton,Itemlistener,所以我想在这里实现的是,如果选择了第一个单选按钮和第一个组合框中的第一个选项,那么我想显示该类别下的食物,如果是第一个单选按钮和第一个组合框中的第二个选项,那么显示该类别下的其他食物。我让radiobutton部分正常工作,但无法将下拉部分与它一起使用。行rb1.isSelected()和&cmbItems[0].isSelected()显示错误。请帮我克服这个错误。这是我的密码: import java.awt.*; import java.awt.event.ActionEvent; imp

所以我想在这里实现的是,如果选择了第一个单选按钮和第一个组合框中的第一个选项,那么我想显示该类别下的食物,如果是第一个单选按钮和第一个组合框中的第二个选项,那么显示该类别下的其他食物。我让radiobutton部分正常工作,但无法将下拉部分与它一起使用。行rb1.isSelected()和&cmbItems[0].isSelected()显示错误。请帮我克服这个错误。这是我的密码:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class Tryout extends JFrame  implements ActionListener {

        private static final long serialVersionUID = 1L;

        private JTabbedPane tabbedPane = new JTabbedPane();
        private JPanel inputpanel;
        private JPanel searchpanel;
        public JButton submit;
        public JRadioButton rb1, rb2;
        public JComboBox <String> cmb;
        public String cmbItems [] ={"North Indian","South Indian","East Indian", "West Indian"};
        JFrame frame=new JFrame("Get selected JRadioButton");  

        public Tryout() {           
                inputpanel = createPage1();
                searchpanel = createPage2();
                tabbedPane.addTab("Input Form", inputpanel);
                tabbedPane.addTab("Search Form", searchpanel);
                this.add(tabbedPane, BorderLayout.CENTER);           
        }

        public JPanel createPage1() {           
            String cmbItems2 [] ={"European","Asian","American"};          
            JPanel panel = new JPanel();
            panel.setLayout(new GridBagLayout());
            GridBagConstraints c = new GridBagConstraints();
            //Column1
            c.anchor = GridBagConstraints.LINE_START;
            c.weightx = 0.5;
            c.weighty = 0.5;
            JLabel region = new JLabel("Enter Region");      
            c.gridx = 0;
            c.gridy = 0;
            panel.add(region, c);
            JLabel subregion = new JLabel("Enter Sub-Region");
            c.gridx = 0;
            c.gridy = 1;
            panel.add(subregion, c);
            //Column2
            c.anchor = GridBagConstraints.LINE_START;
            ButtonGroup bg = new ButtonGroup();
            rb1 = new JRadioButton("Indian"); 
            c.gridx = 1;
            c.gridy = 0;
            bg.add(rb1);
            panel.add(rb1, c);
            cmb = new JComboBox<String>(cmbItems);
            c.gridx = 1;
            c.gridy = 1;   
            panel.add(cmb, c);        
            //Column3
            c.anchor = GridBagConstraints.LINE_START;
            rb2 = new JRadioButton("International");
            c.gridx = 2;
            c.gridy = 0;           
            bg.add(rb2);
            panel.add(rb2, c);          
            JComboBox<String> cmb2 = new JComboBox<String>(cmbItems2);
            c.gridx = 2;
            c.gridy = 1;      
            cmb2.setEnabled(false);
            panel.add(cmb2, c); 
            submit = new JButton("Submit");
            c.weighty = 10;
            c.anchor = GridBagConstraints.FIRST_LINE_START;
            c.gridx = 1;
            c.gridy = 2;
            panel.add(submit, c);
            submit.addActionListener(this);           
            return panel;            
        }

        public void actionPerformed(ActionEvent e) {
            if(e.getSource()==submit) {
                if(rb1.isSelected() && cmbItems[0].isSelected()) {
                    JOptionPane.showMessageDialog(frame,"You select : "+rb1.getText());
                }
                else if(rb2.isSelected()) {
                    JOptionPane.showMessageDialog(frame,"You select : "+rb2.getText());
                }               
            }           
        }

        public JPanel createPage2() {           
                JPanel panel = new JPanel();
                panel.setLayout(new GridBagLayout());
                GridBagConstraints c = new GridBagConstraints();
                c.fill = GridBagConstraints.RELATIVE;
                JTextField field = new JTextField(20);
                panel.add(field);
                JButton search = new JButton("SEARCH");
                panel.add(search);
                return panel;
        }

        public static void main(String args[]) {
                SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                                Tryout ex = new Tryout();
                                ex.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                                ex.setSize(500,500);
                                ex.setVisible(true);
                        }
                });
        }       
}
import java.awt.*;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.*;
公共类试用扩展JFrame实现ActionListener{
私有静态最终长serialVersionUID=1L;
private JTabbedPane tabbedPane=新JTabbedPane();
专用JPanel输入面板;
私人JPanel搜索小组;
公共按钮提交;
公共JRadioButton rb1、rb2;
公共JComboxCMB;
公共项目[]={“北印度”、“南印度”、“东印度”、“西印度”};
JFrame=newjframe(“获取所选JRadioButton”);
公开试用({
inputpanel=createPage1();
searchpanel=createPage2();
tabbedPane.addTab(“输入表单”,inputpanel);
tabbedPane.addTab(“搜索表单”,搜索面板);
添加(选项卡窗格,BorderLayout.CENTER);
}
公共JPanel createPage1(){
字符串cmbItems2[]={“欧洲”、“亚洲”、“美国”};
JPanel面板=新的JPanel();
panel.setLayout(新的GridBagLayout());
GridBagConstraints c=新的GridBagConstraints();
//专栏1
c、 锚点=GridBagConstraints.LINE\u开始;
c、 权重x=0.5;
c、 权重y=0.5;
JLabel区域=新JLabel(“输入区域”);
c、 gridx=0;
c、 gridy=0;
增补(c区);
JLabel subsection=新JLabel(“输入子区域”);
c、 gridx=0;
c、 gridy=1;
增补小组(次区域,c);
//专栏2
c、 锚点=GridBagConstraints.LINE\u开始;
ButtonGroup bg=新建ButtonGroup();
rb1=新JRadioButton(“印度”);
c、 gridx=1;
c、 gridy=0;
bg.add(rb1);
增补(rb1,c);
cmb=新的JComboBox(cmb项目);
c、 gridx=1;
c、 gridy=1;
小组.增补(中巴,c);
//第3栏
c、 锚点=GridBagConstraints.LINE\u开始;
rb2=新JRadioButton(“国际”);
c、 gridx=2;
c、 gridy=0;
bg.add(rb2);
增补(rb2,c);
JCOMBOX cmb2=新的JCOMBOX(CMBS2);
c、 gridx=2;
c、 gridy=1;
cmb2.设置启用(假);
补充小组(cmb2,c);
提交=新按钮(“提交”);
c、 权重=10;
c、 锚点=GridBagConstraints.FIRST\u LINE\u START;
c、 gridx=1;
c、 gridy=2;
小组。添加(提交,c);
submit.addActionListener(this);
返回面板;
}
已执行的公共无效操作(操作事件e){
如果(如getSource()==提交){
if(rb1.isSelected()&&cmbItems[0].isSelected()){
showMessageDialog(框架,“您选择:”+rb1.getText());
}
else if(rb2.isSelected()){
showMessageDialog(框架,“您选择:”+rb2.getText());
}               
}           
}
公共JPanel createPage2(){
JPanel面板=新的JPanel();
panel.setLayout(新的GridBagLayout());
GridBagConstraints c=新的GridBagConstraints();
c、 fill=GridBagConstraints.RELATIVE;
JTextField=新的JTextField(20);
面板。添加(字段);
JButton search=新JButton(“search”);
面板。添加(搜索);
返回面板;
}
公共静态void main(字符串参数[]){
SwingUtilities.invokeLater(新的Runnable(){
@凌驾
公开募捐{
试用期ex=新试用期();
例如,setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ex.setSize(500500);
例如,setVisible(真);
}
});
}       
}

cmbItems
是一个字符串数组,因此它自然不知道它是否被“选中”。您想问组合框本身所选的项目是什么,所以您应该说

if(rb1.isSelected() && cmb.getSelectedIndex() == 0) {
    JOptionPane.showMessageDialog(frame,"You select : "+rb1.getText());
}

工作得很有魅力。我还有一件事要问你。我还想做的是,将两个组合框都保持禁用状态,当选择Indian时,我希望启用对应于Indian的下拉列表,然后单击按钮,显示相关消息。向两个单选按钮添加一个
ActionListener
。对于“Indian”ActionListener,使用
cmb.setEnabled(true)
启用组合框。对于“国际”ActionListener,使用
cmb.setEnabled(false)
禁用组合框。当GUI初始加载时,您可能还想禁用combobox。我无法完全实现,一个小示例会有所帮助。另外,您必须使cmb2成为最终版本,否则它将无法编译:)在您的提示的帮助下,我使用了另一种方法。非常感谢:)