gpt4 book ai didi

java:改变对 JTextPane 的关注

转载 作者:行者123 更新时间:2023-12-01 13:45:23 25 4
gpt4 key购买 nike

我有一个计算器类,它将从 4 个文本字段中的 3 个获取输入,然后使用我已经创建的 Loan 类计算另一个文本字段。

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

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 到该对象中。 编辑

最佳答案

您可以使用requestFocusInWindow方法将焦点设置在组件上。确保组件可显示、可聚焦且可见。

来自How to Use the Focus Subsystem

If you want to ensure that a particular component gains the focus the first time a window is activated, you can call the requestFocusInWindow method on the component after the component has been realized, but before the frame is displayed.

要在 Amount 字段上设置初始焦点,请在 pack() 之后调用 requestFocusInWindow

同一教程提供了有关焦点遍历策略焦点遍历键的示例。例如如何添加 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 按键使用react,即:

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

关于java:改变对 JTextPane 的关注,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20405745/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com