簡體   English   中英

getCursor()在我的actionListener中不起作用

[英]getCursor() doesn't work in my actionListener

因此,我只是把分配的最后細節放進去,我注意到了getCursor();。 當我運行程序時,它可以工作,但是稍后在該程序中(在兩個actionListener中),它不會轉向華氏度TB。 我想知道是否能得到一些建議。 非常感謝。 這是我的代碼:

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);
}
}

一個很好的起點是閱讀本教程: “如何使用Focus子系統” 根據您的情況,您可以嘗試在組件上調用requestFocusInWindow()

這是您的更新代碼:

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();
}

在我的測試中,“再試一次”按預期工作

就我個人而言,你可能還需要調用selectAll您請求焦點后fahrenheitTB.selectAll 這是個人的事情,但是現在用戶無需手動清除該字段即可在其中輸入下一個值...;)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM