package sample;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class jueduizhi extends JFrame{
private JTextField a,b;
jueduizhi(){
setTitle("求绝对值");
setBounds(200,300,200,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
a =new JTextField(10);
b =new JTextField(10);
a.setEditable(true);
b.setEditable(false);
a.addFocusListener(new FocusAdapter(){
public void focusLost(FocusEvent e){
int s=Integer.parseInt(a.getText());
Math.abs(s);
b.setText(String.valueOf(s));
}
});
add(a);
add(b);
}
}
public class Jueduizhi {
public static void main(String[] args) {
// TODO Auto-generated method stub
jueduizhi frm=new jueduizhi();
frm.setVisible(true);
}
}
就是这段求绝对值的函数。。。为什么无法加载主类
------解决思路----------------------
你得b不是editable设置为false么,那你还给它setText()能成么。jueduizhi构造函数变为public
------解决思路----------------------
Math.abs(s);改为 s = Math.abs(s);