gpt4 book ai didi

java - 将 ActionListener 添加到 JRadioButton 和 JComboBox

转载 作者:行者123 更新时间:2023-11-30 04:27:19 24 4
gpt4 key购买 nike

所以我在这里想要实现的是,如果选择了第一个单选按钮和第一个组合框中的第一个选项,那么我想在该类别下显示食物,如果它是第一个单选按钮和第二个选项第一个组合框然后是该类别下的其他一些食物。我让单选按钮部分工作正常,但无法同时获得下拉部分。 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);
}
});
}
}

最佳答案

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

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

关于java - 将 ActionListener 添加到 JRadioButton 和 JComboBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15538263/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com