asked in 電腦與網際網路程式設計 · 1 decade ago

問java程式

老師的程式執行一直有不明錯誤

請大大幫忙看一下那邊有錯

課本的題目:

http://homepage13.seed.net.tw/web@3/keroro/school/...

來源:

自己的網頁空間

程式:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

public class Ex9_2 extends JFrame implements ChangeListener, ActionListener {

private JSlider slider;

private JTextField textField;

private JButton button;

private int max = 0;

public static void main(String[] args) {

Ex9_2 demo = new Ex9_2();

demo.setSize(200,300);

demo.createGUI();

demo.setVisible(true);

}

private void createGUI() {

setDefaultCloseOperation(EXIT_ON_CLOSE);

Container window = getContentPane();

window.setLayout(new FlowLayout());

slider = new JSlider(JSlider.VERTICAL, 0, 100, 0);

slider.setMajorTickSpacing(10);

slider.setPaintTicks(true);

slider.addChangeListener(this);

window.add(slider);

textField = new JTextField(12);

window.add(textField);

button = new JButton("reset");

button.addActionListener(this);

window.add(button);

}

public void stateChanged(ChangeEvent e) {

int tem;

tem = slider.getValue();

if (tem > temp.getHValue()) {

temp.setHVal(tem);

}

display();

}

public void actionPerformed(ActionEvent event) {

textField.setText("");

temp.setHVal(0);

}

private void display() {

textField.setText("maximum value is " + temp.getHValue());

}

}

Update:

To Rei

問題還是一樣耶

一直是 Temp沒有

1 Answer

Rating
  • Cola
    Lv 5
    1 decade ago
    Best Answer

    你的程式没有定義類別物件~temp

    因字數有限~,有些不用更改的就省略了~

    程式變更如下:

    public class Ex9_2 extends JFrame implements ChangeListener, ActionListener {

    ...

    ...

    private JTextField htextField;//顯示最高溫度

    private JTextField ltextField;//顯示最低溫度

    private JTextField ctextField;//顯示目前溫度

    tempObj temp=new tempObj();//建立tempObj物件

    public static void main(String[] args) {

    ...

    demo.setSize(200,350);

    demo.setLocation((1024-200)/2,(768-350)/2);//視窗置中

    ...

    }

    private void createGUI() {

    ....

    ....

    window.add(slider);

    ctextField = new JTextField(12);

    window.add(ctextField);

    htextField = new JTextField(12);

    window.add(htextField);

    ltextField = new JTextField(12);

    window.add(ltextField);

    ...

    window.add(button);

    temp.init(20); //初始化temp

    slider.setValue(temp.getcurrentVal()); //設slider起始值

    }

    public void stateChanged(ChangeEvent e) {

    temp.setNewValue(slider.getValue());

    display();

    }

    public void actionPerformed(ActionEvent event) {

    slider.setValue(20);

    temp.init(slider.getValue());

    display();

    }

    private void display() {

    ctextField.setText("current value is " temp.getcurrentVal());

    htextField.setText("maximum value is " temp.getHighestVal());

    ltextField.setText("minimal value is " temp.getLowestVal());

    }

    class tempObj {

    private int highestTemp;

    private int lowestTemp;

    private int currentTemp;

    public tempObj(){

    init(0);

    }

    public void init(int val){

    this.currentTemp=val;

    this.highestTemp=val;

    this.lowestTemp=val;

    }

    public void setNewValue(int val){

    setcurrentVal(val);

    if (val < lowestTemp){

    lowestTemp=val;

    }else if (val > highestTemp){

    highestTemp=val;

    }

    }

    public void setHighestVal(int val){

    this.highestTemp=val;

    }

    public int getHighestVal(){

    return this.highestTemp;

    }

    public void setLowestVal(int val){

    this.lowestTemp=val;

    }

    public int getLowestVal(){

    return this.lowestTemp;

    }

    public void setcurrentVal(int val){

    this.currentTemp=val;

    }

    public int getcurrentVal(){

    return this.currentTemp;

    }

    }

    }

    2007-12-25 19:21:44 補充:

    雷大大~字數超過限制了~麻煩到這裡下~有完整程式碼

    http://www.badongo.com/file/7032127

Still have questions? Get your answers by asking now.