help!!!
public void actionPerformed(ActionEvent e){
int m, n;
m = Integer.parseInt(num1.getText());
n = Integer.parseInt(num2.getText());
if(btn1 == e.getSource())
{
int k = m < n ? m : n;
for(; k > 0; k--)
if(m % k == 0 && n % k == 0)
{
result.setText("" + k);
break;
}
}
if(btn2 == e.getSource())
{
int k = m > n ? m : n;
for(; k <= m * n; k++)
if(k % m == 0 && k % n == 0)
{
result.setText("" + k);
break;
}
}
}
}
这个程序是利用swing组件求两个数的最大公约数和最小公倍数
在actionPerformed这个类出现了错误,怎么修改?
--------------------编程问答-------------------- 出现什么错? --------------------编程问答-------------------- actionPerformed这个类出错了,我不会写。这是完整的程序。import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class lianxi_1 extends JFrame implements ActionListener
{ private JTextField a=new JTextField(10);
private JTextField b=new JTextField(10);
private JLabel r=new JLabel("求最大公约数和最小公倍数");
private JLabel A=new JLabel("A:");
private JLabel B=new JLabel("B:");
private JLabel m=new JLabel();
private JLabel n=new JLabel();
private JLabel M=new JLabel("最大公约数:");
private JLabel N=new JLabel("最小公倍数:");
private JButton js=new JButton("计算");
public lianxi_1()
{ super("lianxi");
JPanel contentPane=(JPanel)getContentPane();
contentPane.setLayout(new BorderLayout());
JPanel p1=new JPanel();
JPanel p2=new JPanel(new GridLayout(0,2));
JPanel p3=new JPanel();
contentPane.setBorder(new EmptyBorder(10,10,10,10));
p1.add(r,BorderLayout.CENTER);
p2.add(A);
p2.add(a);
p2.add(B);
p2.add(b);
p2.add(M);
p2.add(m);
p2.add(N);
p2.add(n);
p3.add(js,BorderLayout.NORTH);
js.addActionListener(this);
contentPane.add(p1,BorderLayout.NORTH);
contentPane.add(p2,BorderLayout.CENTER);
contentPane.add(p3,BorderLayout.SOUTH);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent event)
{ String command=event.getActionCommand();
if(command.equals("计算"))
{
int a ,b,m;
int a1=Integer.parseInteger(a.getText().trim());
int b1=Integer.parseInteger(b.getText().trim());
Scanner s = new Scanner(System.in);
System.out.print( "键入一个整数: ");
a = s.nextInt();
System.out.print( "再键入一个整数: "); b = s.nextInt();
deff cd = new deff();
m = cd.deff(a,b);
int n = a * b / m;
m.setText(Integer.toString(getM()));
n.setText(Integer.toString(getN()));
System.out.println("最大公约数: " + m);
System.out.println("最小公倍数: " + n);
try{ m.setText(Integer.toString(getM()));
n.setText(Integer.toString(getN()));
}catch(Exception e){ }
}
}
class deff{
public int deff(int x, int y)
{
int t;
if(x < y)
{
t = x;
x = y;
y = t;
}
while(y != 0)
{
if(x == y) return x;
else {
int k = x % y;
x = y;
y = k;
}
}
return x;
}
}
public static void main(String arg[])
{
new lianxi_1();
}
} --------------------编程问答-------------------- 能把出错信息说一下么,不然大家不会一行一行看你代码,看哪出问题的,把出错信息写出来问题解决的快些 --------------------编程问答--------------------
这是我在eclipse中看你代码出现的问题,你的a和b都是int类型所以不能调用getText()方法
没有引入java.util.Scanner
没有getM()和getN()方法 --------------------编程问答-------------------- 这就算是错误,主要就是 actionPerformed 这个类写错了。
补充:Java , Java SE