.addComponent(jRadioButton3) .addComponent(jRadioButton4) .addComponent(jRadioButton5)) .addContainerGap()) .addGroup(jpanellayout.createSequentialGroup() .addGap(99,99,99) .addComponent(jButton1,javax.swing.GroupLayout.PREFERRED_SIZE,114,javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(110,简称最大值)) .addGroup(jpanellayout.createSequentialGroup() .addGap(69,69,69) .addComponent(jLabel1) .addGap(92,92,92)) .addGroup(jpanellayout.createSequentialGroup() .addGap(55,55,55) .addComponent(jPanel2,javax.swing.GroupLayout.PREFERRED\u SIZE,javax.swing.GroupLayout.DEFAULT\u SIZE,javax.swing.GroupLayout.PREFERRED\u SIZE) .addContainerGap(80,简称最大值)) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,jPanel1Layout.createSequentialGroup() .addContainerGap() .addComponent(jLabel1) .addGap(29,29,29) .addComponent(jPanel2,javax.swing.GroupLayout.PREFERRED\u SIZE,javax.swing.GroupLayout.DEFAULT\u SIZE,javax.swing.GroupLayout.PREFERRED\u SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,47,Short.MAX_值) .addComponent(jcombox1,javax.swing.GroupLayout.PREFERRED\u SIZE,javax.swing.GroupLayout.DEFAULT\u SIZE,javax.swing.GroupLayout.PREFERRED\u SIZE) .addGap(26,26,26) .addComponent(jRadioButton1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jRadioButton2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jRadioButton3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jRadioButton4) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jRadioButton5) .addGap(18,18,18) .addComponent(jButton1) .addGap(29,29,29)) ); javax.swing.GroupLayout=newjavax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(布局); layout.setHorizontalGroup( createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jPanel1,javax.swing.GroupLayout.DEFAULT\u SIZE,javax.swing.GroupLayout.DEFAULT\u SIZE,Short.MAX\u值) .addContainerGap()) ); layout.setVerticalGroup( createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jPanel1,javax.swing.GroupLayout.DEFAULT\u SIZE,javax.swing.GroupLayout.DEFAULT\u SIZE,Short.MAX\u值) .addContainerGap()) ); 包装(); }// 私有void jButton1ActionPerformed(java.awt.event.ActionEvent evt){ } /** *@param指定命令行参数 */ 公共静态void main(字符串参数[]){ invokeLater(new Runnable()){ 公开募捐{ 新建Gui().setVisible(true); } }); } //变量声明-不修改 私有javax.swing.ButtongGroup结果; 私有javax.swing.JButton jButton1; 私有javax.swing.JComboBox jcombox1; 私有javax.swing.JLabel jLabel1; 私有javax.swing.JPanel jPanel1; 私有javax.swing.JPanel jPanel2; 私有javax.swing.JRadioButton JRadioButton 1; 私有javax.swing.JRadioButton JRadioButton 2; 私有javax.swing.JRadioButton JRadioButton 3; 私有javax.swing.JRadioButton JRadioButton 4; 私有javax.swing.JRadioButton JRadioButton 5; //变量结束声明 }
通过扫描代码,看起来您已经在容器中初始化并布置了组件 为了使接口具有交互性,您需要研究Java Swing接口的另一个重要部分,即事件侦听器 基本上,您所做的一切(单击、值更改、移动鼠标、按键等)都会生成一个事件。然后编写对这些事件作出反应的事件侦听器.addComponent(jRadioButton3) .addComponent(jRadioButton4) .addComponent(jRadioButton5)) .addContainerGap()) .addGroup(jpanellayout.createSequentialGroup() .addGap(99,99,99) .addComponent(jButton1,javax.swing.GroupLayout.PREFERRED_SIZE,114,javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(110,简称最大值)) .addGroup(jpanellayout.createSequentialGroup() .addGap(69,69,69) .addComponent(jLabel1) .addGap(92,92,92)) .addGroup(jpanellayout.createSequentialGroup() .addGap(55,55,55) .addComponent(jPanel2,javax.swing.GroupLayout.PREFERRED\u SIZE,javax.swing.GroupLayout.DEFAULT\u SIZE,javax.swing.GroupLayout.PREFERRED\u SIZE) .addContainerGap(80,简称最大值)) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,jPanel1Layout.createSequentialGroup() .addContainerGap() .addComponent(jLabel1) .addGap(29,29,29) .addComponent(jPanel2,javax.swing.GroupLayout.PREFERRED\u SIZE,javax.swing.GroupLayout.DEFAULT\u SIZE,javax.swing.GroupLayout.PREFERRED\u SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,47,Short.MAX_值) .addComponent(jcombox1,javax.swing.GroupLayout.PREFERRED\u SIZE,javax.swing.GroupLayout.DEFAULT\u SIZE,javax.swing.GroupLayout.PREFERRED\u SIZE) .addGap(26,26,26) .addComponent(jRadioButton1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jRadioButton2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jRadioButton3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jRadioButton4) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jRadioButton5) .addGap(18,18,18) .addComponent(jButton1) .addGap(29,29,29)) ); javax.swing.GroupLayout=newjavax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(布局); layout.setHorizontalGroup( createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jPanel1,javax.swing.GroupLayout.DEFAULT\u SIZE,javax.swing.GroupLayout.DEFAULT\u SIZE,Short.MAX\u值) .addContainerGap()) ); layout.setVerticalGroup( createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jPanel1,javax.swing.GroupLayout.DEFAULT\u SIZE,javax.swing.GroupLayout.DEFAULT\u SIZE,Short.MAX\u值) .addContainerGap()) ); 包装(); }// 私有void jButton1ActionPerformed(java.awt.event.ActionEvent evt){ } /** *@param指定命令行参数 */ 公共静态void main(字符串参数[]){ invokeLater(new Runnable()){ 公开募捐{ 新建Gui().setVisible(true); } }); } //变量声明-不修改 私有javax.swing.ButtongGroup结果; 私有javax.swing.JButton jButton1; 私有javax.swing.JComboBox jcombox1; 私有javax.swing.JLabel jLabel1; 私有javax.swing.JPanel jPanel1; 私有javax.swing.JPanel jPanel2; 私有javax.swing.JRadioButton JRadioButton 1; 私有javax.swing.JRadioButton JRadioButton 2; 私有javax.swing.JRadioButton JRadioButton 3; 私有javax.swing.JRadioButton JRadioButton 4; 私有javax.swing.JRadioButton JRadioButton 5; //变量结束声明 },java,swing,radio-button,drop-down-menu,Java,Swing,Radio Button,Drop Down Menu,通过扫描代码,看起来您已经在容器中初始化并布置了组件 为了使接口具有交互性,您需要研究Java Swing接口的另一个重要部分,即事件侦听器 基本上,您所做的一切(单击、值更改、移动鼠标、按键等)都会生成一个事件。然后编写对这些事件作出反应的事件侦听器 检查此项。通过扫描代码,看起来您已经在容器中初始化并布置了组件 为了使接口具有交互性,您需要研究Java Swing接口的另一个重要部分,即事件侦听器 基本上,您所做的一切(单击、值更改、移动鼠标、按键等)都会生成一个事件。然后编写对这些事件作出
检查此项。通过扫描代码,看起来您已经在容器中初始化并布置了组件 为了使接口具有交互性,您需要研究Java Swing接口的另一个重要部分,即事件侦听器 基本上,您所做的一切(单击、值更改、移动鼠标、按键等)都会生成一个事件。然后编写对这些事件作出反应的事件侦听器
查看此项。对于您当前面临的问题,简而言之,问题是如何在按下JButton时从JComboBox和JradioButton集合中获取所选项。您如何处理这些信息取决于您的程序逻辑,实际上这不是问题所在。要了解这一点,我建议您转到Swing教程了解详细信息。简单地说,JComboBox中的选择只需在组合框上调用getSelectedItem即可获得。对于JRadioButton集合来说,这有点棘手,但我一直喜欢从保存JRadioButton的ButtonGroup中获得选择。下面的示例中显示了此方法和组合框的方法:
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class GetInfoFromCombosRadios extends JPanel {
private static final String[] STUFF_A = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
private static final String[] STUFF_B = {"Fe", "Fi", "Fo", "Fum"};
private JComboBox comboStuffA = new JComboBox(STUFF_A);
private ButtonGroup btnGroupStuffB = new ButtonGroup();
public GetInfoFromCombosRadios() {
JPanel radioPanel = new JPanel(new GridLayout(0, 1, 0, 5));
for (String stuffB : STUFF_B) {
JRadioButton radioBtn = new JRadioButton(stuffB);
radioBtn.setActionCommand(stuffB);
btnGroupStuffB.add(radioBtn);
radioPanel.add(radioBtn);
}
JPanel comboPanel = new JPanel();
comboPanel.add(comboStuffA);
JButton doItBtn = new JButton("Do It!");
doItBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
doItActionPerformed();
}
});
JPanel doItBtnPanel = new JPanel();
doItBtnPanel.add(doItBtn);
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
add(comboPanel);
add(radioPanel);
add(doItBtnPanel);
}
private void doItActionPerformed() {
String comboSelection = comboStuffA.getSelectedItem().toString();
System.out.println("Combo Selection: " + comboSelection);
String radioSelection = "";
ButtonModel selectedBtnModel = btnGroupStuffB.getSelection();
if (selectedBtnModel == null) {
radioSelection = "No radio button selected yet";
} else {
radioSelection = selectedBtnModel.getActionCommand();
}
System.out.println("Radio selection: " + radioSelection);
}
private static void createAndShowUI() {
JFrame frame = new JFrame("GetInfoFromCombosRadios");
frame.getContentPane().add(new GetInfoFromCombosRadios());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}
对于您当前面临的问题,请参阅
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class GetInfoFromCombosRadios extends JPanel {
private static final String[] STUFF_A = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
private static final String[] STUFF_B = {"Fe", "Fi", "Fo", "Fum"};
private JComboBox comboStuffA = new JComboBox(STUFF_A);
private ButtonGroup btnGroupStuffB = new ButtonGroup();
public GetInfoFromCombosRadios() {
JPanel radioPanel = new JPanel(new GridLayout(0, 1, 0, 5));
for (String stuffB : STUFF_B) {
JRadioButton radioBtn = new JRadioButton(stuffB);
radioBtn.setActionCommand(stuffB);
btnGroupStuffB.add(radioBtn);
radioPanel.add(radioBtn);
}
JPanel comboPanel = new JPanel();
comboPanel.add(comboStuffA);
JButton doItBtn = new JButton("Do It!");
doItBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
doItActionPerformed();
}
});
JPanel doItBtnPanel = new JPanel();
doItBtnPanel.add(doItBtn);
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
add(comboPanel);
add(radioPanel);
add(doItBtnPanel);
}
private void doItActionPerformed() {
String comboSelection = comboStuffA.getSelectedItem().toString();
System.out.println("Combo Selection: " + comboSelection);
String radioSelection = "";
ButtonModel selectedBtnModel = btnGroupStuffB.getSelection();
if (selectedBtnModel == null) {
radioSelection = "No radio button selected yet";
} else {
radioSelection = selectedBtnModel.getActionCommand();
}
System.out.println("Radio selection: " + radioSelection);
}
private static void createAndShowUI() {
JFrame frame = new JFrame("GetInfoFromCombosRadios");
frame.getContentPane().add(new GetInfoFromCombosRadios());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}