Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java上制作一个简单的键盘来高效地输入JTextFields?_Java_Jtextfield_Keypad - Fatal编程技术网

如何在java上制作一个简单的键盘来高效地输入JTextFields?

如何在java上制作一个简单的键盘来高效地输入JTextFields?,java,jtextfield,keypad,Java,Jtextfield,Keypad,我正在使用java上的一个键盘,用于在两个JTextField(x&y)中输入数据。一旦用户点击“回车”,它应该计算x和y的差值。我是否必须为每个按钮添加一组ActionListener??有没有一个有效的方法来做到这一点 到目前为止,在我的代码中,我已经拥有了所有需要的按钮,但我不想使用键盘,而是想使用虚拟键盘来输入数字(从技术上讲,是一个字符串)。有什么建议吗 import javax.swing.*; import java.awt.*; public class EmbeddedMai

我正在使用java上的一个键盘,用于在两个JTextField(x&y)中输入数据。一旦用户点击“回车”,它应该计算x和y的差值。我是否必须为每个按钮添加一组ActionListener??有没有一个有效的方法来做到这一点

到目前为止,在我的代码中,我已经拥有了所有需要的按钮,但我不想使用键盘,而是想使用虚拟键盘来输入数字(从技术上讲,是一个字符串)。有什么建议吗

import javax.swing.*;
import java.awt.*;

public class EmbeddedMain extends JFrame
{

    public static void main (String[] args)
        {
        EmbeddedMain em = new EmbeddedMain();
        }


    public EmbeddedMain()
    {
        setTitle("testing");
        setSize(450,350);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setLayout(new GridLayout(4,5,3,3));

    JButton button1= new JButton("7");
    JButton button2= new JButton("8");
    JButton button3= new JButton("9");
    JButton button4= new JButton("4");
    JButton button5= new JButton("5");
    JButton button6= new JButton("6");
    JButton button7= new JButton("1");
    JButton button8= new JButton("2");
    JButton button9= new JButton("3");
    JButton button0= new JButton("0");
    JButton buttonR= new JButton("Reset");
    JButton buttonE= new JButton("Enter");

     JTextField x = new JTextField("      ");
     JTextField y = new JTextField("      ");
     JTextField CPP_entry = new JTextField("    ");

   CPP_entry.setEditable(false);        

    add(button1);
    add(button2);
    add(button3);
    add(new JLabel("    x:")); 
    add(x); 
    add(button4);
    add(button5);
    add(button6);
    add(new JLabel("    y:")); 
    add(y); 
    add(button7);
    add(button8);
    add(button9);
    add(new JLabel("    x-y:")); 
    add(CPP_entry); 
    add(buttonR);
    add(button0);
    add(buttonE);

        setVisible(true);


        }

        }
两件事:

首先,您可以只使用一个
ActionListener
,只需注意将其添加到所有按钮,然后您可以执行以下操作:

  public void actionPerformed(ActionEvent e)
  {
     JButton currentButton = (JButton)e.getSource();
     int number = currentButton.getText().charAt(0) - '0';

     // do whatever with the number
  }

其次,您可以使用
按键监听器
捕捉键盘聚焦时按下的按键,该监听器有三种方法:
按键按下(按键事件e)
按键释放(按键事件e)
按键键入(按键事件e)
。看看oracle教程,了解如何使用它们。

我会制作一系列按钮,如下所示:

JButton[] buttonArray = new JButton[10];
for(int i = 0; i < buttonArray.length; i++ ) {
  buttonArray[i] = new JButton(String.valueOf(i));
  buttonArray[i].addActionListener(yourActionListener);
}
JButton[]buttonArray=newjbutton[10];
对于(int i=0;i
然后在事件触发时检查其来源。

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

   public class EmbeddedMain extends JFrame implements ActionListener 
   {
      private JTextField[] tfld = new JTextField[4];// x,y,z;
      private String sigma = " "+(char)0x2211;
      private String[] str = {"X=","Y=","Z=",sigma+"="};
      private String[] txt = {
         "7",     "8",    "9",     
         "4",     "5",    "6",    
         "1",     "2",    "3",    
         "Reset", "0",  "Enter"};
      private JButton[] but = new JButton[12];

      public static void main (String[] args)
      {
         EmbeddedMain em = new EmbeddedMain();
      }

      public EmbeddedMain() //constructor begins, method for embedded main class
      {
         setTitle("Subtraction");
         setSize(420,350);
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         setLocationRelativeTo(null);

         setLayout(new FlowLayout());
         JPanel copa = (JPanel)getContentPane();
         copa.setPreferredSize(dim(3*70+140+5*5, 4*70+5*5));
         copa.setBackground(new Color(100,255,100));

         for (int i = 0, j = 0, k = 1; i < but.length; i++, k++) {
            but[i] = new JButton(txt[i]);
            but[i].setPreferredSize(dim(70,70));
            but[i].setFont(new Font("default",0,18));
            but[i].setMargin(new Insets(0,0,0,0));
            but[i].addActionListener(this);
            but[i].setName(""+i);
            add(but[i]);

            if (k%3 == 0) {
               tfld[j]  = new JTextField(str[j]); 
               tfld[j].setPreferredSize(dim(140,70));
               add(tfld[j++]);
            }   
         }
         pack();
         setVisible(true);
      }

      public void actionPerformed(ActionEvent e)
      {
         Object src = e.getSource();
         if (src instanceof JButton) {
            JButton but = (JButton)src;
            String nm = but.getName();
            int nr = Integer.parseInt(nm)/3;
            String txt = but.getText();
            if (txt.equalsIgnoreCase("reset")) {
               for (int j = 0; j < tfld.length; j++)
                  tfld[j].setText(str[j]);
            }

            else if (txt.equalsIgnoreCase("enter")) {
            }

            else
               tfld[0].setText(tfld[0].getText() + txt);
         }  
      }

      public Dimension dim(int w, int h) {
         return new Dimension(w,h);
      }
   }
导入java.awt.event.*; 导入java.awt.*; 公共类EmbeddedMain扩展JFrame实现ActionListener { 私有JTextField[]tfld=新JTextField[4];//x,y,z; 私有字符串sigma=“”+(字符)0x2211; 私有字符串[]str={“X=”,“Y=”,“Z=”,sigma+“=”}; 专用字符串[]txt={ "7", "8", "9", "4", "5", "6", "1", "2", "3", “重置”、“0”、“输入”}; 私有JButton[]但=新JButton[12]; 公共静态void main(字符串[]args) { EmbeddedMain em=新的EmbeddedMain(); } public EmbeddedMain()//构造函数开始,用于嵌入式主类的方法 { 设置标题(“减法”); 设置大小(420350); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(空); setLayout(新的FlowLayout()); JPanel copa=(JPanel)getContentPane(); copa.setPreferredSize(尺寸(3*70+140+5*5,4*70+5*5)); copa.setBackground(新颜色(100255100)); 对于(int i=0,j=0,k=1;i
永远不要使用setXXSize