2012-06-25 15 views
5

Tôi có một chương trình mà tôi tính cho 1 trong 2 biến tùy thuộc vào nút radio được chọn. Tôi đang cố gắng sử dụng getValue để nhận giá trị từ trường JFormattedText và hiển thị nó trên trường JFormattedText khác (cuối cùng tôi sẽ thực hiện một số phép tính với số).Sử dụng getValue cho JFormattedText để hiển thị trong một trường JFormattedText khác

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

public class FutureValueFrame extends JFrame 
{ 
    public FutureValueFrame() 
    { 
     setTitle("Sample App"); 
     setSize(400,400); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 


    public static void main(String[] args) 
    { 
     JFrame f = new FutureValueFrame(); 

     //GUI and BUTTONS 
     JRadioButton monthlyRadioButton = new JRadioButton("Monthly Payment"); 
     JRadioButton loanAmountButton = new JRadioButton("Loan Amount"); 
     ButtonGroup selection = new ButtonGroup(); 
     selection.add(monthlyRadioButton); 
     selection.add(loanAmountButton); 

     JFormattedTextField loanAmountField = new JFormattedTextField(new DecimalFormat("####.##")); 
     JFormattedTextField interestRateField = new JFormattedTextField(new DecimalFormat("####.##")); 
     JFormattedTextField yearField = new JFormattedTextField(new DecimalFormat("####.##")); 
     JFormattedTextField monthlyPaymentField = new JFormattedTextField(new DecimalFormat("####.##")); 


     JPanel menuPanel = new JPanel(); 
     menuPanel.setLayout(new GridLayout(1,2)); 

     //ACTION LISTENER FOR RADIO BUTTONS 
     monthlyRadioButton.addActionListener(new SelectionListener(monthlyRadioButton, loanAmountButton, loanAmountField, monthlyPaymentField)); 
     loanAmountButton.addActionListener(new SelectionListener(monthlyRadioButton, loanAmountButton, loanAmountField, monthlyPaymentField)); 

     JPanel topPanel = new JPanel(); 
     topPanel.setLayout(new GridLayout(1,2)); 
     topPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 
     topPanel.add(monthlyRadioButton); 
     topPanel.add(loanAmountButton); 

     JPanel botPanel = new JPanel(); 
     botPanel.setLayout(new GridLayout(4,2)); 

     botPanel.add(new JLabel("Loan Amount:")); 
     botPanel.add(loanAmountField); 

     botPanel.add(new JLabel("Yearly Interest Rate:")); 
     botPanel.add(interestRateField); 

     botPanel.add(new JLabel("Number of Years:")); 
     botPanel.add(yearField); 

     botPanel.add(new JLabel("Monthly Payment:")); 
     botPanel.add(monthlyPaymentField); 

     JPanel container = new JPanel(); 
     container.setLayout(new GridLayout(3,1)); 
     container.add(topPanel); 
     container.add(botPanel); 
     container.add(menuPanel); 

     f.add(container); 

     JButton calculateButton = new JButton("Calculate"); 

     calculateButton.addActionListener(new CalculateMonthlyListener(loanAmountField, interestRateField, yearField, monthlyPaymentField, selection, monthlyRadioButton, loanAmountButton)); 


     JButton exitButton = new JButton("Exit"); 
     exitButton.addActionListener(new ExitListener()); 

     menuPanel.add(calculateButton); 
     menuPanel.add(exitButton);  

     f.setVisible(true); 
     f.setLocationRelativeTo(null); 
    } 

    class CalculateMonthlyListener implements ActionListener 
    { 
     private JFormattedTextField loanAmountField; 
     private JFormattedTextField monthlyPaymentField; 
     private JFormattedTextField interestRateField; 
     private JFormattedTextField yearField; 
     private double result; 
     private ButtonGroup selection; 
     private JRadioButton monthlyRadioButton; 
     private JRadioButton loanAmountButton; 

     public CalculateMonthlyListener (JFormattedTextField loanAmountField, 
             JFormattedTextField interestRateField, 
             JFormattedTextField yearField, 
             JFormattedTextField monthlyPaymentField, 
             ButtonGroup selection, 
             JRadioButton monthlyRadioButton, 
             JRadioButton loanAmountButton) 
     { 
      this.interestRateField = interestRateField; 
      this.yearField = yearField; 
      this.loanAmountField = loanAmountField; 
      this.selection = selection; 
      this.monthlyRadioButton = monthlyRadioButton; 
      this.loanAmountButton = loanAmountButton; 
      this.monthlyPaymentField = monthlyPaymentField; 
     } 

     public void actionPerformed(ActionEvent e) 
     { 
      if (selection.getSelection().equals(monthlyRadioButton.getModel())) 
      { 
       result = ((Double) interestRateField.getValue()).floatValue(); 
       monthlyPaymentField.setValue(new Double(result)); 
       System.out.println("You selected monthly"); 
      } 
      else 
      { 
       loanAmountField.setValue(new Double(12.22)); 
       System.out.println("You selected loan"); 
      } 
     } 
    } 

    class ExitListener implements ActionListener 
    { 
     public void actionPerformed(ActionEvent event) 
     { 
      //f.dispose(); 
      System.exit(0); 
      //System.out.println("You clicked exit"); 
     } 
    } 

    class SelectionListener implements ActionListener 
    { 
     private JRadioButton monthlyRadioButton; 
     private JRadioButton loanAmountButton; 
     private JFormattedTextField loanAmountField; 
     private JFormattedTextField monthlyPaymentField; 

     public SelectionListener (JRadioButton monthlyRadioButton, 
           JRadioButton loanAmountButton, 
           JFormattedTextField loanAmountField, 
           JFormattedTextField monthlyPaymentField) 
     { 
      this.monthlyRadioButton = monthlyRadioButton; 
      this.loanAmountButton = loanAmountButton; 
      this.loanAmountField = loanAmountField; 
      this.monthlyPaymentField = monthlyPaymentField; 
     } 

     public void actionPerformed(ActionEvent event) 
     { 
      if(event.getSource() == monthlyRadioButton) 
      { 
       loanAmountField.setEditable(false); 
       monthlyPaymentField.setEditable(true); 
      } 
      else 
      { 
       monthlyPaymentField.setEditable(false); 
       loanAmountField.setEditable(true); 
      } 
     } 
    } 
} 

Vấn đề của tôi xảy ra trong đoạn mã dưới đây:

public void actionPerformed(ActionEvent e) { 
    if (selection.getSelection().equals(monthlyRadioButton.getModel())) { 
     result = ((Double) interestRateField.getValue()).floatValue(); 
     monthlyPaymentField.setValue(new Double(result)); 
     System.out.println("You selected monthly"); 
    } else { 
     loanAmountField.setValue(new Double(12.22)); 
     System.out.println("You selected loan"); 
    } 
} 

Ở đây, tôi đang cố gắng để gán getValue đến kết quả. Tôi đã xem tài liệu Oracle và nó có vẻ như mã để có được giá trị chỉ đơn giản là interestRateField.getValue(); Khi tôi đã thử điều này mặc dù, tôi nhận được một lỗi nói Can't convert object to double vì vậy tôi đã thêm floatValue() và bỏ nó để loại bỏ lỗi.

Khi tôi nhấn tính toán, nó không hiển thị đầu vào từ interestRateField trong monthlyPaymentField.

Làm thế nào để nhận được giá trị của tôi (từ JFormattedTextField sử dụng DecimalFormat) sử dụng getValue và sau đó hiển thị nó trong một khác nhau JFormattedTextField?

+0

Thật không khuyến khích làm công cụ Swing trên một chủ đề khác so với EDT. Nó có thể dẫn đến rất nhiều vấn đề. Chỉ cần nói ... –

Trả lời

5

Hơi điều chỉnh câu trả lời của Hovercraft Full Of Lươn sử dụng getValuesetValue phương pháp của JFormattedTextField và bỏ qua phân tích cú pháp bằng tay/định dạng

import javax.swing.AbstractAction; 
import javax.swing.JButton; 
import javax.swing.JFormattedTextField; 
import javax.swing.JFrame; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
import java.awt.event.ActionEvent; 
import java.text.DecimalFormat; 

public class FutureValueFrame extends JFrame { 
    public static void main(String[] args) { 
    DecimalFormat format = new DecimalFormat("####.##"); 
    //in case you always want to see the 2 fraction digits 
    //format.setMinimumFractionDigits(2); 
    final JFormattedTextField field1 = new JFormattedTextField(
     format); 
    final JFormattedTextField field2 = new JFormattedTextField(
     format); 
    field1.setColumns(15); 
    field2.setColumns(15); 
    JButton btn = new JButton(new AbstractAction("Multiply by 2") { 

     @Override 
     public void actionPerformed(ActionEvent e) { 
     Number value = (Number) field1.getValue(); 
     if (value != null){ 
      field2.setValue(2 * value.doubleValue()); 
     } 
     } 
    }); 

    JPanel panel = new JPanel(); 
    panel.add(field1); 
    panel.add(btn); 
    panel.add(field2); 
    JOptionPane.showMessageDialog(null, panel); 
    } 
} 

này ít nhất sử dụng JFormattedTextField vì nó được thiết kế, nhưng vẫn có tất cả các khả năng sử dụng 'quirks' của JFormattedTextField.

Có một blogpost trên the tips4java site về improving the JFormattedTextField giải quyết vấn đề lớn nhất và có thể đáng giá hơn (mặc dù tôi vẫn nghĩ rằng phiên bản có thể được cải thiện, chẳng hạn như tô màu nền đỏ ngay sau khi dữ liệu không hợp lệ được nhập để cung cấp cho người dùng một đầu mối trực quan.)

+0

Tôi đã xóa câu trả lời của mình. Cảm ơn bạn đã tìm kiếm và dạy chúng tôi giải pháp tốt hơn! 1+ –

Các vấn đề liên quan