可以帮我看看有什么问题吗?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Event1 extends JFrame
{
JButton but1,but2;
JTextField txt;
public Event1()
{super( "阶乘计算的窗口 ");
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(0);}});
initialize();
}
public void initialize()
{
setside(300,200);
(new handle()).setLayout(null);
but1=new JButton( "确定 ");
but1.setBounds(10,40,70,25);
but1.addActionListener(new handle());
add(but1);
but2=new JButton( "重置 ");
but2.setBounds(105,40,70,25);
but2.addActionListener(new handle());
add(but2);
txt=new JTextField();
txt.setBounds(75,70,70,25);
add(txt);
show();
}
public class handle implements ActionListener{
public void actionPerformed(ActionEvent e)
{int dialogtype=JOptionpane.PLAIN_MESSAGE;//对话框的类型;
if(e.getSource()==but1)
{
int i, m,sum=1;
float s=txt.getText();
m=(s%1==0&&s> 0)?1:2;
switch(m)
{
case 1:for(i=1;i <=s;i++)
sum=sum*i;
txt.setText(sum);break;
case 2:
dialogtype=JOptionPane.ERROR_MESSAGE;
JOptionPane.showMessageDialog(null, "请输入正整数! ",dialogtype);break;
}
}
else
{new Event1();
}
}
}
public static void main(String args[])
{new Event1();
}
}
[解决办法]
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Event1 extends JFrame
{
JButton but1,but2;
JTextField txt;
public Event1()
{super( "阶乘计算的窗口 ");
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(0);}});
initialize();
}
public void initialize()
{
setSize(300,200);
//(new handle()).setLayout(null);
but1=new JButton( "确定 ");
but1.setBounds(10,40,70,25);
but1.addActionListener(new handle());
add(but1);
but2=new JButton( "重置 ");
but2.setBounds(105,40,70,25);
but2.addActionListener(new handle());
add(but2);
txt=new JTextField();
txt.setBounds(75,70,70,25);
add(txt);
show();
}
class handle implements ActionListener{
public void actionPerformed(ActionEvent e)
{int dialogtype=JOptionPane.PLAIN_MESSAGE;//对话框的类型;
if(e.getSource()==but1)
{
int i, m,sum=1;
float s=Float.parseFloat(txt.getText());
m=(s%1==0&&s> 0)?1:2;
switch(m)
{
case 1:for(i=1;i <=s;i++)
sum=sum*i;
txt.setText(sum+ " ");break;
case 2:
dialogtype=JOptionPane.ERROR_MESSAGE;
JOptionPane.showMessageDialog(null, "请输入正整数! ", " ",dialogtype);break;
}
}
else
{new Event1();
}
}
}
public static void main(String args[])
{new Event1();
}
}
没试.不知道还有没有错误
[解决办法]
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane.*;import javax.swing.*;
class Event1 extends JFrame implements ActionListener
{
JButton but1,but2; JTextField txt;
Event1(String s)
{
super(s);
but1=new JButton( "确定 ");
but1.setBounds(10,40,70,25);
but1.addActionListener(this);
add(but1,BorderLayout.NORTH);
but2=new JButton( "重置 ");
but2.setBounds(105,40,70,25);
but2.addActionListener(this);
add(but2,BorderLayout.SOUTH);
txt=new JTextField();
add(txt,BorderLayout.CENTER);
but1.addActionListener(this);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{System.exit(0);}});
setVisible(true);
validate();
setSize(300,200);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==but1)
{
int i, m,sum=1;
float s=Float.valueOf(txt.getText()).floatValue();
m=(s%1==0&&s> 0)?1:2;
switch(m)
{
case 1:for(i=1;i <=s;i++)
sum=sum*i;
txt.setText( " "+sum);break;
case 2:
JOptionPane.showMessageDialog(null, "请输入正整数! ", "警告对话框 ",JOptionPane.WARNING_MESSAGE);break;
}
}
else
{ new Event1( "阶乘计算器 ");
}
}
}
public class Example
{ public static void main(String args[])
{
new Event1( "阶乘计算器 ");
}
}
你的错误太多了,简单修改了下,多把基础的东西专研下吧