Java getCursor()不';我不能在我的办公室工作

Java getCursor()不';我不能在我的办公室工作,java,swing,cursor,mouseevent,actionlistener,Java,Swing,Cursor,Mouseevent,Actionlistener,所以我只是把作业的最后细节放进去,我注意到了getCursor();当我运行我的程序时工作,但在程序的后面(在两个actionListeners中),它不会转换为华氏温度。我想知道我是否能得到一些意见。非常感谢。这是我的密码: import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Part3Question1 extends JPanel { private JTextField fahr

所以我只是把作业的最后细节放进去,我注意到了getCursor();当我运行我的程序时工作,但在程序的后面(在两个actionListeners中),它不会转换为华氏温度。我想知道我是否能得到一些意见。非常感谢。这是我的密码:

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


public class Part3Question1 extends JPanel
{
private JTextField fahrenheitTB;
private JTextField celsiusResultTB;
private JButton tryAgain;

public Part3Question1()
{
    Color color=new Color(255,0,0);  

    JLabel fahrenheitLabel = new JLabel ("Enter Fahrenheit temperature:");
    add (fahrenheitLabel);

    fahrenheitTB = new JTextField (5);
    fahrenheitTB.setHorizontalAlignment(fahrenheitTB.CENTER);
    //Works here!
    fahrenheitTB.getCursor();
    add (fahrenheitTB);

    JLabel celsiusLabel = new JLabel ("Temperature in Celsius: ");
    add (celsiusLabel);

    celsiusResultTB = new JTextField (5);
    celsiusResultTB.setForeground(color);
    celsiusResultTB.setEditable(false);
    celsiusResultTB.setHorizontalAlignment(celsiusResultTB.CENTER);
    add (celsiusResultTB);

    JButton convertButton = new JButton("Convert!");
    add (convertButton);

    tryAgain = new JButton("Try Again!");
    tryAgain.setVisible(false);
    add (tryAgain);

    convertButton.addActionListener (new myFirstActionListener());
    tryAgain.addActionListener (new mysecondActionListener());
}
private class myFirstActionListener implements ActionListener
{
  public void actionPerformed (ActionEvent event)
  {
          int fahrenheitTemp, celsiusTemp;
          String text = fahrenheitTB.getText();
          try
          {
              fahrenheitTemp = Integer.parseInt (text);
              celsiusTemp = (fahrenheitTemp-32) * 5/9;
              celsiusResultTB.setText (Integer.toString (celsiusTemp));
              tryAgain.setVisible(true);

          }
          catch (NumberFormatException e)
          {
              String myException="Invalid entry. Please enter Integers Only";
              JOptionPane.showMessageDialog(null,myException);
              fahrenheitTB.setText("");
              //doesn't work here!
              fahrenheitTB.getCursor();
          }
      }
}
private class mysecondActionListener implements ActionListener
{
  public void actionPerformed (ActionEvent event)
  {
          fahrenheitTB.setText("");
          //doesn't work here!
          fahrenheitTB.getCursor();
          celsiusResultTB.setText("");
      }
}

public static void main (String[] args)
{
    JFrame myFrame = new JFrame ();
    myFrame.setTitle("Fahrenheit to Celsius Converter");
    myFrame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    myFrame.setPreferredSize(new Dimension(300,150));
    myFrame.setLocation(400,300);
    Part3Question1 panel = new Part3Question1();
    myFrame.getContentPane().add(panel);
    myFrame.pack();
    myFrame.setVisible(true);
}
}

一个好的起点是阅读本教程:。对于您的情况,您可以尝试调用您的组件

