2010-12-13 38 views
0

私の小さな変換プログラムは、私が修正して助けを必要とすることはできませんいくつかの問題があります。私は、テキストフィールドがするまでsetEditable(偽)になりたいのJava JMenuにし、JTextFieldのない文字列

  1. をユーザーは通貨を選択しました。ユーザーが通貨を選択するまでテキストフィールドに何も入力できないようにする
  2. ユーザーがjtextfieldにnumber以外を入力した場合、resultLabelはエラーメッセージを表示する必要があります。

私は最初の部分で試してみましたが、テキストフィールド全体を編集できないようにしました。

/* 
* 
* Currency Converter Window 
* A currency converting program that accepts user defined amount 
* and converts that amount in one of four currencies. 
* 
* Date 
* @author 
* 
*/ 

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

public class CurrencyConverterWin extends JFrame 
{ 

    //three panels for the GUI 
    private JPanel inputPanel; 
    private JPanel resultPanel; 
    private JPanel menuPanel; 
    //labels that identify the fields 
    private JLabel promptLabel; 
    private JLabel resultLabel; 
    private JLabel selectLabel; 
    //menu for the list of currencies 
    private JMenu currencyMenu; 
    private JMenuBar currencyMenuBar; 
    //input field for user to enter currency 
    private JTextField inputField; 
    private JButton goButton; 


    //initial values for each currency to 1 sterling 
    private double euros = 1.22; 
    private double japaneseYen = 152.07; 
    private double russianRubles = 42.53; 
    private double usDollars = 1.55; 

    public CurrencyConverterWin()      //constructor 
    { 
     super(); 
     this.setSize(600, 150);       //set size of the window 
     this.setLayout(new GridLayout(3, 1));   //split the grid with panels 
     this.setTitle("Currency Converter Window");  //set window title 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //close window 

     this.selectLabel = new JLabel("Select a currency to convert to: ", JLabel.RIGHT); 

     this.resultLabel = new JLabel(" ", JLabel.CENTER); 

     this.currencyMenu = new JMenu("(no currency selected)");  //create a menu of currencies 

     JMenuItem Euros = new JMenuItem("Euros");      //store the string Euros as a menu item 
     Euros.addActionListener(new java.awt.event.ActionListener()  //add a listener to this item 
     { 
      public void actionPerformed(java.awt.event.ActionEvent evt) //listen for event 
      { 
       menuChanged(evt); 
      } 
     }); 
     this.currencyMenu.add(Euros); 

     JMenuItem JapaneseYen = new JMenuItem("Japanese Yen");   //store the string Japanese Yen as a menu item 
     JapaneseYen.addActionListener(new java.awt.event.ActionListener() //add a listener to this item 
     { 
      public void actionPerformed(java.awt.event.ActionEvent evt) 
      { 
       menuChanged(evt); 
      } 
     }); 
     this.currencyMenu.add(JapaneseYen); 

     JMenuItem RusRubbles = new JMenuItem("Russian Rubles");   //store the string russian rubles as a menu item 
     RusRubbles.addActionListener(new java.awt.event.ActionListener() //add a listener to this item 
     { 
      public void actionPerformed(java.awt.event.ActionEvent evt) 
      { 
       menuChanged(evt); 
      } 
     }); 
     this.currencyMenu.add(RusRubbles); 

     JMenuItem USD = new JMenuItem("US Dollars");     //store the string US Dollars as a menu item 
     USD.addActionListener(new java.awt.event.ActionListener()  //add a listener to this item 
     { 
      public void actionPerformed(java.awt.event.ActionEvent evt) 
      { 
       menuChanged(evt); 
      } 
     }); 
     this.currencyMenu.add(USD); 


     currencyMenuBar = new JMenuBar();    //initialise a new menubar and add it to the currency menu 
     currencyMenuBar.add(currencyMenu); 

     this.menuPanel = new JPanel(); 
     this.menuPanel.add(this.selectLabel); 
     this.menuPanel.add(this.currencyMenuBar); 
     this.add(this.menuPanel); 

     this.promptLabel = new JLabel("(select a currency first) ", JLabel.RIGHT); 
     this.resultLabel = new JLabel(" ", JLabel.CENTER); 

     this.inputField = new JTextField("", 8); 
     //this.amountField.setEditable(false); //need help with this part 


     this.goButton = new JButton("GO"); 
     goButton.addActionListener(new java.awt.event.ActionListener() 
     { 

      public void actionPerformed(java.awt.event.ActionEvent evt) 
      { 
       buttonClicked(evt); 
      } 
     }); 

     this.inputPanel = new JPanel(); 
     this.inputPanel.add(this.promptLabel); 
     this.inputPanel.add(this.inputField); 
     this.inputPanel.add(this.goButton); 

     this.add(this.inputPanel); 

     this.resultPanel = new JPanel(); 
     this.resultPanel.add(this.resultLabel); 
     this.add(this.resultPanel); 
    } 

