大家帮帮我,我的程序为什么不出界面(编译通过,执行时一闪即逝)
是一个简单的计算器程序,运行时闪都不闪,直接结束了,不知为什么,请指点一下
主要是为什么jframe都不显示出来,什么反映都没有,我同学也遇到了这个问题,
我们想可能是 JPanel,JFrame 没有设置好,
谢谢了
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.math.*;
public class Calculator implements ActionListener{
double OP1=0,OP2=0,OP3=0,OP4=0;
Double op;
int FLAG1=0,FLAG2=-1;
JFrame counterFrame;
JPanel resultPanel,keyPanel;
JLabel result;
public void Calculator(){
counterFrame=new JFrame("Calculator");
resultPanel=new JPanel();
keyPanel=new JPanel();
keyPanel.setLayout(new GridLayout(5,4,5,5));
addWidgets();
counterFrame.add(resultPanel);
counterFrame.add(keyPanel);
counterFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
counterFrame.pack();
counterFrame.setVisible(true);
}
private void addWidgets(){
result=new JLabel(" ");
resultPanel.add(result);
Button button1=new Button("7");
Button button2=new Button("8");
Button button3=new Button("9");
Button button4=new Button("/");
Button button5=new Button("4");
Button button6=new Button("5");
Button button7=new Button("6");
Button button8=new Button("*");
Button button9=new Button("1");
Button button10=new Button("2");
Button button11=new Button("3");
Button button12=new Button("-");
Button button13=new Button("0");
Button button14=new Button("Л");
Button button15=new Button("ln");
Button button16=new Button("+");
Button button17=new Button("DEL");
Button button18=new Button("+/-");
Button button19=new Button(".");
Button button20=new Button("=");
keyPanel.add(button1);
keyPanel.add(button2);
keyPanel.add(button3);
keyPanel.add(button4);
keyPanel.add(button5);
keyPanel.add(button6);
keyPanel.add(button7);
keyPanel.add(button8);
keyPanel.add(button9);
keyPanel.add(button10);
keyPanel.add(button11);
keyPanel.add(button12);
keyPanel.add(button13);
keyPanel.add(button14);
keyPanel.add(button15);
keyPanel.add(button16);
keyPanel.add(button17);
keyPanel.add(button18);
keyPanel.add(button19);
keyPanel.add(button20);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
button6.addActionListener(this);
button7.addActionListener(this);
button8.addActionListener(this);
button9.addActionListener(this);
button10.addActionListener(this);
button11.addActionListener(this);
button12.addActionListener(this);
button13.addActionListener(this);
button14.addActionListener(this);
button15.addActionListener(this);
button16.addActionListener(this);
button17.addActionListener(this);
button18.addActionListener(this);
button19.addActionListener(this);
button20.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(e.getActionCommand()=="button1") {
if(FLAG1==0){
if(FLAG2<0){
OP1=10*OP1+7;
op=new Double(OP1);
result.setText(op.toString(op.doubleValue()));
}
else{
FLAG2++;
OP3+=7/Math.pow(10,(double)FLAG2);
op=new Double(OP1+OP3);
result.setText(op.toString(op.doubleValue()));
}
}
else{
if(FLAG2<0){
OP2=10*OP2+7;
op=new Double(OP2);
result.setText(op.toString(op.doubleValue()));
}
else{
FLAG2++;
OP3+=7/Math.pow(10,(double)FLAG2);
op=new Double(OP2+OP3);
result.setText(op.toString(op.doubleValue()));
}
}
}
if(e.getActionCommand()=="button2") {
if(FLAG1==0){
if(FLAG2<0){
OP1=10*OP1+8;
op=new Double(OP1);
result.setText(op.toString(op.doubleValue()));
}
else{
FLAG2++;
OP3+=8/Math.pow(10,(double)FLAG2);
op=new Double(OP1+OP3);
result.setText(op.toString(op.doubleValue()));
}
}
else{
if(FLAG2<0){
OP2=10*OP2+8;
op=new Double(OP2);
result.setText(op.toString(op.doubleValue()));
}
else{
FLAG2++;
OP3+=8/Math.pow(10,(double)FLAG2);
op=new Double(OP2+OP3);
result.setText(op.toString(op.doubleValue()));
}
}
}
if(e.getActionCommand()=="button3") {
if(FLAG1==0){
if(FLAG2<0){
OP1=10*OP1+9;
op=new Double(OP1);
result.setText(op.toString(op.doubleValue()));
}
问题点数:0、回复次数:7Top
1 楼feitianfly(feitian)回复于 2003-11-03 19:40:47 得分 0
else{
FLAG2++;
OP3+=9/Math.pow(10,(double)FLAG2);
op=new Double(OP1+OP3);
result.setText(op.toString(op.doubleValue()));
}
}
else{
if(FLAG2<0){
OP2=10*OP2+9;
op=new Double(OP2);
result.setText(op.toString(op.doubleValue()));
}
else{
FLAG2++;
OP3+=9/Math.pow(10,(double)FLAG2);
op=new Double(OP2+OP3);
result.setText(op.toString(op.doubleValue()));
}
}
}
if(e.getActionCommand()=="button4") {
FLAG1=4;
FLAG2=-1;
}
if(e.getActionCommand()=="button5") {
if(FLAG1==0){
if(FLAG2<0){
OP1=10*OP1+4;
op=new Double(OP1);
result.setText(op.toString(op.doubleValue()));
}
else{
FLAG2++;
OP3+=4/Math.pow(10,(double)FLAG2);
op=new Double(OP1+OP3);
result.setText(op.toString(op.doubleValue()));
}
}
else{
if(FLAG2<0){
OP2=10*OP2+4;
op=new Double(OP2);
result.setText(op.toString(op.doubleValue()));
}
else{
FLAG2++;
OP3+=4/Math.pow(10,(double)FLAG2);
op=new Double(OP2+OP3);
result.setText(op.toString(op.doubleValue()));
}
}
}
if(e.getActionCommand()=="button6") {
if(FLAG1==0){
if(FLAG2<0){
OP1=10*OP1+5;
op=new Double(OP1);
result.setText(op.toString(op.doubleValue()));
}
else{
FLAG2++;
OP3+=5/Math.pow(10,(double)FLAG2);
op=new Double(OP1+OP3);
result.setText(op.toString(op.doubleValue()));
}
}
else{
if(FLAG2<0){
OP2=10*OP2+5;
op=new Double(OP2);
result.setText(op.toString(op.doubleValue()));
}
else{
FLAG2++;
OP3+=5/Math.pow(10,(double)FLAG2);
op=new Double(OP2+OP3);
result.setText(op.toString(op.doubleValue()));
}
}
}
if(e.getActionCommand()=="button7") {
if(FLAG1==0){
if(FLAG2<0){
OP1=10*OP1+6;
op=new Double(OP1);
result.setText(op.toString(op.doubleValue()));
}
else{
FLAG2++;
OP3+=6/Math.pow(10,(double)FLAG2);
op=new Double(OP1+OP3);
result.setText(op.toString(op.doubleValue()));
}
}
else{
if(FLAG2<0){
OP2=10*OP2+6;
op=new Double(OP2);
result.setText(op.toString(op.doubleValue()));
}
else{
FLAG2++;
OP3+=6/Math.pow(10,(double)FLAG2);
op=new Double(OP2+OP3);
result.setText(op.toString(op.doubleValue()));
}
}
}
if(e.getActionCommand()=="button8") {
FLAG1=8;
FLAG2=-1;
}
if(e.getActionCommand()=="button9") {
if(FLAG1==0){
if(FLAG2<0){
OP1=10*OP1+1;
op=new Double(OP1);
result.setText(op.toString(op.doubleValue()));
}
else{
FLAG2++;
OP3+=1/Math.pow(10,(double)FLAG2);
op=new Double(OP1+OP3);
result.setText(op.toString(op.doubleValue()));
}
}
else{
if(FLAG2<0){
OP2=10*OP2+1;
op=new Double(OP2);
result.setText(op.toString(op.doubleValue()));
}
else{
FLAG2++;
OP3+=1/Math.pow(10,(double)FLAG2);
op=new Double(OP2+OP3);
result.setText(op.toString(op.doubleValue()));
}
}
}
if(e.getActionCommand()=="button10") {
if(FLAG1==0){
if(FLAG2<0){
OP1=10*OP1+2;
op=new Double(OP1);
result.setText(op.toString(op.doubleValue()));
}
else{
FLAG2++;
OP3+=2/Math.pow(10,(double)FLAG2);
op=new Double(OP1+OP3);
result.setText(op.toString(op.doubleValue()));
}
}Top
2 楼feitianfly(feitian)回复于 2003-11-03 19:41:13 得分 0
else{
if(FLAG2<0){
OP2=10*OP2+2;
op=new Double(OP2);
result.setText(op.toString(op.doubleValue()));
}
else{
FLAG2++;
OP3+=2/Math.pow(10,(double)FLAG2);
op=new Double(OP2+OP3);
result.setText(op.toString(op.doubleValue()));
}
}
}
if(e.getActionCommand()=="button11") {
if(FLAG1==0){
if(FLAG2<0){
OP1=10*OP1+3;
op=new Double(OP1);
result.setText(op.toString(op.doubleValue()));
}
else{
FLAG2++;
OP3+=3/Math.pow(10,(double)FLAG2);
op=new Double(OP1+OP3);
result.setText(op.toString(op.doubleValue()));
}
}
else{
if(FLAG2<0){
OP2=10*OP2+3;
op=new Double(OP2);
result.setText(op.toString(op.doubleValue()));
}
else{
FLAG2++;
OP3+=3/Math.pow(10,(double)FLAG2);
op=new Double(OP2+OP3);
result.setText(op.toString(op.doubleValue()));
}
}
}
if(e.getActionCommand()=="button12") {
FLAG1=12;
FLAG2=-1;
}
if(e.getActionCommand()=="button13") {
if(FLAG1==0){
if(FLAG2<0){
OP1=10*OP1+0;
op=new Double(OP1);
result.setText(op.toString(op.doubleValue()));
}
else{
FLAG2++;
OP3+=0/Math.pow(10,(double)FLAG2);
op=new Double(OP1+OP3);
result.setText(op.toString(op.doubleValue()));
}
}
else{
if(FLAG2<0){
OP2=10*OP2+0;
op=new Double(OP2);
result.setText(op.toString(op.doubleValue()));
}
else{
FLAG2++;
OP3+=0/Math.pow(10,(double)FLAG2);
op=new Double(OP2+OP3);
result.setText(op.toString(op.doubleValue()));
}
}
}
if(e.getActionCommand()=="button14") {
if(FLAG1==0){
OP1=Math.PI;
op=new Double(OP1);
result.setText(op.toString(op.doubleValue()));
}
else{
OP2=Math.PI;
op=new Double(OP2);
result.setText(op.toString(op.doubleValue()));
}
}
if(e.getActionCommand()=="button15") {
OP4=Math.log(OP1);
op=new Double(OP4);
result.setText(op.toString(op.doubleValue()));
FLAG1=0;
FLAG2=-1;
}
if(e.getActionCommand()=="button16") {
FLAG1=16;
FLAG2=-1;
}
if(e.getActionCommand()=="button17") {
result.setText(" ");
FLAG1=0;
FLAG2=-1;
}
if(e.getActionCommand()=="button18") {
if(FLAG1==0){
OP1=-1*OP1;
op=new Double(OP1);
result.setText(op.toString(op.doubleValue()));
}
else{
OP2=-1*OP2;
op=new Double(OP2);
result.setText(op.toString(op.doubleValue()));
}
}
if(e.getActionCommand()=="button19") {
FLAG2=0;
}
if(e.getActionCommand()=="button20") {
switch(FLAG1){
case 4:
OP4=OP1/OP2;
op=new Double(OP4);
result.setText(op.toString(op.doubleValue()));
FLAG1=0;
FLAG2=-1;break;
case 8:
OP4=OP1*OP2;
op=new Double(OP4);
result.setText(op.toString(op.doubleValue()));
FLAG1=0;
FLAG2=-1;break;
case 12:
OP4=OP1-OP2;
op=new Double(OP4);
result.setText(op.toString(op.doubleValue()));
FLAG1=0;
FLAG2=-1;break;
case 16:
OP4=OP1+OP2;
op=new Double(OP4);
result.setText(op.toString(op.doubleValue()));
FLAG1=0;
FLAG2=-1;break;
}
}
}
public static void main(String[] args){
/*counterFrame=new JFrame("Calculator");*/
try {
UIManager.setLookAndFeel(
UIManager.getCrossPlatformLookAndFeelClassName());
} catch (Exception e) {}
Calculator calculator1=new Calculator();
/* counterFrame.add(resultPanel);
counterFrame.add(keyPanel);
counterFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
counterFrame.pack();
counterFrame.setVisible(true);*/
}
}
Top
3 楼samus(kmlzkma)回复于 2003-11-03 20:11:12 得分 0
太他妈长了!Top
4 楼zhanghuazhanghome(517(张))回复于 2003-11-03 20:15:16 得分 0
构建器应为public Calculator(){},那有void啊!Top
5 楼zhanghuazhanghome(517(张))回复于 2003-11-03 20:28:57 得分 0
counterFrame.add(resultPanel);
counterFrame.add(keyPanel);
改为
counterFrame.getContentPane().add(resultPanel);
counterFrame.getContentPane().add(keyPanel);Top
6 楼d992901(沉思)回复于 2003-11-03 20:55:59 得分 0
jFrame.setVisible(true);
写的好乱阿,ft
Top
7 楼feitianfly(feitian)回复于 2003-11-03 22:38:18 得分 0
谢谢zhanghuazhanghome,你是对的,解决了让我们很郁闷的问题
也代我同学谢谢你Top




