如何将插入号码的用户限制为文本字段?

时间:2013-12-02 16:54:22

标签: java swing user-interface jtextfield limits

这是我的Sales.java

package assignment2;

import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Sales implements ActionListener{

private String paymentmethod[] = {"Visa","Master"};

private String first = "Crunchy Combo 1 - RM19.70";
private String second = "Crunchy Combo 2 - RM26.90";
private String third = "Hot Variety Box - RM28.28";
private String fourth = "Hot Power Meal 1 - RM22.90";
private String fifth = "Hot Power Meal 2 - RM53.90";

private double price1 = 19.70;
private double price2 = 26.90;
private double price3 = 28.28;
private double price4 = 22.90;
private double price5 = 53.90;
private double total = 0.0;

private JRadioButton crunchy1 = new JRadioButton("Crunchy Combo 1 - RM 19.70",true);
private JRadioButton crunchy2 = new JRadioButton("Crunchy Combo 2 - RM 26.90");
private JRadioButton hotvariety = new JRadioButton("Hot Variety Box - RM 28.28");
private JRadioButton hotpower1 = new JRadioButton("Hot Power Meal 1 - RM 22.90");
private JRadioButton hotpower2 = new JRadioButton("Hot Power Meal 2 - RM 53.90");
private ButtonGroup gp = new ButtonGroup();

private JLabel item = new JLabel("Select Item");    
private JLabel quantity = new JLabel("Quantity: ");
private JLabel payment = new JLabel("Payment Method: ");
private JLabel creditCard = new JLabel("Credit Card number: ");
/*item.setHorizontalAlignment(JLabel.LEFT);
quantity.setHorizontalAlignment(JLabel.LEFT);
payment.setHorizontalAlignment(JLabel.LEFT);
creditcard.setHorizontalAlignment(JLabel.LEFT);*/

private JTextField quantitytext = new JTextField("1",10);
private JTextField creditCardtext = new JTextField("1",10);
private JTextArea commenttext = new JTextArea();

private JComboBox combo = new JComboBox(paymentmethod);

private JButton done = new JButton("DONE!");

public void createGUI(){

    JFrame frame = new JFrame("Point of Sales");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new FlowLayout());
    frame.setBounds(100,100,500,400);

    gp.add(crunchy1);
    gp.add(crunchy2);
    gp.add(hotvariety);
    gp.add(hotpower1);
    gp.add(hotpower2);

    JPanel all = new JPanel();
    all.setLayout(new GridLayout(0,1));

    JPanel panelItem = new JPanel();
    panelItem.setLayout(new FlowLayout());

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

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

    JPanel panelTextArea = new JPanel();
    panelTextArea.setSize(500, 50);

    combo.setSelectedIndex(1);

    done.addActionListener(this);
    commenttext.setEditable(false);

    frame.add(all);
    all.add(panelItem);
    all.add(panelSales);
    all.add(panelTextArea);

    panelOption.add(crunchy1);
    panelOption.add(crunchy2);
    panelOption.add(hotvariety);
    panelOption.add(hotpower1);
    panelOption.add(hotpower2);

    panelItem.add(item,BorderLayout.WEST);
    panelItem.add(panelOption,BorderLayout.EAST);

    panelSales.add(quantity);
    panelSales.add(quantitytext);
    panelSales.add(payment);
    panelSales.add(combo);
    panelSales.add(creditCard);
    panelSales.add(creditCardtext);
    panelSales.add(done);

    panelTextArea.add(commenttext);

    frame.pack();
    frame.setVisible(true);
}

public static void main(String args[]){
    Sales sale = new Sales();
    sale.createGUI();
}

@Override
public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub
    if(arg0.getSource()==done)
    { 
        if(crunchy1.isSelected())
        { 
            total=price1*Double.parseDouble(quantitytext.getText());
            Object d=combo.getSelectedItem(); 
            String h=d.toString();
            commenttext.setText("Selected Item and Price per Unit:" + first + "\nQuantity: "+ quantitytext.getText() + "\nThe amount is RM" + total + " Charged to "+ h +" "+ creditCardtext.getText());
        }

        else if(crunchy2.isSelected())
        { 
            total=price2*Double.parseDouble(quantitytext.getText());
            Object d=combo.getSelectedItem(); 
            String h=d.toString();
            commenttext.setText("Selected Item and Price per Unit:" + second + "\nQuantity: "+ quantitytext.getText() + "\nThe amount is RM" + total + " Charged to "+ h +" "+ creditCardtext.getText());
        }

        else if(hotvariety.isSelected())
        { 
            total=price3*Double.parseDouble(quantitytext.getText());
            Object d=combo.getSelectedItem(); 
            String h=d.toString();
            commenttext.setText("Selected Item and Price per Unit:" + third + "\nQuantity: "+ quantitytext.getText() + "\nThe amount is RM" + total + " Charged to "+ h +" "+ creditCardtext.getText());
        }

        else if(hotpower1.isSelected())
        { 
            total=price4*Double.parseDouble(quantitytext.getText());
            Object d=combo.getSelectedItem(); 
            String h=d.toString();
            commenttext.setText("Selected Item and Price per Unit:" + fourth + "\nQuantity: "+ quantitytext.getText() + "\nThe amount is RM" + total + " Charged to "+ h +" "+ creditCardtext.getText());
        }

        else if(hotpower2.isSelected())
        { 
            total=price5*Double.parseDouble(quantitytext.getText());
            Object d=combo.getSelectedItem(); 
            String h=d.toString();
            commenttext.setText("Selected Item and Price per Unit:" + fifth + "\nQuantity: "+ quantitytext.getText() + "\nThe amount is RM" + total + " Charged to "+ h +" "+ creditCardtext.getText());
        }
    }
}

}

涉及的TextField是

private JTextField quantitytext = new JTextField("1",10);
private JTextField creditCardtext = new JTextField("1",10);

我应该如何编写try和catch()函数来限制用户输入? 由于项目数量和信用卡号码只能接受插入。 非常感谢您的帮助〜

1 个答案:

答案 0 :(得分:3)

您正在寻找DocumentFilter。这里有官方教程如何实现它。 Implementing a documentFilter。默认情况下,您的文档必须从AbstractDocument扩展。Swing文本组件使用AbstractDocument子类作为其文档。

myTextField.getDocument().setDocumentFilter( new MyDocumentFilter() )

class MyDocumentFilter extends DocumentFilter {

@Override
public void insertString(DocumentFilter.FilterBypass fb,
                int offset,
                String string,
                AttributeSet attr)
                  throws BadLocationException{

          //your business logic here
          // Example : if it's a number super.insertString(..) else do nothing.
       }

}

您也可以使用JFormattedTextfield。 How to use formatted textfields