import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.Container.*;
public class WeightConApplication extends JFrame
{
private JFrame mainFrame;
private JButton exitButton;
private JTextField inputField;
private JTextField messageField;
private JLabel inputLabel;
public WeightConApplication()
{
mainFrame=new JFrame("WeightConApplication");
ConButton=new JButton("Convert");
inputLabel=new JLabel("Input weight");
inputField=new JTextField(20);
Container c=mainFrame.getContentPane();
c.setLayout(new FlowLayout());
c.add(inputLabel);
c.add(inputField);
c.add(ConButton);
c.add(messageField);
mainFrame.setSize(250,125);
}
}
public static void main(String args[])
{
new WeightConApplication();
}
编译后显示:Z:\WeightConApplication.java:34: 'class' or 'interface' expected
public static void main(String args[])
^
1 error
请问这是什么意思啊?路过的大牛们请教,:-)
[此贴子已经被作者于2007-5-13 21:57:39编辑过]
----------------解决方案--------------------------------------------------------
public static void main(String args[])
{
new WeightConApplication();
}
要写在public class WeightConApplication extends JFrame内部
还有ConButton没定义
----------------解决方案--------------------------------------------------------
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.Container.*;
public class WeightConApplication extends JFrame implements TextListener
{
private JFrame mainFrame;
private JButton ConButton;
private JTextField inputField;
private JTextField messageField;
private JLabel inputLabel;
public WeightConApplication()
{
mainFrame=new JFrame("WeightConApplication");
ConButton=new JButton("Convert");
inputLabel=new JLabel("Input weight");
inputField=new JTextField(20);
Container c=mainFrame.getContentPane();
c.setLayout(new FlowLayout());
c.add(inputLabel);
c.add(inputField);
c.add(ConButton);
c.add(messageField);
mainFrame.setSize(250,125);
}
public static void main(String args[])
{
new WeightConApplication();
}
}
Z:\WeightConApplication.java:6: WeightConApplication is not abstract and does not override abstract method textValueChanged(java.awt.event.TextEvent) in java.awt.event.TextListener
public class WeightConApplication extends JFrame implements TextListener
^
1 error
我又不明白了,java真难学啊- -||
----------------解决方案--------------------------------------------------------
实现TextListener应该还有一个
对应的方法吧
----------------解决方案--------------------------------------------------------
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.Container.*;
public class WeightConApplication extends JFrame //implements TextListener
{
private JFrame mainFrame;
private JButton ConButton;
private JTextField inputField;
private JTextField messageField;
private JLabel inputLabel;
public WeightConApplication()
{
mainFrame=new JFrame("WeightConApplication");
ConButton=new JButton("Convert");
inputLabel=new JLabel("Input weight");
inputField=new JTextField(20);
Container c=mainFrame.getContentPane();
c.setLayout(new FlowLayout());
c.add(inputLabel);
c.add(inputField);
c.add(ConButton);
c.add(messageField);
mainFrame.setSize(250,125);
}
public static void main(String args[])
{
new WeightConApplication();
}
}
这样可以编译,不过在3楼的问题我还是不知道是什么意思。
----------------解决方案--------------------------------------------------------
其实这道问题是这样的:
编写java独立应用程序,接收人在地球上体重,输出他在月球上的体重。
(1) 月球上人的体重是在地球上体重的1/6。
(2) 定义可实例化类 ,以实现人在地球上的体重与在月球上体重的转换。
(3) 使用图形用户界面组件标签、文本框、按钮。
(4) 处理输入错误的情况。
我的代码如下:
import java.awt.event.*;
import java.awt.*;
public class WeightConApplication extends Frame implements ActionListener,WindowListener{
public Frame mainFrame;
public Button ConButton;
public TextField inputField;
public TextField messageField;
public Label inputLabel;
WeightConApplication()
{
setLayout(new FlowLayout());
inputField=new TextField(20);
inputLabel=new Label("Input weight");
ConButton=new Button("Convert");
add(ConButton);
add(inputField);
add(messageField);
add(inputLabel);
ConButton.addActionListener(this);
setBounds(100,100,150,150);
setVisible(true);
//mainFrame.setSize(250,125);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
validate();
}
public static void main(String args[])
{
System.out.println("Starting WeightConApplication...");
WeightConApplication mainFrame = new WeightConApplication();
mainFrame.setSize(400, 400);
mainFrame.setTitle("WeightConApplication");
mainFrame.setVisible(true);
}
public void actionPerformed(ActionEvent exception)
{
try{
if(exception.getSource()==ConButton){
messageField.setText(Double.parseDouble(inputField.getText())/6);
}
}
catch(NumberFormatException e)
{
messageField.setText("非法字符");}
}
}
编译后有两个错误:
Z:\WeightConApplication.java:4: WeightConApplication is not abstract and does not override abstract method windowDeactivated(java.awt.event.WindowEvent) in java.awt.event.WindowListener
public class WeightConApplication extends Frame implements ActionListener,WindowListener{
^
Z:\WeightConApplication.java:55: cannot find symbol
symbol : method setText(double)
location: class java.awt.TextField
messageField.setText(Double.parseDouble(inputField.getText())/6);
^
2 errors
热心的斑斑或者路过的大牛们能够指点指点,我实在是没办法。java书上的例子很容易看懂,一到自己动手了就开始不行了。
----------------解决方案--------------------------------------------------------
1.WindowListener接口的抽象方法需要实现
2.messageField.setText(String );参数是string型!
----------------解决方案--------------------------------------------------------
import java.awt.event.*;
import java.awt.*;
public class WeightConApplication extends Frame implements ActionListener{
public static void main(String args[])
{
System.out.println("Starting WeightConApplication...");
new WeightConApplication();
WeightConApplication mainFrame = new WeightConApplication();
mainFrame.setSize(400, 400);
mainFrame.setTitle("WeightConApplication");
mainFrame.setVisible(true);
}
public Button ConButton;
public TextArea inputField;
public TextArea messageField;
public Label inputLabel;
WeightConApplication()
{
setLayout(new FlowLayout());
inputField=new TextArea(6,15);
inputLabel=new Label("Input weight");
ConButton=new Button("Convert");
inputField=new TextArea(6,15);
messageField=new TextArea(6,15);
add(ConButton);
add(inputField);
add(messageField);
add(inputLabel);
setBounds(100,100,300,300);
setVisible(true);
ConButton.addActionListener(this);
setBounds(100,100,150,150);
validate();
}
public void actionPerformed(ActionEvent e)
{
messageField.append(Double.valueOf(inputField.getText())/6); //这里出了问题?
}
}
Z:\WeightConApplication.java:44: append(java.lang.String) in java.awt.TextArea cannot be applied to (double)
messageField.append(Double.valueOf(inputField.getText())/6);
^
1 error
说是不能转化成double型?
[此贴子已经被作者于2007-5-13 21:56:36编辑过]
----------------解决方案--------------------------------------------------------
public void actionPerformed(ActionEvent e)
{
Double m=Double.valueOf(inputField.getText())/6;
String n=m.toString();
messageField.append(n); //这里出了问题?
}
JField.append(String a),将其计算出的Double数据转换成字符串?
建议:异常要处理,加入关闭JFrame的代码setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
----------------解决方案--------------------------------------------------------
和JField有什么关系,呵呵我没用swing组件啊。嗯,知道问题出现哪里了,谢谢大家!谢谢斑斑!
[此贴子已经被作者于2007-5-14 19:00:31编辑过]
----------------解决方案--------------------------------------------------------