2012-10-31 43 views
0

所以我只是把我的任務的最後細節放在裏面,我注意到了getCursor();當我運行我的程序時工作,但後來在程序中(在兩個actionListeners中)它不會轉化爲華氏TB。我想知道我是否可以得到一些輸入。非常感謝。這裏是我的代碼:getCursor()在我的actionListener中不工作

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

你是什麼意思,它不工作?據我所知,你沒有做任何事情?你想把焦點移到這個領域嗎?然後你需要使用'requestFocusInWindow'。 'getCursor'只會返回該字段正在使用的鼠標光標形狀。 – MadProgrammer

+0

@MadProgrammer,你是對的,我不清楚。基本上我試圖讓光標回到華氏度。我會嘗試你所說的。感謝您的迴應。 –

+0

所以我嘗試了requestFocusInWindow並且光標沒有重新出現在華氏TB中。 –

回答

4

好的一點是閱讀本教程:"How to Use the Focus Subsystem"。對於你的情況,你可以試試你的組件

這是你更新的代碼上調用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); 

      } 
     }); 
    } 
} 
+0

你確定這個工作;) – MadProgrammer

+0

+1爲正確的軌道-1錯過了一個明顯的錯誤...修復,我會讓它回來;) – MadProgrammer

+0

Sunjay,謝謝你非常。我注意到你添加了幾行代碼。我需要閱讀這些內容,看看它們是什麼。我對此很新,所以例如你在** main **中添加的代碼,我必須學習。 感謝您的幫助和意見。 –

2

的問題是在你的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(); 
} 

在我的測試中, 「再試一次」 的預期

就我個人的工作,你可能在請求重點fahrenheitTB.selectAll之後還想要致電selectAll。這是個人的事情,但現在用戶不需要手動清除字段輸入下一個值...;)

+0

非常感謝你!我非常感謝你的幫助!它非常完美! –

+1

[不專注是非常異步的,特別是JTextComponents和來自複合JComponents的派生編輯器](http://stackoverflow.com/a/9956650/714968) – mKorbel

+0

@mKorbel Spinners特別有趣。雖然公平點 – MadProgrammer

相關問題