快帮我改一下吧!!PLEASE!!
import javax.swing.*;import java.awt.event.ActionEvent;
import java.awt.*;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionListener;
public class NumTest1 extends JFrame implements ActionListener
{
private JTextField num1 = new JTextField(15);
private JTextField num2 = new JTextField(15);
private JTextField result = new JTextField(15);
private JLabel label_1 = new JLabel("Num1");
private JLabel label_2 = new JLabel("Num2");
private JLabel label_3 = new JLabel("结果");
private JButton btn1 = new JButton("最大公约数");
private JButton btn2 = new JButton("最小公倍数");
private JLabel r=new JLabel("求两个正整数的最大公约数和最小公倍数");
public NumTest1()
{
JPanel contentPane=(JPanel)getContentPane();
contentPane.setLayout(new BorderLayout());
JPanel p1=new JPanel();
JPanel p2=new JPanel(new GridLayout(0,3));
JPanel p3=new JPanel();
contentPane.setBorder(new EmptyBorder(15,15,15,15));
p1.add(r,BorderLayout.CENTER);
p2.add(NUM1);
p2.add(num1);
p2.add(NUM2);
p2.add(num2);
p2.add(RESULT);
p2.add(result);
p3.add(btn1);
p3.add(btn2);
btn1.addActionListener(this);
btn2.addActionListener(this);
contentPane.add(p1,BorderLayout.NORTH);
contentPane.add(p2,BorderLayout.CENTH);
contentPane.add(p3,BorderLayout.SOUTH);
pack();
setVisible(true);
}
public static void main(String[] args)
{
new NumTest1();
}
public void actionPerformed(ActionEvent e)
{
int m, n;
m = Integer.parseInt(num1.getText());
n = Integer.parseInt(num2.getText());
if(btn1 == e.getSource())
{
result.setText("" + f1(m,n));
}
if(btn2 == e.getSource())
{
result.setText("" + m * n / f1(m, n));
}
}
public int f1(int m , int n)
{
return f2(m>n?m:n, m<=n?m:n);
}
public int f2(int a, int b)
{
int c = a % b;
if(c == 0)
return b;
else
{
return f2(b, c);
}
}
} --------------------编程问答-------------------- 参考一下:
--------------------编程问答-------------------- 最近正好看了这一块的东西,帮忙整了一下。
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.*;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionListener;
public class NumTest1 extends JFrame implements ActionListener {
private JTextField num1 = new JTextField(15);
private JTextField num2 = new JTextField(15);
private JTextField result = new JTextField(15);
private JLabel label_1 = new JLabel("Num1");
private JLabel label_2 = new JLabel("Num2");
private JLabel label_3 = new JLabel("结果");
private JButton btn1 = new JButton("最大公约数");
private JButton btn2 = new JButton("最小公倍数");
private JLabel r = new JLabel("求两个正整数的最大公约数和最小公倍数");
public NumTest1() {
JPanel contentPane = (JPanel) getContentPane();
contentPane.setLayout(new BorderLayout());
JPanel p1 = new JPanel();
JPanel p20 = new JPanel();//新加
JPanel p21 = new JPanel();
JPanel p22 = new JPanel();
JPanel p2 = new JPanel(new GridLayout(0, 3));
JPanel p3 = new JPanel();
contentPane.setBorder(new EmptyBorder(15, 15, 15, 15));
p1.add(r, BorderLayout.CENTER);
p20.add(label_1);//NUM1,NUM2,RESULT没定义.
p20.add(num1);
p21.add(label_2);
p21.add(num2);
p22.add(label_3);
p22.add(result);
p2.add(p20);
p2.add(p21);
p2.add(p22);
p3.add(btn1);
p3.add(btn2);
btn1.addActionListener(this);
btn2.addActionListener(this);
contentPane.add(p1, BorderLayout.NORTH);
contentPane.add(p2, BorderLayout.CENTER);//CENTH --> CENTER
contentPane.add(p3, BorderLayout.SOUTH);
pack();
setVisible(true);
}
public static void main(String[] args) {
new NumTest1();
}
public void actionPerformed(ActionEvent e) {
int m, n;
m = Integer.parseInt(num1.getText());
n = Integer.parseInt(num2.getText());
if (btn1 == e.getSource()) {
result.setText("" + f1(m, n));
}
if (btn2 == e.getSource()) {
result.setText("" + m * n / f1(m, n));
}
}
public int f1(int m, int n) {
return f2(m > n ? m : n, m <= n ? m : n);
}
public int f2(int a, int b) {
int c = a % b;
if (c == 0)
return b;
else {
return f2(b, c);
}
}
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.border.EmptyBorder;
public class NumTest1
{
public static void main(String[] args)
{
NumFrame calcFrame = new NumFrame();
calcFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
calcFrame.setVisible(true);
}
}
class NumFrame extends JFrame implements ActionListener
{
public NumFrame()
{
setTitle("NumTest1");
//创建上中下三个面板,以及为了中间面板排版的三个辅助面板
JPanel panelN = new JPanel();
JPanel panelC = new JPanel(new GridLayout(0,3));
JPanel panelS = new JPanel();
JPanel panelC1 = new JPanel();
JPanel panelC2 = new JPanel();
JPanel panelC3 = new JPanel();
//创建标签和文本域和按钮
JLabel label_1 = new JLabel("Num1");
JLabel label_2 = new JLabel("Num2");
JLabel label_3 = new JLabel("结果");
JLabel r = new JLabel("求两个正整数的最大公约数和最小公倍数");
num1=new JTextField(15);
num2=new JTextField(15);
result=new JTextField(15);
btn1=new JButton("最大公约数");
btn2=new JButton("最小公倍数");
//将各组件按照需要的排版放到各面板中
panelN.add(r,BorderLayout.CENTER);
panelC1.add(label_1);
panelC1.add(num1);
panelC2.add(label_2);
panelC2.add(num2);
panelC3.add(label_3);
panelC3.add(result);
panelC.add(panelC1);
panelC.add(panelC2);
panelC.add(panelC3);
panelS.add(btn1);
panelS.add(btn2);
//将上中下面板放置到窗口内
JPanel contentPane=(JPanel)getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.setBorder(new EmptyBorder(15,15,15,15));
contentPane.add(panelN,BorderLayout.NORTH);
contentPane.add(panelC,BorderLayout.CENTER);
contentPane.add(panelS,BorderLayout.SOUTH);
//上面对界面的布局已经完成,现在要为两个按钮加上易做图即可
btn1.addActionListener(this);
btn2.addActionListener(this);
pack();
}
public void actionPerformed(ActionEvent e)
{
int m;
int n;
m=Integer.parseInt(num1.getText());
n=Integer.parseInt(num2.getText());
if (btn1 == e.getSource())
{
result.setText("" + f1(m, n));
}
if (btn2 == e.getSource())
{
result.setText("" + m * n / f1(m, n));
}
}
public int f1(int m,int n)
{
return f2(m>n?m:n, m<=n?m:n);
}
public int f2(int a,int b)
{
int c = a % b;
if (c == 0)
{
return b;
}
else
{
return f2(b, c);
}
}
private JTextField num1;
private JTextField num2;
private JTextField result;
private JButton btn1;
private JButton btn2;
}
运行结果按照你的程序应该是这样的
--------------------编程问答-------------------- 我这个程序是求两个正整数的最大公公约数和最小公倍数,现在我想加一个对话框,当我输入一个负数时,就出来这个对话框告诉我“输入错误,请重新输入”,然后点击确定回到NUMTEST1这个界面。我应该加什么代码呢??? --------------------编程问答-------------------- 楼主参考一下:
--------------------编程问答-------------------- 我又加入了一段代码,在关闭文本框的时候出来一个对话框说:您确定退出吗?点击是就退出,点击否就什么也不做,可我应该把代码加在哪里呢?
public void actionPerformed(ActionEvent e) {
int m = 0, n = 0;
boolean success = true;//数据转换成功标志.
/**
* 判断num1 是否可转成整数,及是否是负数
*/
try {
m = Integer.parseInt(num1.getText());
}
catch(Exception ex) {
ex.printStackTrace();
success = false;//未成功
}
if(!success) {
JOptionPane.showMessageDialog(null, "num1 数据格式错误!", "error", JOptionPane.ERROR_MESSAGE);
num1.requestFocus();
return;
}
else {
if(m < 0 ) {
JOptionPane.showMessageDialog(null, "num1 不能为负!", "error", JOptionPane.ERROR_MESSAGE);
num1.requestFocus();
return;
}
}
/**
* 判断num2 是否可转成整数,及是否是负数
*/
try {
n = Integer.parseInt(num2.getText());
}
catch( Exception ex) {
ex.printStackTrace();
success = false;
}
if(!success) {
JOptionPane.showMessageDialog(null, "num2 数据格式错误!", "error", JOptionPane.ERROR_MESSAGE);
num2.requestFocus();
return;
}
else {
if (n <0 ) {
JOptionPane.showMessageDialog(null, "num2不能为负!", "error", JOptionPane.ERROR_MESSAGE);
num2.requestFocus();
return;
}
}
if (btn1 == e.getSource()) {
result.setText("" + f1(m, n));
}
if (btn2 == e.getSource()) {
result.setText("" + m * n / f1(m, n));
}
}
public void windowClosed(WindowEvent e)
{ }
public void windowClosing(WindowEvent e)
{ int result=JOptionPane.showConfirmDialog(this,"确实要退出吗","系统消息",JOptionPane.YES_NO_OPTION);
if(result==JOptionPane.YES_OPTION)
{ this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} --------------------编程问答--------------------
--------------------编程问答-------------------- 参考:http://bbs.csdn.net/topics/80453552 --------------------编程问答-------------------- if (result == JOptionPane.YES_OPTION) {
public NumTest1() {
JPanel contentPane = (JPanel) getContentPane();
contentPane.setLayout(new BorderLayout());
JPanel p1 = new JPanel();
JPanel p20 = new JPanel();// 新加
JPanel p21 = new JPanel();
JPanel p22 = new JPanel();
JPanel p2 = new JPanel(new GridLayout(0, 3));
JPanel p3 = new JPanel();
contentPane.setBorder(new EmptyBorder(15, 15, 15, 15));
p1.add(r, BorderLayout.CENTER);
p20.add(label_1);// NUM1,NUM2,RESULT没定义.
p20.add(num1);
p21.add(label_2);
p21.add(num2);
p22.add(label_3);
p22.add(result);
p2.add(p20);
p2.add(p21);
p2.add(p22);
p3.add(btn1);
p3.add(btn2);
btn1.addActionListener(this);
btn2.addActionListener(this);
contentPane.add(p1, BorderLayout.NORTH);
contentPane.add(p2, BorderLayout.CENTER);// CENTH --> CENTER
contentPane.add(p3, BorderLayout.SOUTH);
// ------------------------------加在这
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
int result = JOptionPane.showConfirmDialog(null, "确实要退出吗",//null 别用this.
"系统消息", JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
});
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);// 注意这里是 DO_NOTHING_ON_CLOSE
// --------------------------------
pack();
setVisible(true);
}
System.exit(0);
}
}
});这里缺少“}”,但这里的{}都是完整的??
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);这条语句也有错??
补充:Java , Java相关