java用swing的一个小程序 不知道哪错了
packageguess;importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassM...
package guess;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
public class FahrenheitPanel extends JPanel
{
private JLabel inputLabel, outputLabel, resultLabel;
private JTextField fahrenheit;
public FahrenheitPanel()
{
inputLabel = new JLabel (
"Enter Fahrenheit temperature:");
outputLabel = new JLabel (
"Temperature in Celsius: ");
resultLabel = new JLabel ("---");
fahrenheit = new JTextField (5);
add (inputLabel);
add (fahrenheit);
add (outputLabel);
add (resultLabel);
setPreferredSize (new Dimension(300, 75));
setBackground (Color.yellow);
}
fahrenheit.addActionListener (new TempListener());
private class TempListener implements ActionListener{
public void actionPerformed (ActionEvent event)
{
int fahrenheitTemp, celsiusTemp;
String text = fahrenheit.getText();
fahrenheitTemp = Integer.parseInt (text);
celsiusTemp = (fahrenheitTemp-32) * 5/9;
resultLabel.setText (
Integer.toString (celsiusTemp));
}
}
}
} 展开
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
public class FahrenheitPanel extends JPanel
{
private JLabel inputLabel, outputLabel, resultLabel;
private JTextField fahrenheit;
public FahrenheitPanel()
{
inputLabel = new JLabel (
"Enter Fahrenheit temperature:");
outputLabel = new JLabel (
"Temperature in Celsius: ");
resultLabel = new JLabel ("---");
fahrenheit = new JTextField (5);
add (inputLabel);
add (fahrenheit);
add (outputLabel);
add (resultLabel);
setPreferredSize (new Dimension(300, 75));
setBackground (Color.yellow);
}
fahrenheit.addActionListener (new TempListener());
private class TempListener implements ActionListener{
public void actionPerformed (ActionEvent event)
{
int fahrenheitTemp, celsiusTemp;
String text = fahrenheit.getText();
fahrenheitTemp = Integer.parseInt (text);
celsiusTemp = (fahrenheitTemp-32) * 5/9;
resultLabel.setText (
Integer.toString (celsiusTemp));
}
}
}
} 展开
展开全部
你的程序我帮你改好了,你看看吧。(改动的地方见注释)
package guess;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
JFrame jf=new JFrame();//这里加一句
FahrenheitPanel fp=new FahrenheitPanel(); //这里加一句
jf.add(fp);//这里加一句
jf.setSize(300, 75);//这里加一句
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//这里加一句
jf.setVisible(true);//这里加一句
}
}
class FahrenheitPanel extends JPanel//这里把类移出main函数并且去掉public
{
private JLabel inputLabel, outputLabel, resultLabel;
private JTextField fahrenheit;
public FahrenheitPanel()
{
inputLabel = new JLabel (
"Enter Fahrenheit temperature:");
outputLabel = new JLabel (
"Temperature in Celsius: ");
resultLabel = new JLabel ("---");
fahrenheit = new JTextField (5);
add (inputLabel);
add (fahrenheit);
add (outputLabel);
add (resultLabel);
setPreferredSize (new Dimension(300, 75));
setBackground (Color.yellow);
fahrenheit.addActionListener(new TempListener());//把这句移到FahrenheitPanel()函数里面
}
private class TempListener implements ActionListener{
public void actionPerformed (ActionEvent event)
{
int fahrenheitTemp, celsiusTemp;
String text = fahrenheit.getText();
fahrenheitTemp = Integer.parseInt (text);
celsiusTemp = (fahrenheitTemp-32) * 5/9;
resultLabel.setText (
Integer.toString (celsiusTemp));
}
}
}
package guess;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
JFrame jf=new JFrame();//这里加一句
FahrenheitPanel fp=new FahrenheitPanel(); //这里加一句
jf.add(fp);//这里加一句
jf.setSize(300, 75);//这里加一句
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//这里加一句
jf.setVisible(true);//这里加一句
}
}
class FahrenheitPanel extends JPanel//这里把类移出main函数并且去掉public
{
private JLabel inputLabel, outputLabel, resultLabel;
private JTextField fahrenheit;
public FahrenheitPanel()
{
inputLabel = new JLabel (
"Enter Fahrenheit temperature:");
outputLabel = new JLabel (
"Temperature in Celsius: ");
resultLabel = new JLabel ("---");
fahrenheit = new JTextField (5);
add (inputLabel);
add (fahrenheit);
add (outputLabel);
add (resultLabel);
setPreferredSize (new Dimension(300, 75));
setBackground (Color.yellow);
fahrenheit.addActionListener(new TempListener());//把这句移到FahrenheitPanel()函数里面
}
private class TempListener implements ActionListener{
public void actionPerformed (ActionEvent event)
{
int fahrenheitTemp, celsiusTemp;
String text = fahrenheit.getText();
fahrenheitTemp = Integer.parseInt (text);
celsiusTemp = (fahrenheitTemp-32) * 5/9;
resultLabel.setText (
Integer.toString (celsiusTemp));
}
}
}
展开全部
不能在主函数里面定义类
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你的程序写的很乱啊,好像是写的时候定义的类的位置不对吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询