gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 08:16:17 25 4
gpt4 key购买 nike

所以我只是把作业的最后细节放进去,我注意到了 getCursor();当我运行我的程序时有效,但稍后在程序中(在两个actionListeners中)它不会转向华氏度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);
}
}

最佳答案

阅读本教程是一个好的开始:"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);

}
});
}
}

关于java - getCursor() 在我的 actionListener 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13151864/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com