以下是您的更新代码:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class Part3Question1 extends JPanel {
    private JTextField fahrenheitTB;
    private JTextField celsiusResultTB;
    private JButton tryAgain;

    public Part3Question1() {
        Color color = new Color(255, 0, 0);

        JLabel fahrenheitLabel = new JLabel("Enter Fahrenheit temperature:");
        add(fahrenheitLabel);

        fahrenheitTB = new JTextField(5);
        fahrenheitTB.setHorizontalAlignment(fahrenheitTB.CENTER);
        // updated
        fahrenheitTB.requestFocusInWindow();
        add(fahrenheitTB);

        JLabel celsiusLabel = new JLabel("Temperature in Celsius: ");
        add(celsiusLabel);

        celsiusResultTB = new JTextField(5);
        celsiusResultTB.setForeground(color);
        celsiusResultTB.setEditable(false);
        celsiusResultTB.setHorizontalAlignment(celsiusResultTB.CENTER);
        add(celsiusResultTB);

        JButton convertButton = new JButton("Convert!");
        add(convertButton);

        tryAgain = new JButton("Try Again!");
        tryAgain.setVisible(false);
        add(tryAgain);

        convertButton.addActionListener(new myFirstActionListener());
        tryAgain.addActionListener(new mysecondActionListener());
    }

    private class myFirstActionListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            int fahrenheitTemp, celsiusTemp;
            String text = fahrenheitTB.getText();
            try {
                fahrenheitTemp = Integer.parseInt(text);
                celsiusTemp = (fahrenheitTemp - 32) * 5 / 9;
                celsiusResultTB.setText(Integer.toString(celsiusTemp));
                tryAgain.setVisible(true);

            } catch (NumberFormatException e) {
                String myException = "Invalid entry. Please enter Integers Only";
                JOptionPane.showMessageDialog(null, myException);
                fahrenheitTB.setText("");
            }finally{
                // updated
                fahrenheitTB.requestFocusInWindow();
                    }
        }
    }

    private class mysecondActionListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            fahrenheitTB.setText("");
            // updated
            fahrenheitTB.requestFocusInWindow();
            celsiusResultTB.setText("");
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame myFrame = new JFrame();
                myFrame.setTitle("Fahrenheit to Celsius Converter");
                myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                myFrame.setPreferredSize(new Dimension(300, 150));
                myFrame.setLocation(400, 300);
                Part3Question1 panel = new Part3Question1();
                myFrame.getContentPane().add(panel);
                myFrame.pack();
                myFrame.setVisible(true);

            }
        });
    }
}

一个好的起点是阅读本教程:。对于您的情况,您可以尝试调用您的组件

以下是您的更新代码:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class Part3Question1 extends JPanel {
    private JTextField fahrenheitTB;
    private JTextField celsiusResultTB;
    private JButton tryAgain;

    public Part3Question1() {
        Color color = new Color(255, 0, 0);

        JLabel fahrenheitLabel = new JLabel("Enter Fahrenheit temperature:");
        add(fahrenheitLabel);

        fahrenheitTB = new JTextField(5);
        fahrenheitTB.setHorizontalAlignment(fahrenheitTB.CENTER);
        // updated
        fahrenheitTB.requestFocusInWindow();
        add(fahrenheitTB);

        JLabel celsiusLabel = new JLabel("Temperature in Celsius: ");
        add(celsiusLabel);

        celsiusResultTB = new JTextField(5);
        celsiusResultTB.setForeground(color);
        celsiusResultTB.setEditable(false);
        celsiusResultTB.setHorizontalAlignment(celsiusResultTB.CENTER);
        add(celsiusResultTB);

        JButton convertButton = new JButton("Convert!");
        add(convertButton);

        tryAgain = new JButton("Try Again!");
        tryAgain.setVisible(false);
        add(tryAgain);

        convertButton.addActionListener(new myFirstActionListener());
        tryAgain.addActionListener(new mysecondActionListener());
    }

    private class myFirstActionListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            int fahrenheitTemp, celsiusTemp;
            String text = fahrenheitTB.getText();
            try {
                fahrenheitTemp = Integer.parseInt(text);
                celsiusTemp = (fahrenheitTemp - 32) * 5 / 9;
                celsiusResultTB.setText(Integer.toString(celsiusTemp));
                tryAgain.setVisible(true);

            } catch (NumberFormatException e) {
                String myException = "Invalid entry. Please enter Integers Only";
                JOptionPane.showMessageDialog(null, myException);
                fahrenheitTB.setText("");
            }finally{
                // updated
                fahrenheitTB.requestFocusInWindow();
                    }
        }
    }

    private class mysecondActionListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            fahrenheitTB.setText("");
            // updated
            fahrenheitTB.requestFocusInWindow();
            celsiusResultTB.setText("");
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame myFrame = new JFrame();
                myFrame.setTitle("Fahrenheit to Celsius Converter");
                myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                myFrame.setPreferredSize(new Dimension(300, 150));
                myFrame.setLocation(400, 300);
                Part3Question1 panel = new Part3Question1();
                myFrame.getContentPane().add(panel);
                myFrame.pack();
                myFrame.setVisible(true);

            }
        });
    }
}

