java:改变对JTextPane的关注

时间:2013-12-05 16:44:51

标签: java swing jframe jtextfield

我有一个Calculator类,它将从4个文本域中的3个中获取输入,然后使用我已经制作的Loan类计算另一个。

现在我需要添加“计算器样式”按钮,用户可以使用这些按钮将这些ammounts输入到TextFields中。这是我到目前为止所拥有的。 。 。

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

public class Calculator extends JFrame implements ActionListener
{
public static final int width = 400;
public static final int height = 300;
private JButton calculate;
private static JTextField amount;
private JTextField interest;
private JTextField term;
private JTextField payment;
private JLabel amountL;
private JLabel interestL;
private JLabel termL;
private JLabel paymentL;
private String i = "0";

public Calculator ()
{
    super("Loan Calculator");
    setSize(width, height);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    calculate = new JButton("payment");
    calculate.setFocusable(false);
    amount = new JTextField(10);
    interest = new JTextField(10);
    term = new JTextField(10);
    payment = new JTextField(10);

    JPanel numbButt = new JPanel();
    numbButt.setLayout(new GridLayout(4,3));
    JButton zero = new JButton("0");
    zero.setFocusable(false);
    JButton one = new JButton("1");
    one.setFocusable(false);
    JButton two = new JButton("2");
    two.setFocusable(false);
    JButton three = new JButton("3");
    three.setFocusable(false);
    JButton four = new JButton("4");
    four.setFocusable(false);
    JButton five = new JButton("5");
    five.setFocusable(false);
    JButton six = new JButton("6");
    six.setFocusable(false);
    JButton seven = new JButton("7");
    seven.setFocusable(false);
    JButton eight = new JButton("8");
    eight.setFocusable(false);
    JButton nine = new JButton("9");
    nine.setFocusable(false);
    JButton period = new JButton(".");
    period.setFocusable(false);
    numbButt.add(seven);
    seven.addActionListener(this);
    numbButt.add(eight);
    eight.addActionListener(this);
    numbButt.add(nine);
    nine. addActionListener(this);
    numbButt.add(four);
    four.addActionListener(this);
    numbButt.add(five);
    five.addActionListener(this);
    numbButt.add(six);
    six.addActionListener(this);
    numbButt.add(one);
    one.addActionListener(this);
    numbButt.add(two);
    two.addActionListener(this);
    numbButt.add(three);
    three.addActionListener(this);
    numbButt.add(zero);
    zero.addActionListener(this);
    numbButt.add(period);
    period.addActionListener(this);
    JButton enter = new JButton("enter");
    enter.setFocusable(false);
    numbButt.add(enter);
    enter.addActionListener(this);

    JPanel inputPanel = new JPanel();
    inputPanel.setLayout(new GridLayout(4,2));
    setLayout(new BorderLayout());
    amountL = new JLabel("amount");
    interestL = new JLabel("interest");
    termL = new JLabel("term");
    paymentL = new JLabel("payment");
    inputPanel.add(amountL);
    inputPanel.add(amount);
    inputPanel.add(interestL);
    inputPanel.add(interest);
    inputPanel.add(termL);
    inputPanel.add(term);
    inputPanel.add(paymentL);
    inputPanel.add(payment);
    add(inputPanel, BorderLayout.CENTER);

    calculate.addActionListener(this);
    JButton amountB = new JButton("amount");
    amountB.setFocusable(false);
    amountB.addActionListener(this);
    JPanel buttonPanel = new JPanel();
    buttonPanel.add(calculate);
    buttonPanel.add(amountB);
    add(buttonPanel, BorderLayout.SOUTH);
    add(numbButt, BorderLayout.WEST);
    interest.requestFocusInWindow();

    ActionListener actionListener = new ActionFocusMover();
    enter.addActionListener(actionListener);
}
@Override
public void actionPerformed(ActionEvent e) 
{

    String iamt, ii, iterm, ipmt;
    String buttonString = e.getActionCommand();
    Component x;
    if(buttonString.equals("1"))
        i=i+1;
    else if(buttonString.equals("2"))
        i=i+2;
    else if(buttonString.equals("3"))
        i=i+3;
    else if(buttonString.equals("4"))
        i=i+4;
    else if(buttonString.equals("5"))
        i=i+5;
    else if(buttonString.equals("6"))
        i=i+6;
    else if(buttonString.equals("7"))
        i=i+7;
    else if(buttonString.equals("8"))
        i=i+8;
    else if(buttonString.equals("9"))
        i=i+9;
    else if(buttonString.equals("0"))
        i=i+0;
    else if(buttonString.equals("."))
        i=i+".";
    else if(buttonString.equals("enter"))

    else if(buttonString.equals("payment"))
    {
        iamt = amount.getText();
        ii = interest.getText();
        iterm = term.getText();

        Loan justin = new Loan(Integer.parseInt(iterm),Double.parseDouble(ii), Double.parseDouble(iamt));

        payment.setText(Double.toString(justin.getPayment()));
    }
    else 
    {
        ii = interest.getText();
        iterm = term.getText();
        ipmt = payment.getText();

        Loan justin = new Loan(Double.parseDouble(ipmt), Integer.parseInt(iterm),Double.parseDouble(ii));

        amount.setText(Double.toString(justin.getAmount()));
    }

    amount.setText(i);
}

public static void setFocus()
{
    amount.requestFocusInWindow();
}
class ActionFocusMover implements ActionListener {
      public void actionPerformed(ActionEvent actionEvent) {
        KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
        manager.focusNextComponent();
      }
    }
}

我设置的方式允许将按钮输入字符串,然后暂时放入静态字段(金额)。我需要知道的是如何编辑来检查哪个对象当前具有焦点,并将inputbuttonString放入该对象中。 编辑

1 个答案:

答案 0 :(得分:1)

您可以使用requestFocusInWindow方法设置对组件的关注。确保组件可显示,可聚焦且可见。

来自How to Use the Focus Subsystem

  

如果您想确保特定组件获得焦点   第一次激活一个窗口,你可以调用   组件之后的组件上的requestFocusInWindow方法   已经实现,但在显示框架之前。

要在Amount之后设置requestFocusInWindow字段调用pack()的初始焦点。

相同的教程提供了有关focus traversal policyfocus traversal keys的示例。例如,如何将Enter添加为焦点遍历键:

Set forwardKeys = getFocusTraversalKeys(
    KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
Set newForwardKeys = new HashSet(forwardKeys);
newForwardKeys.add(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0));
setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
    newForwardKeys);

或者,您可以将焦点转移到JTextArea动作方法中,该方法会对Enter击键作出反应,即:

amount.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        interest.requestFocusInWindow();
    }
});