내가 만들고자하는 프로그램에는 세 개의 텍스트 필드가 여러 버튼 (각 숫자를 나타내는)과 함께 표시됩니다. 키보드를 사용하지 않고 버튼을 클릭하여 입력 할 수있게하려고합니다.버튼을 사용하여 텍스트 필드에 입력하는 방법은 무엇입니까?
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.text.*;
public class GUI extends JFrame {
private JButton[] numPad = new JButton[10];
private JTextField totalBill = new JTextField();
private JTextField totalPeople = new JTextField();
private JTextField tipPercentage = new JTextField();
private JTextField tipAmount = new JTextField();
private JTextField grandTotal = new JTextField();
private JTextField totalPerPerson = new JTextField();
private JButton doneButton = new JButton("Done");
private JButton clearButton = new JButton("Clear");
////////////////////////////////////////////////////////////////////////
private JPanel superContainer;
private JPanel container;
private JPanel panel1 = new JPanel();
private JPanel panel2 = new JPanel();
public GUI() {
//Set JFrame title.
super("Tip Calculator");
superContainer = new JPanel();
superContainer.setLayout(new BoxLayout(superContainer, BoxLayout.Y_AXIS));
//Create a container to hold two GridLayouts beside one another.
container = new JPanel();
container.setLayout(new BoxLayout(container, BoxLayout.X_AXIS));
//Create panels to be placed in container panel.
panel1.setPreferredSize(new Dimension(500, 250));
panel1.setLayout(new GridLayout(4,3,10,10));
panel2.setPreferredSize(new Dimension(500, 250));
panel2.setLayout(new GridLayout(7,2,10,10));
//Populate all the JButtons for the numPad.
for (int i=0; i<=9; i++) {
numPad[i] = new JButton(Integer.toString(i));
}
//Place each numPad button on the first JPanel.
for (int i=1; i<=9; i++) {
panel1.add(numPad[i]);
}
panel1.add(numPad[0]);
//Populate second GridLayout.
panel2.add(new JLabel("Total Bill: "));
panel2.add(totalBill);
panel2.add(new JLabel("Total People: "));
panel2.add(totalPeople);
panel2.add(new JLabel("Total Percentage: "));
panel2.add(tipPercentage);
panel2.add(doneButton);
panel2.add(clearButton);
panel2.add(new JLabel("Tip Amount: "));
panel2.add(tipAmount);
panel2.add(new JLabel("Grand Total: "));
panel2.add(grandTotal);
panel2.add(new JLabel("Total/Person: "));
panel2.add(totalPerPerson);
grandTotal.setEditable(false);
tipAmount.setEditable(false);
totalPerPerson.setEditable(false);
//Add the first GridLayout panel to the container.
container.add(panel1);
//Create a space between the GridLayout panels.
container.add(Box.createRigidArea(new Dimension(30,0)));
//Add the second GridLayout panel to the container.
container.add(panel2);
//Same as above but with title ontop and container panel below.
superContainer.add(new JLabel("Title"));
superContainer.add(Box.createRigidArea(new Dimension(0,30)));
superContainer.add(container);
//The panel the JFrame uses.
this.setContentPane(superContainer);
TheHandler handler = new TheHandler();
doneButton.addActionListener(handler);
clearButton.addActionListener(handler);
}
private class TheHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource()==doneButton) {
tipAmount.setText(Double.toString(Double.parseDouble(totalBill.getText()) * (Double.parseDouble(tipPercentage.getText())/100)));
grandTotal.setText(Double.toString(Double.parseDouble(tipAmount.getText()) + Double.parseDouble(totalBill.getText())));
totalPerPerson.setText(Double.toString(Double.parseDouble(grandTotal.getText())/Double.parseDouble(totalPeople.getText()))); }
else if (e.getSource()==clearButton) {
grandTotal.setText("");
tipAmount.setText("");
totalPerPerson.setText("");
totalBill.setText("0");
tipPercentage.setText("0");
totalPeople.setText("1");
totalBill.requestFocus();
totalBill.selectAll(); }
}
}
}
인터넷을 검색 한 후, 나는 다음과 같은 코드를 건너 온 : 지금까지, 나는이 가지고있는 유일한 문제는
private class AddDigit extends TextAction {
private String digit;
public AddDigit(String digit) {
super(digit);
this.digit = digit;
}
public void actionPerformed(ActionEvent e) {
JTextComponent component = getFocusedComponent();
component.replaceSelection(digit);
}
}
, 내가 어떻게 (내가 찾은 코드를 사용하는 단서가 없다 두 번째 블록 인 코드).
그래서 사용자가 수동으로 필드에 값을 입력하는 대신 num pad를 "클릭"할 수 있습니까? 또는 숫자 패드를 키보드 숫자 패드와 같이 작동 시키려면 숫자를 입력하면 해당 단추가 트리거됩니까? – MadProgrammer
@Reimeus 첫 번째 코드 블록이 제 코드입니다. 두 번째 블록이 첫 번째 블록과 어떻게 작동하는지 궁금합니다. – DTHCND
@MadProgrammer 그래서 키보드를 사용하여 숫자판을 클릭 할 수 있습니다. – DTHCND