2016-02-04 52 views
0

我是新來的java和創建一個簡單的gui應用程序。在這個簡單的應用程序中,我試圖爲企業寫一封電子商務信函。所以,我計劃我的應用這樣的東西..Java - 用Jbutton聽衆獲取JRadioButton cmd

首先,我要求用戶,如果他想寫一封信給英國公司或美國。爲此,我使用兩個單選按鈕(一個用於美國公司,另一個用於英國)和JButton。當用戶觸發jbutton然後我想要獲得單選按鈕命令(哪種字母用戶想寫)。

問題是我沒有任何想法得到Radiobutton命令,當我觸發jButton。請給我一個簡單的想法(如果可能的exapmle並不複雜的begginers)得到單選按鈕值..

這是我的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); 
    } 
} 

我使用記事本+ +和CMD ..不基於任何其他像netbeans initllli ecplisse這樣的工具。 **重新編輯**我想一個可能的解決方案,能滿足我..這個應用的工作原理,但我沒能獲得與單選按鈕jubtton條命令..

+0

啓發式爲NullPointerExceptions的是幾乎總是相同的。 **你應該仔細閱讀你的異常的堆棧跟蹤,找到有故障的代碼行,引發異常的代碼行,然後仔細檢查該行**,找出哪個變量爲空,然後追溯到代碼中以查看爲什麼。你會一次又一次遇到這些,相信我。 –

+0

但我問這個問題得到一個可能的解決方案,不會出現錯誤。 –

+0

將來,將所有異常作爲文本發佈到您的問題。從cmd窗口複製和粘貼文本很容易。你也總是想要指出哪些行會拋出異常。 –

回答

2

你有幾個問題:

  • 過度使用靜態。您的代碼的大部分字段和方法應該是非靜態的
  • 您缺少密鑰字段這將是傳輸所需信息所必需的。要獲取選定的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(); 
      } 
     }); 
    } 
}