问题在于您的
myFirstActionListener
,焦点请求从未被调用

异常块只有在存在异常时才会执行,否则将忽略它

而不是

} catch (NumberFormatException e) {
    String myException = "Invalid entry. Please enter Integers Only";
    JOptionPane.showMessageDialog(null, myException);
    fahrenheitTB.setText("");
    //doesn't work here!
    fahrenheitTB.requestFocusInWindow();
}
你需要使用

} catch (NumberFormatException e) {
    String myException = "Invalid entry. Please enter Integers Only";
    JOptionPane.showMessageDialog(null, myException);
    fahrenheitTB.setText("");
    //doesn't work here!
} finally {
    fahrenheitTB.requestFocusInWindow();
}
在我的测试中,“重试”按预期工作


在个人备忘中,您也可以在请求focus
fahrenheitTB.selectAll
后拨打
selectAll
。这是个人的事情,但现在用户不需要手动清除字段来输入下一个值…;)

问题在于您的
myFirstActionListener
,焦点请求从未被调用

异常块只有在存在异常时才会执行,否则将忽略它

而不是

} catch (NumberFormatException e) {
    String myException = "Invalid entry. Please enter Integers Only";
    JOptionPane.showMessageDialog(null, myException);
    fahrenheitTB.setText("");
    //doesn't work here!
    fahrenheitTB.requestFocusInWindow();
}
你需要使用

} catch (NumberFormatException e) {
    String myException = "Invalid entry. Please enter Integers Only";
    JOptionPane.showMessageDialog(null, myException);
    fahrenheitTB.setText("");
    //doesn't work here!
} finally {
    fahrenheitTB.requestFocusInWindow();
}
在我的测试中,“重试”按预期工作


在个人备忘中,您也可以在请求focus
fahrenheitTB.selectAll
后拨打
selectAll
。这是个人的事情,但现在用户不需要手动清除字段来输入下一个值…;)

你所说的“代码”不起作用是什么意思?据我所知,你什么都没做?你想把焦点转移到场上吗??然后您需要使用
requestFocusInWindow
getCursor
将简单地返回该字段正在使用的鼠标光标形状。@MadProgrammer,你说得对,我不清楚。基本上我是想让光标回到华氏温度。我要试试你说的话。感谢您的回复。因此我尝试了requestFocusInWindow,但光标没有在华氏温度下重新出现。您所说的“代码”不起作用是什么意思?据我所知,你什么都没做?你想把焦点转移到场上吗??然后您需要使用
requestFocusInWindow
getCursor
将简单地返回该字段正在使用的鼠标光标形状。@MadProgrammer,你说得对,我不清楚。基本上我是想让光标回到华氏温度。我要试试你说的话。感谢您的回复。因此我尝试了requestFocusInWindow,但光标没有在华氏温度下重新出现。+1因为它在正确的轨道上-1因为缺少一个明显的错误…修复,我将返回;)孙杰,非常感谢你。我注意到您添加了几行代码。我需要仔细阅读这些,看看它们是什么。我对此非常陌生,因此,例如,您在main中添加的代码,我必须学习。感谢您的帮助和投入。@MadProgrammer:Ohh,您的意思是在最后一块有这个:)Woops,请注意:在发布之前检查您的答案!:D+1表示在正确的轨道上-1表示缺少一个明显的错误…修复后,我会将其返回;)孙杰,非常感谢你。我注意到您添加了几行代码。我需要仔细阅读这些,看看它们是什么。我对此非常陌生,因此,例如,您在main中添加的代码,我必须学习。感谢您的帮助和投入。@MadProgrammer:Ohh,您的意思是在最后一块有这个:)Woops,请注意:在发布之前检查您的答案!:D