问题描述
importjavax.swing.*;importjava.awt.event.ActionEvent;importjava.awt.*;importjavax.swing.border.EmptyBorder;importjava.awt.event.ActionListener;publicclassNumTest1extendsJFrameimplementsActionListener{privateJTextFieldnum1=newJTextField(15);privateJTextFieldnum2=newJTextField(15);privateJTextFieldresult=newJTextField(15);privateJLabellabel_1=newJLabel("Num1");privateJLabellabel_2=newJLabel("Num2");privateJLabellabel_3=newJLabel("结果");privateJButtonbtn1=newJButton("最大公约数");privateJButtonbtn2=newJButton("最小公倍数");privateJLabelr=newJLabel("求两个正整数的最大公约数和最小公倍数");publicNumTest1(){JPanelcontentPane=(JPanel)getContentPane();contentPane.setLayout(newBorderLayout());JPanelp1=newJPanel();JPanelp2=newJPanel(newGridLayout(0,3));JPanelp3=newJPanel();contentPane.setBorder(newEmptyBorder(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);}publicstaticvoidmain(String[]args){newNumTest1();}publicvoidactionPerformed(ActionEvente){intm,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));}}publicintf1(intm,intn){returnf2(m>n?m:n,m<=n?m:n);}publicintf2(inta,intb){intc=a%b;if(c==0)returnb;else{returnf2(b,c);}}}
解决方案
解决方案二:
参考一下:importjavax.swing.*;importjava.awt.event.ActionEvent;importjava.awt.*;importjavax.swing.border.EmptyBorder;importjava.awt.event.ActionListener;publicclassNumTest1extendsJFrameimplementsActionListener{privateJTextFieldnum1=newJTextField(15);privateJTextFieldnum2=newJTextField(15);privateJTextFieldresult=newJTextField(15);privateJLabellabel_1=newJLabel("Num1");privateJLabellabel_2=newJLabel("Num2");privateJLabellabel_3=newJLabel("结果");privateJButtonbtn1=newJButton("最大公约数");privateJButtonbtn2=newJButton("最小公倍数");privateJLabelr=newJLabel("求两个正整数的最大公约数和最小公倍数");publicNumTest1(){JPanelcontentPane=(JPanel)getContentPane();contentPane.setLayout(newBorderLayout());JPanelp1=newJPanel();JPanelp20=newJPanel();//新加JPanelp21=newJPanel();JPanelp22=newJPanel();JPanelp2=newJPanel(newGridLayout(0,3));JPanelp3=newJPanel();contentPane.setBorder(newEmptyBorder(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-->CENTERcontentPane.add(p3,BorderLayout.SOUTH);pack();setVisible(true);}publicstaticvoidmain(String[]args){newNumTest1();}publicvoidactionPerformed(ActionEvente){intm,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));}}publicintf1(intm,intn){returnf2(m>n?m:n,m<=n?m:n);}publicintf2(inta,intb){intc=a%b;if(c==0)returnb;else{returnf2(b,c);}}}
解决方案三:
最近正好看了这一块的东西,帮忙整了一下。importjava.awt.*;importjavax.swing.*;importjava.awt.event.*;importjavax.swing.event.*;importjavax.swing.border.EmptyBorder;publicclassNumTest1{publicstaticvoidmain(String[]args){NumFramecalcFrame=newNumFrame();calcFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);calcFrame.setVisible(true);}}classNumFrameextendsJFrameimplementsActionListener{publicNumFrame(){setTitle("NumTest1");//创建上中下三个面板,以及为了中间面板排版的三个辅助面板JPanelpanelN=newJPanel();JPanelpanelC=newJPanel(newGridLayout(0,3));JPanelpanelS=newJPanel();JPanelpanelC1=newJPanel();JPanelpanelC2=newJPanel();JPanelpanelC3=newJPanel();//创建标签和文本域和按钮JLabellabel_1=newJLabel("Num1");JLabellabel_2=newJLabel("Num2");JLabellabel_3=newJLabel("结果");JLabelr=newJLabel("求两个正整数的最大公约数和最小公倍数");num1=newJTextField(15);num2=newJTextField(15);result=newJTextField(15);btn1=newJButton("最大公约数");btn2=newJButton("最小公倍数");//将各组件按照需要的排版放到各面板中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);//将上中下面板放置到窗口内JPanelcontentPane=(JPanel)getContentPane();contentPane.setLayout(newBorderLayout());contentPane.setBorder(newEmptyBorder(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();}publicvoidactionPerformed(ActionEvente){intm;intn;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));}}publicintf1(intm,intn){returnf2(m>n?m:n,m<=n?m:n);}publicintf2(inta,intb){intc=a%b;if(c==0){returnb;}else{returnf2(b,c);}}privateJTextFieldnum1;privateJTextFieldnum2;privateJTextFieldresult;privateJButtonbtn1;privateJButtonbtn2;}
运行结果按照你的程序应该是这样的
解决方案四:
我这个程序是求两个正整数的最大公公约数和最小公倍数,现在我想加一个对话框,当我输入一个负数时,就出来这个对话框告诉我“输入错误,请重新输入”,然后点击确定回到NUMTEST1这个界面。我应该加什么代码呢???
解决方案五:
楼主参考一下:publicvoidactionPerformed(ActionEvente){intm=0,n=0;booleansuccess=true;//数据转换成功标志./***判断num1是否可转成整数,及是否是负数*/try{m=Integer.parseInt(num1.getText());}catch(Exceptionex){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(Exceptionex){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));}}
解决方案六:
我又加入了一段代码,在关闭文本框的时候出来一个对话框说:您确定退出吗?点击是就退出,点击否就什么也不做,可我应该把代码加在哪里呢?publicvoidwindowClosed(WindowEvente){}publicvoidwindowClosing(WindowEvente){intresult=JOptionPane.showConfirmDialog(this,"确实要退出吗","系统消息",JOptionPane.YES_NO_OPTION);if(result==JOptionPane.YES_OPTION){this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}
解决方案七:
publicNumTest1(){JPanelcontentPane=(JPanel)getContentPane();contentPane.setLayout(newBorderLayout());JPanelp1=newJPanel();JPanelp20=newJPanel();//新加JPanelp21=newJPanel();JPanelp22=newJPanel();JPanelp2=newJPanel(newGridLayout(0,3));JPanelp3=newJPanel();contentPane.setBorder(newEmptyBorder(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-->CENTERcontentPane.add(p3,BorderLayout.SOUTH);//------------------------------加在这this.addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){intresult=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);}
解决方案八:
参考:http://bbs.csdn.net/topics/80453552
解决方案九:
if(result==JOptionPane.YES_OPTION){System.exit(0);}}});这里缺少“}”,但这里的{}都是完整的??setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);这条语句也有错??