import java.applet.AudioClip;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JApplet;import org.omg.CORBA.PRIVATE_MEMBER;
 
public class TemperatureFrame extends JFrame implements ActionListener {
 
    private JButton transformFButton = new JButton("转化成摄氏");
    private JButton transformCButton = new JButton("转化成华氏");
    private JButton stopButton = new JButton("暂停");
    private JButton startButton = new JButton("开始");
    private JTextField fTextField = new JTextField();
    private JTextField cTextField = new JTextField();
    private JTextField sTextField = new JTextField();
    private JLabel transformJLabel = new JLabel("转换结果为:");
    private JLabel transformJLabel1 = new JLabel("请输入摄氏度");
    private JLabel transformJLabel2 = new JLabel("请输入华氏度");
    private JLabel I1 = new JLabel("first");
    private Panel ImgePanel = new Panel();
    private AudioClip audio;
    //测试
    ImageIcon first = new ImageIcon("春");
    ImageIcon second = new ImageIcon("冬");
    float c, f,s,p;
 
    public TemperatureFrame() {
        super("华氏温度摄氏温度转换");
        try {
         mianban();
            show(c);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    public  void mianban () {
     first.setImage(first.getImage().getScaledInstance(WIDTH,HEIGHT,Image.SCALE_DEFAULT));//设置图片大小
        fTextField.setBounds(200, 30, 100, 25);
        cTextField.setBounds(300, 30, 100, 25);
        sTextField.setBounds(400, 30, 100, 50);
        transformCButton.setBounds(200, 54, 100, 25);
        transformFButton.setBounds(300, 54, 100, 25);
        transformJLabel.setBounds(420, 10, 100, 25);
        transformJLabel1.setBounds(210, 10, 100, 25);
        transformJLabel2.setBounds(310, 10, 100, 25);
        stopButton.setBounds(700, 35, 70, 50);
        startButton.setBounds(780, 35, 70, 50);
        transformCButton.addActionListener(this);
        transformFButton.addActionListener(this);
        //transformJLabel.addActionListener(this);
        stopButton.addActionListener(this);
        startButton.addActionListener(this);
        Container c = getContentPane(); 
        c.add(fTextField);
        c.add(cTextField);
        c.add(sTextField);
        c.add(transformCButton);
        c.add(transformFButton);
        c.add(transformJLabel);
        c.add(transformJLabel1);
        c.add(transformJLabel2);
        c.add(stopButton);
        c.add(I1);
        c.add(startButton);
        c.setLayout(null);
        c.add(ImgePanel, BorderLayout.CENTER);// 放置四季图片出现的地方的布局
        
        this.setSize(1080, 720);
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);     
    }
        
       // music = getAudioClip(getCodeBase(), "Faded.wav");// 得到背景音乐
       // music.loop();


/*
        try{
            audio=getAudioClip(getDocumentBase(),"Faded.wav");
           }catch(Exception e){}
            */

    public void actionPerformed(ActionEvent e) {
        //华氏温度F与摄氏度C转换的公式
        //F= C* 9/5 + 32
        //C=(F-32)*5/9
        if (e.getSource() == transformFButton) {
 
            try {
                c = Float.parseFloat(cTextField.getText());
                show(c);
                f = c * 9 / 5 + 32;
                show(c);
                sTextField.setText(String.valueOf(f)+ "C");
                
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        
        if (e.getSource() == transformCButton) {
            try {
            
             //show(r);
                f = Float.parseFloat(fTextField.getText());
                c = (f - 32) * 5 / 9;
                show(c);
                sTextField.setText(String.valueOf(c)+ "F");
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        
       
    }
   
    
    public  void show(double c)// 是画面板的图片
{
    
     Graphics g = ImgePanel.getGraphics();
if (c < 10) {
System.out.println("测试");
System.out.println(c);
ImageIcon im = new ImageIcon("冬.jpg");// 得到一个图片(冬天)

Image image = im.getImage();
g.drawImage(image, 0, 0, 800,700,this);
}
if (10 <= c && c < 22) {
            
ImageIcon im = new ImageIcon("春.jpg");// 得到一个图片(春天)
Image image = im.getImage();
g.drawImage(image, 0, 0,this);
}
if (22 <= c && c < 30) {
ImageIcon im = new ImageIcon("秋.jpg");// 得到一个图片(秋天)
Image image = im.getImage();
g.drawImage(image, 0, 0,this) ;
}
if (30 <= c) {
ImageIcon im = new ImageIcon("夏.jpg");// 得到一个图片(夏天)
Image image = im.getImage();
g.drawImage(image, 0, 0,,this);
}
}
    
    
    public static void main(String[] args) {
        new TemperatureFrame();
    }
 }

解决方案 »

  1.   

    看了一下,你的TextField和Button的名字对应的有问题,看起来比较乱,我改了一下代码顺序,还有就是Button绑定的Action逻辑写错了,改的都在程序里注释了。对于显示图片不是特别了解,简单写了一个不知道符合你的要求不,修改图片位置大小也在程序里注释了。package jframe;import java.applet.AudioClip;
    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.Panel;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.net.URL;import javax.swing.Icon;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;public class TemperatureFrame extends JFrame implements ActionListener { private JLabel transformJLabel1 = new JLabel("请输入摄氏度");
    private JLabel transformJLabel2 = new JLabel("请输入华氏度");
    private JLabel transformJLabel = new JLabel("转换结果为:"); private JTextField cTextField = new JTextField();
    private JTextField fTextField = new JTextField();
    private JTextField sTextField = new JTextField(); private JButton transformCButton = new JButton("转化成华氏");
    private JButton transformFButton = new JButton("转化成摄氏"); private JButton stopButton = new JButton("暂停");
    private JButton startButton = new JButton("开始");

    private URL url = null;
    private JLabel I1 = new JLabel();
    private Panel ImgePanel = new Panel();
    private AudioClip audio;
    // 测试
    // ImageIcon first = new ImageIcon("春");
    // ImageIcon second = new ImageIcon("冬");
    float c, f, s, p; public TemperatureFrame() {
    super("华氏温度摄氏温度转换");
    try {
    mianban();
    // show(c);
    } catch (Exception e) {
    e.printStackTrace();
    }
    } public void mianban() {
    // first.setImage(first.getImage().getScaledInstance(WIDTH, HEIGHT, Image.SCALE_DEFAULT));// 设置图片大小 url = TemperatureFrame.class.getResource("1.jpg");
    Icon icon = new ImageIcon(url);
    I1.setIcon(icon);
    I1.setBounds(500, 200, 300, 300); //在这里修改图片位置和大小
    /*
     * 修改:将cTextField和fTextField的位置交换
     * cTextField对应CButton,fTextfield对应FButton
     */
    cTextField.setBounds(200, 30, 100, 25);
    fTextField.setBounds(300, 30, 100, 25);
    sTextField.setBounds(400, 30, 100, 50); transformCButton.setBounds(200, 54, 100, 25);
    transformFButton.setBounds(300, 54, 100, 25); transformJLabel1.setBounds(210, 10, 100, 25);
    transformJLabel2.setBounds(310, 10, 100, 25);
    transformJLabel.setBounds(400, 10, 100, 25); stopButton.setBounds(700, 35, 70, 50);
    startButton.setBounds(780, 35, 70, 50);
    transformCButton.addActionListener(this);
    transformFButton.addActionListener(this);
    // transformJLabel.addActionListener(this);
    stopButton.addActionListener(this);
    startButton.addActionListener(this);
    Container c = getContentPane();
    c.add(fTextField);
    c.add(cTextField);
    c.add(sTextField);
    c.add(transformCButton);
    c.add(transformFButton);
    c.add(transformJLabel);
    c.add(transformJLabel1);
    c.add(transformJLabel2);
    c.add(stopButton);
    c.add(I1);
    c.add(startButton);
    c.setLayout(null);
    c.add(ImgePanel, BorderLayout.CENTER);// 放置四季图片出现的地方的布局 this.setSize(1080, 720);
    this.setResizable(false);
    this.setLocationRelativeTo(null);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);
    } // music = getAudioClip(getCodeBase(), "Faded.wav");// 得到背景音乐
    // music.loop(); /*
     * try{ audio=getAudioClip(getDocumentBase(),"Faded.wav"); }catch(Exception
     * e){}
     */ public void actionPerformed(ActionEvent e) {
    // 华氏温度F与摄氏度C转换的公式
    // F= C* 9/5 + 32
    // C=(F-32)*5/9
    if (e.getSource() == transformFButton) { // 如果是FButton,那应该输入华氏温度,转化成摄氏温度。逻辑错误 try {
    // c = Float.parseFloat(cTextField.getText());
    // 所以应该获取fText的华氏温度
    f = Float.parseFloat(fTextField.getText());
    // f = c * 9 / 5 + 32; //公式错了
    c = (f - 32) * 5 / 9;
    show(c);
    // sTextField.setText(String.valueOf(f)+ "C");
    sTextField.setText(String.valueOf(c) + "C"); // 应该显示c摄氏温度 } catch (Exception ex) {
    ex.printStackTrace();
    }
    } if (e.getSource() == transformCButton) { // 同理和上边的相反,不加注释了
    try {
    // show(r);
    c = Float.parseFloat(cTextField.getText());
    f = c * 9 / 5 + 32;
    show(c);
    sTextField.setText(String.valueOf(f) + "F");
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    }
    }// public void show(double c)// 是画面板的图片
    // {
    //
    // Graphics g = ImgePanel.getGraphics();
    // if (c < 10) {
    // System.out.println("测试");
    // System.out.println(c);
    // ImageIcon im = new ImageIcon("冬.jpg");// 得到一个图片(冬天)
    //
    // Image image = im.getImage();
    // g.drawImage(image, 0, 0, 800, 700, this);
    // }
    // if (10 <= c && c < 22) {
    //
    // ImageIcon im = new ImageIcon("春.jpg");// 得到一个图片(春天)
    // Image image = im.getImage();
    // g.drawImage(image, 0, 0, this);
    // }
    // if (22 <= c && c < 30) {
    // ImageIcon im = new ImageIcon("秋.jpg");// 得到一个图片(秋天)
    // Image image = im.getImage();
    // g.drawImage(image, 0, 0, this);
    // }
    // if (30 <= c) {
    // ImageIcon im = new ImageIcon("夏.jpg");// 得到一个图片(夏天)
    // Image image = im.getImage();
    // g.drawImage(image, 0, 0, this);
    // }
    // }

    public void show(double c)// 是画面板的图片
    {

    if (c < 10) {
    url = TemperatureFrame.class.getResource("1.jpg");
    }else if (10 <= c && c < 22) {
    url = TemperatureFrame.class.getResource("2.jpg");
    }else if (22 <= c && c < 30) {
    url = TemperatureFrame.class.getResource("3.jpg");
    }else if (30 <= c) {
    url = TemperatureFrame.class.getResource("4.jpg");
    }
    Icon icon = new ImageIcon(url);
    I1.setIcon(icon);
    } public static void main(String[] args) {
    new TemperatureFrame();
    }}