我应该写一个小程序,显示一个可以用滑块调节的温度计。我相信我有GUI,一切都想通了。在JGrasp(我通常使用的)它询问我是否要将其作为applet运行然后一切正常。对于我当前的课程,我的教师希望在NetBeans中完成所有工作并且我没有错误,但是当我运行applet时没有任何反应。非常感谢任何帮助!
package thermometer;
/**
* MTCH_HRTG
*/
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class Thermometer extends JApplet implements ChangeListener {
// Container
private Container contentPane;
// Panels
private JPanel sliderP;
private JPanel displayP;
private JPanel titleP;
//Slider
private JSlider slider;
//Labels
private JLabel title;
private JLabel tempL;
//Textfield
private JTextField temp;
public void init () {
contentPane = getContentPane();
contentPane.setLayout (new BorderLayout ());
// Setting the panels and objects
titleP = new JPanel ();
displayP = new JPanel ();
sliderP = new JPanel ();
title = new JLabel ("Adjust the slider to change the temperature");
titleP.add(title);
// Creates the thermometer
slider = new JSlider (JSlider.VERTICAL, -50, 120, 70);
slider.setMajorTickSpacing(10);
slider.setMinorTickSpacing(5);
slider.setPaintTicks(true);
slider.setPaintLabels(true);
slider.addChangeListener(this);
sliderP.add(slider);
temp = new JTextField ("70", 2);
temp.setEditable(false);
tempL = new JLabel ("Degrees in Fahrenheit");
displayP.add(temp);
displayP.add(tempL);
//Adding the panels to the content pane
contentPane.add(titleP, BorderLayout.NORTH);
contentPane.add(sliderP, BorderLayout.CENTER);
contentPane.add(displayP, BorderLayout.SOUTH);
}
public void stateChanged (ChangeEvent e) {
int temperature = slider.getValue();
temp.setText(Integer.toString(temperature));
}
public static void main(String[] args) {
}
}