    /* 
    * change the state of the menu bar depending on the selected currency 
    */ 
    public void menuChanged(ActionEvent e) 
    { 
     if (e.getActionCommand().equals("Euros")) 
     { 
      currencyMenu.setText("Euros"); 
     } 
     if (e.getActionCommand().equals("Japanese Yen")) { 
      currencyMenu.setText("Japanese Yen"); 
     } 

     if (e.getActionCommand().equals("Russian Rubles")) { 
      currencyMenu.setText("Russian Rubles"); 
     } 

     if (e.getActionCommand().equals("US Dollars")) { 
      currencyMenu.setText("US Dollars"); 
     } 

    } 

    /* 
    * Events listeners for goButton 
    * when the goButton is clicked it should return the user's initial value 
    * plus the converted amount and some predefined strings. 
    */ 
    public void buttonClicked(ActionEvent evt) 
    { 
     if(currencyMenu.getText().equals("Euros")) 
     { 
      resultLabel.setText(inputField.getText() + " in sterling is " + EurosToSterling() + " Euros."); 
     } 
     if(currencyMenu.getText().equals("Japanese Yen")) 
     { 
      resultLabel.setText(inputField.getText() + " in sterling is " + JapaneseYenToSterling() + " Japanese Yen."); 
     } 
     if(currencyMenu.getText().equals("Russian Rubles")) 
     { 
      resultLabel.setText(inputField.getText() + " in sterling is " + RussianRublesToSterling() + " Russian Rubles."); 
     } 
     if(currencyMenu.getText().equals("US Dollars")) 
     { 
      resultLabel.setText(inputField.getText() + " in sterling is " + USDollarsToSterling() + " US Dollars."); 
     } 
    } 

    /* 
    * Functions for converting currencies 
    * get the user entry from inputField, convert it to a 
    * double and multiply it by the rate of a particular 
    * currency to a sterling. 
    */ 

    //calculate the rate for euros 
    double EurosToSterling() 
    { 
     double calcTotal = Double.parseDouble(inputField.getText()) * euros; 
     return calcTotal; 
    } 
    //calculate the conversion rate for japanese yen 
    double JapaneseYenToSterling() 
    { 
     double calcTotal = Double.parseDouble(inputField.getText()) * japaneseYen; 
     return calcTotal; 
    } 
    //calculate the rate for russian rubles 
    double RussianRublesToSterling() 
    { 
     double calcTotal = Double.parseDouble(inputField.getText()) * russianRubles; 
     return calcTotal; 
    } 
    //calculate the rate for us dollars 
    double USDollarsToSterling() 
    { 
     double calcTotal = Double.parseDouble(inputField.getText()) * usDollars; 
     return calcTotal; 
    } 

    /* 
    * main method to initialise CurrencyConverterWin 
    */ 
    public static void main(String[] args) 
    { 
     CurrencyConverterWin CurConWin = new CurrencyConverterWin(); 
     CurConWin.setVisible(true); 
    } 
} 

答えて

0

あなたは、ユーザーがテキストボックスに入力した内容を適切なフォーマットを持っている場合のKeyListenerをチェックすることをお勧めします:

おかげ は、ここに私のコードです。

2

あなただけの数字は、2つの一般的なアプローチがある場合:

A)のJFormattedTextFieldを使用します。

b)テキストフィールドのドキュメントにDocumentFilterを追加します。

どちらのアプローチも、Swing tutorialで詳しく説明されています。 「書式付きテキストフィールドの使用方法」および「テキストコンポーネントの機能(ドキュメントフィルタの実装)」の節を参照してください。

0

あなたの例は本当に長く、私はあなたが今何をしているのか正確にはわかりません。しかし、あなたの質問に答えてください:

  1. テキストボックスを編集不可に設定してください。 リスナーを選択に追加し、通貨を選択すると を入力して、 テキストボックスを編集可能に設定します。
  2. の不要な形式を防ぐには、JFormattedTextFieldを使用します。