【问题标题】:Java - get JRadioButton cmd with Jbutton listenerJava - 使用 Jbutton 监听器获取 JRadioButton cmd
【发布时间】:2016-05-14 11:20:20
【问题描述】:

我是 java 新手,正在创建一个简单的 gui 应用程序。在这个简单的应用程序中,我正在尝试为公司写一封电子商务信函。所以,我计划我的应用程序是这样的......

首先我问用户他是想写一封信给英国公司还是美国人。为此,我使用了两个单选按钮(一个用于美国公司,第二个用于英国公司)和 JButton。当用户触发 jbutton 时,我想获取单选按钮命令(用户想要写哪种类型的字母)。

问题是我不知道触发 jButton 时获取 Radiobutton 命令。请给我一个简单的想法(如果可能的话,对于初学者来说例子并不复杂)以获得 RadioButtons 值..

这是我的java代码:

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

class englet{

    static public JFrame f;
    static public JPanel p;
    static class getTypeOfLetter implements ActionListener{

        public void actionPerformed( ActionEvent e){
            String btnInput = e.getActionCommand();
            System.out.println(btnInput);

        }
    }
    public static void askletter(){
        JRadioButton btnRadio1;
        JRadioButton btnRadio2;
        ButtonGroup btngrp;
        JButton btnGo = new JButton("Write");
        btnRadio1 = new JRadioButton("Write Letter For American Firm");
        btnRadio1.setActionCommand("Amer");
        btnRadio2 = new JRadioButton("Write Letter For British Firm");
        btnRadio2.setActionCommand("Brit");
        btngrp    = new ButtonGroup();
        btnGo.setActionCommand("WriteTest");
        btnGo.addActionListener(new getTypeOfLetter());
        btngrp.add(btnRadio1);
        btngrp.add(btnRadio2);
        p.add(btnRadio1);
        p.add(btnRadio2);
        p.add(btnGo);
    }
    englet(){
        f  = new JFrame("English Letter");
        p  = new JPanel();
        askletter();
        f.add(p);
        f.setSize(400,200);
        f.setVisible(true);

    }
    public static void main (String[] argv ){
        englet i = new englet();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

我使用的是 Notepad++ 和 CMD.. 没有任何其他工具,例如 netbeans initllli ecplisse。 **重新编辑**我想要一个可能的解决方案,并且可以满足我..这个应用程序有效,但我无法使用 jubtton 获得单选按钮命令..

【问题讨论】:

  • NullPointerExceptions 的启发式方法几乎总是相同的。 您应该仔细阅读异常的堆栈跟踪,找出出错的代码行、引发异常的行,然后仔细检查该行,找出哪个变量为空,然后回溯到您的代码看看为什么。你会一次又一次地遇到这些,相信我。
  • 但我问这个问题是为了得到一个可能的解决方案而不是错误..
  • 以后,将所有例外情况作为文本发布到您的问题中。从 cmd 窗口复制和粘贴文本很容易。您还总是希望指出哪些行引发了异常。
  • 只有 知道哪一行引发了异常 -- 再次查看该行上的变量并找出哪些为空。
  • Please give me an Simple Idea(if possible with exapmle not complicated for begginers) to get RadioButtons value - 你读过 JRadioButton API 吗?您将找到有关“如何使用单选按钮”的 Swing 教程的链接,其中包含有关如何测试操作命令的工作示例。 Swing 教程是您应该首先寻找示例的地方。

标签: java swing radio-button jbutton


【解决方案1】:

你有几个问题:

  • 过度使用静电。代码中的大部分字段和方法都应该是非静态的
  • 您缺少传输所需信息所必需的关键字段。要获取选定的 JRadioButton,您需要创建 JRadioButton 字段并检查选择了哪个字段,或者(以及我的偏好),您需要将 ButtonGroup 变量设置为字段并根据 ButtonGroup 返回的 ButtonModel 检查选择了哪个 JRadioButton。

    您当前正在使用局部变量,这些变量在整个类中不可见,这就是为什么 JRadioButtons 或 ButtonModel 大多是字段(在类中声明)。

  • 如果你使用上面的 ButtonModel,你必须给每个 JRadioButton 一个适当的 actionCommand 字符串。

例如:

import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class GetRadio extends JPanel {
    private static final String[] FIRMS = {"American Firm", "British Firm"};

    // You need this field to access it in your listener
    private ButtonGroup buttonGroup = new ButtonGroup();

    public GetRadio() {
        // create JButton and add ActionListener
        JButton button = new JButton("Select");
        button.addActionListener(new ButtonListener());

        // JPanel with a grid layout with one column and variable number of rows
        JPanel radioButtonPanel = new JPanel(new GridLayout(0, 1));
        radioButtonPanel.setBorder(BorderFactory.createTitledBorder("Select Firm")); // give it a title
        for (String firm : FIRMS) {
            // create radiobutton and set actionCommand
            JRadioButton radioButton = new JRadioButton(firm);
            radioButton.setActionCommand(firm);

            // add to button group and JPanel
            buttonGroup.add(radioButton);;
            radioButtonPanel.add(radioButton);
        }

        // add stuff to main JPanel
        add(radioButtonPanel);
        add(button);

    }

    private class ButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            // get button model of selected radio button from ButtonGroup
            ButtonModel model = buttonGroup.getSelection();

            // if null, no country selected
            if (model == null) {
                Component component = GetRadio.this;
                String message = "You must first select a country!";
                String title = "Error: No Country Selected";
                int type = JOptionPane.ERROR_MESSAGE;
                JOptionPane.showMessageDialog(component, message, title, type);
            } else {
                // valid country selected
                String country = model.getActionCommand();
                System.out.println("Letter to " + country);
            }
        }
    }

    private static void createAndShowGui() {
        GetRadio mainPanel = new GetRadio();

        JFrame frame = new JFrame("Get Radio Btn");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-03
    • 1970-01-01
    • 2023-03-17
    相关资源
    最近更新 更多