我的Swing应用程序不再正常显示

时间:2014-09-23 12:26:59

标签: java swing calculator

我使用Swing开发了一个Java Calculator。我有很多按钮,JLabel和其他功能。 在我运行程序之前,它正确显示。在它没有正确显示之后我总是必须关闭应用程序的窗口并反复运行它多次,直到它显示出来。

这就是我得到的:

enter image description here

此外,我没有任何错误或例外。这个问题的原因是什么?

代码:

import javax.swing.*;
import javax.swing.border.Border;

import org.jdesktop.swingx.JXDatePicker;

import net.sourceforge.jdatepicker.impl.JDatePanelImpl;
import net.sourceforge.jdatepicker.impl.JDatePickerImpl;
import net.sourceforge.jdatepicker.impl.UtilDateModel;

public class Calculette extends JFrame implements ActionListener{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;



    Border cadre = BorderFactory.createTitledBorder("");
    //Les panneaux
    JPanel panel1 = new JPanel();
    JPanel panel2= new JPanel();
    JPanel panel3 = new JPanel();

    //les menus
    JMenuBar menubar = new JMenuBar();
    JMenu fichier = new JMenu("Fichier");
    JMenu edition = new JMenu("Edition");
    JMenu help = new JMenu("?");
    JMenuItem fermer = new JMenuItem("Fermer");
    JMenuItem copier = new JMenuItem("Copier");
    JMenuItem coller = new JMenuItem("Coller");
    JMenuItem apropos = new JMenuItem("A propos de la calculatrice");
    JLabel jlab1 = new JLabel("Selectionnez un type de calcul sur les dates :");
    JComboBox<String> list = new JComboBox<String>();
    GridBagLayout gbl = new GridBagLayout();
    GridBagConstraints gbc = new GridBagConstraints();
    BorderLayout borderl = new BorderLayout(); 
    public JTextArea resultat = new JTextArea(2,35);
    ButtonGroup bg = new ButtonGroup();
    JRadioButton degres = new JRadioButton("Degrés");
    JRadioButton radians = new JRadioButton("Radians");
    JRadioButton grades = new JRadioButton("Grades");
    JButton mc = new JButton ("MC");
    JButton mr = new JButton ("MR");
    JButton ms = new JButton ("MS");
    JButton mpls = new JButton ("M+");
    JButton mmns = new JButton ("M-");

    JButton vide = new JButton("");
    JButton inv = new JButton("Inv");
    JButton ln = new JButton("ln");
    JButton pranthese1 = new JButton("(");
    JButton pranthese2 = new JButton(")");


    JButton efface = new JButton("\u2190");
    JButton ce = new JButton("CE");
    JButton c = new JButton("C");
    JButton plusmoins = new JButton(" ±");
    JButton carre = new JButton("√");

    JButton intt = new JButton("Int");
    JButton sinh = new JButton("sinh");
    JButton sin = new JButton("sin");
    JButton xcarre = new JButton("x²");
    JButton nfact = new JButton("n!");

    JButton b7 = new JButton("7");
    JButton b8 = new JButton("8");
    JButton b9 = new JButton("9");
    JButton slash = new JButton("/");
    JButton percent = new JButton("%");

    JButton dms = new JButton("dms");
    JButton cosh = new JButton("cosh");
    JButton cos = new JButton("cos");
    JButton xpowery = new JButton("<html>x<sup>y</sup></html>");
    JButton ycarrex = new JButton("y√x");

    JButton b4 = new JButton("4");
    JButton b5 = new JButton("5");
    JButton b6 = new JButton("6");
    JButton etoil = new JButton("*");
    JButton unsurx = new JButton("1/x");

    JButton pi = new JButton("π");
    JButton tanh = new JButton("tanh");
    JButton tan = new JButton("tan");
    JButton xpowertr = new JButton("x³");
    JButton carrecubx = new JButton("∛x");

    JButton b1 = new JButton("1");
    JButton b2 = new JButton("2");
    JButton b3 = new JButton("3");
    JButton moins = new JButton("-");
    JButton egal = new JButton("=");

    JButton fe = new JButton("F-E");
    JButton exp = new JButton("Exp");
    JButton mod = new JButton("Mod");
    JButton log = new JButton("log");
    JButton dixy = new JButton("<html>10<sup>x</sup></html>");

    public JButton b0 = new JButton("0");
    JButton virgule = new JButton(",");
    JButton plus = new JButton("+");

    //deuxieme panel
    JLabel de = new JLabel("de");
    JLabel a = new JLabel("a");


    JXDatePicker datePicker1 = new JXDatePicker();
    JXDatePicker datePicker2 = new JXDatePicker();
    JXDatePicker datePicker3 = new JXDatePicker();


    JLabel labdiff = new JLabel("Différence (années, mois, semaines, jours)");
    public JTextField textdif = new JTextField(40);
    JLabel labdiffj = new JLabel("Différence (jours)");
    public JTextField textdifj = new JTextField(40);

    JLabel dee = new JLabel("De");
    ButtonGroup btg = new ButtonGroup();
    JRadioButton ajout = new JRadioButton("Ajouter");
    JRadioButton soustr = new JRadioButton("Soustraire");
    JLabel an = new JLabel("Année(s)");
    JLabel mois = new JLabel("Mois");
    JLabel jour = new JLabel("Jour(s)");

    SpinnerModel spinnerModel = new SpinnerNumberModel(0,0,999,1);
    SpinnerModel spinnerModel2 = new SpinnerNumberModel(0,0,999,1);
    SpinnerModel spinnerModel3 = new SpinnerNumberModel(0,0,999,1);

    JSpinner anlist = new JSpinner(spinnerModel);
    JSpinner moilist = new JSpinner(spinnerModel2);
    JSpinner jourlist = new JSpinner(spinnerModel3);

    JLabel datee = new JLabel("Date");
    JTextField textdate = new JTextField(40);
    public JButton calcul1 = new JButton("Calculer");

    public JButton calculer = new JButton("Calculer");

    ////
    Font f = new Font("Arial", Font.BOLD, 10);
    JButton frac = new JButton("Frac");
    JButton ex = new JButton("<html>e<sup>x</sup></html>");
    JButton sinhun = new JButton("<html>sinh<sup>-1</sup></html>");
    JButton sinmun = new JButton("<html>sin<sup>-1</sup></html>");
    JButton deg = new JButton("deg");
    JButton coshun = new JButton("<html>cosh<sup>-1</sup></html>");
    JButton cosun = new JButton("<html>cos<sup>-1</sup></html>");
    JButton deuxpi = new JButton("2*π");
    JButton tanhun = new JButton("<html>tanh<sup>-1</sup></html>");
    JButton tanun = new JButton("<html>tan<sup>-1</sup></html>");
    ////
    int first=1;  double result=0,memory=0,n1=0;
    double num;  int opt=2; int oper1=0,oper2=0;

    @SuppressWarnings("deprecation")
    public Calculette(){

        super("Calculatrice");
        this.setVisible(true);
        this.setSize(1100, 320);
        this.getContentPane().add(panel1);
        panel1.setLayout(borderl);
        panel1.add(panel2,BorderLayout.WEST);
        panel1.add(panel3,BorderLayout.EAST);
        panel3.setBackground(Color.WHITE); 
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);
        this.setJMenuBar(menubar);
        menubar.add(fichier);
        menubar.add(edition);
        menubar.add(help);
        fichier.add(fermer);
        edition.add(copier);
        edition.add(coller);
        help.add(apropos);
        copier.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,Event.CTRL_MASK,true));
        coller.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,Event.CTRL_MASK,true));
        list.addItem("Calculer la différence entre deux dates");
        list.addItem("Ajouter ou soustraire des jours à une date spécifiée");
        list.setSize(20, 10);
        panel2.setBorder(cadre);
        panel3.setBorder(cadre); 
        resultat.setBorder(cadre);
        panel2.setLayout(gbl);
        panel3.setLayout(gbl);
        btg.add(ajout); btg.add(soustr); 
        ajout.setBackground(Color.WHITE);
        soustr.setBackground(Color.WHITE);
        datePicker1.setDate(Calendar.getInstance().getTime());
        datePicker1.setFormats(new SimpleDateFormat("dd/MM/yyyy"));
        datePicker2.setDate(Calendar.getInstance().getTime());
        datePicker2.setFormats(new SimpleDateFormat("dd/MM/yyyy"));
        datePicker3.setDate(Calendar.getInstance().getTime());
        datePicker3.setFormats(new SimpleDateFormat("dd/MM/yyyy"));

        coordonne(0,0,gbc,10,1);
        panel2.add(resultat,gbc);
        resultat.setEditable(false);
        bg.add(degres); bg.add(radians); bg.add(grades);
        vide.setEnabled(false);
        textdif.setEditable(false);
        textdifj.setEditable(false);
        textdate.setEditable(false);

        coordonne(0,1,gbc,1,1);
        panel2.add(degres,gbc);

        coordonne(1,1,gbc,1,1);
        panel2.add(radians,gbc);

        coordonne(2,1,gbc,3,1);
        panel2.add(grades,gbc);

        coordonne(5,1,gbc,1,1);
        panel2.add(mc,gbc);

        coordonne(6,1,gbc,1,1);
        panel2.add(mr,gbc);

        coordonne(7,1,gbc,1,1);
        panel2.add(ms,gbc);

        coordonne(8,1,gbc,1,1);
        panel2.add(mpls,gbc);

        coordonne(9,1,gbc,1,1);
        panel2.add(mmns,gbc);

        coordonne(0,2,gbc,1,1);
        panel2.add(vide,gbc);

        coordonne(1,2,gbc,1,1);
        panel2.add(inv,gbc);

        coordonne(2,2,gbc,1,1);
        panel2.add(ln,gbc);

        coordonne(3,2,gbc,1,1);
        panel2.add(pranthese1,gbc);

        coordonne(4,2,gbc,1,1);
        panel2.add(pranthese2,gbc);

        coordonne(5,2,gbc,1,1);
        panel2.add(efface,gbc);

        coordonne(6,2,gbc,1,1);
        panel2.add(ce,gbc);

        coordonne(7,2,gbc,1,1);
        panel2.add(c,gbc);

        coordonne(8,2,gbc,1,1);
        panel2.add(plusmoins,gbc);

        coordonne(9,2,gbc,1,1);
        panel2.add(carre,gbc);

        coordonne(0,3,gbc,1,1);
        panel2.add(intt,gbc);

        coordonne(1,3,gbc,1,1);
        panel2.add(sinh,gbc);

        coordonne(2,3,gbc,1,1);
        panel2.add(sin,gbc);

        coordonne(3,3,gbc,1,1);
        panel2.add(xcarre,gbc);

        coordonne(4,3,gbc,1,1);
        panel2.add(nfact,gbc);

        coordonne(5,3,gbc,1,1);
        panel2.add(b7,gbc);

        coordonne(6,3,gbc,1,1);
        panel2.add(b8,gbc);

        coordonne(7,3,gbc,1,1);
        panel2.add(b9,gbc);

        coordonne(8,3,gbc,1,1);
        panel2.add(slash,gbc);

        coordonne(9,3,gbc,1,1);
        panel2.add(percent,gbc);

        coordonne(0,4,gbc,1,1);
        panel2.add(dms,gbc);

        coordonne(1,4,gbc,1,1);
        panel2.add(cosh,gbc);

        coordonne(2,4,gbc,1,1);
        panel2.add(cos,gbc);

        coordonne(3,4,gbc,1,1);
        panel2.add(xpowery,gbc);

        coordonne(4,4,gbc,1,1);
        panel2.add(ycarrex,gbc);

        coordonne(5,4,gbc,1,1);
        panel2.add(b4,gbc);

        coordonne(6,4,gbc,1,1);
        panel2.add(b5,gbc);

        coordonne(7,4,gbc,1,1);
        panel2.add(b6,gbc);

        coordonne(8,4,gbc,1,1);
        panel2.add(etoil,gbc);

        coordonne(9,4,gbc,1,1);
        panel2.add(unsurx,gbc);

        coordonne(0,5,gbc,1,1);
        panel2.add(pi,gbc);

        coordonne(1,5,gbc,1,1);
        panel2.add(tanh,gbc);

        coordonne(2,5,gbc,1,1);
        panel2.add(tan,gbc);

        coordonne(3,5,gbc,1,1);
        panel2.add(xpowertr,gbc);

        coordonne(4,5,gbc,1,1);
        panel2.add(carrecubx,gbc);

        coordonne(5,5,gbc,1,1);
        panel2.add(b1,gbc);

        coordonne(6,5,gbc,1,1);
        panel2.add(b2,gbc);

        coordonne(7,5,gbc,1,1);
        panel2.add(b3,gbc);

        coordonne(8,5,gbc,1,1);
        panel2.add(moins,gbc);

        coordonne(9,5,gbc,1,2);
        panel2.add(egal,gbc);

        coordonne(0,6,gbc,1,1);
        panel2.add(fe,gbc);

        coordonne(1,6,gbc,1,1);
        panel2.add(exp,gbc);

        coordonne(2,6,gbc,1,1);
        panel2.add(mod,gbc);

        coordonne(3,6,gbc,1,1);
        panel2.add(log,gbc);

        coordonne(4,6,gbc,1,1);
        panel2.add(dixy,gbc);

        coordonne(5,6,gbc,2,1);
        panel2.add(b0,gbc);

        coordonne(7,6,gbc,1,1);
        panel2.add(virgule,gbc);

        coordonne(8,6,gbc,1,1);
        panel2.add(plus,gbc);


        coordonne(0,0,gbc,6,1);
        panel3.add(jlab1,gbc);

        coordonne(0,1,gbc,6,1);
        panel3.add(list,gbc);
        ////////////////////////
        coordonne(0,2,gbc,2,1);
        panel3.add(de,gbc);

        coordonne(2,2,gbc,1,1);
        panel3.add(datePicker1,gbc);

        coordonne(3,2,gbc,2,1);
        panel3.add(a,gbc);

        coordonne(5,2,gbc,1,1);
        panel3.add(datePicker2,gbc);

        coordonne(0,3,gbc,6,1);
        panel3.add(labdiff,gbc);

        coordonne(0,4,gbc,6,1);
        panel3.add(textdif,gbc);

        coordonne(0,5,gbc,6,1);
        panel3.add(labdiffj,gbc);

        coordonne(0,6,gbc,6,1);
        panel3.add(textdifj,gbc);

        coordonne(5,7,gbc,1,1);
        panel3.add(calculer,gbc);








        //Evenement basics
        list.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {

                if (list.getSelectedIndex()==0 ){

                    panel3.remove(dee); 
                    panel3.validate();
                    panel3.remove(datePicker3); 
                    panel3.validate();
                    panel3.remove(ajout); 
                    panel3.validate();
                    panel3.remove(soustr); 
                    panel3.validate();
                    panel3.remove(an);
                    panel3.validate();
                    panel3.remove(anlist);
                    panel3.validate();
                    panel3.remove(mois);
                    panel3.validate();
                    panel3.remove(moilist);
                    panel3.validate();
                    panel3.remove(jour);
                    panel3.validate();
                    panel3.remove(jourlist);
                    panel3.validate();
                    panel3.remove(datee); 
                    panel3.validate();
                    panel3.remove(textdate);
                    panel3.validate();
                    panel3.remove(calcul1);
                    panel3.validate();
                    ///////////
                    coordonne(0,2,gbc,2,1);
                    panel3.add(de,gbc);

                    coordonne(2,2,gbc,1,1);
                    panel3.add(datePicker1,gbc);

                    coordonne(3,2,gbc,2,1);
                    panel3.add(a,gbc);

                    coordonne(5,2,gbc,1,1);
                    panel3.add(datePicker2,gbc);

                    coordonne(0,3,gbc,6,1);
                    panel3.add(labdiff,gbc);

                    coordonne(0,4,gbc,6,1);
                    panel3.add(textdif,gbc);

                    coordonne(0,5,gbc,6,1);
                    panel3.add(labdiffj,gbc);

                    coordonne(0,6,gbc,6,1);
                    panel3.add(textdifj,gbc);

                    coordonne(5,7,gbc,1,1);
                    panel3.add(calculer,gbc);

                    panel3.validate();      
                }

                if( list.getSelectedIndex()==1){

                    panel3.remove(de);
                    panel3.validate();
                    panel3.remove(datePicker1);
                    panel3.validate();
                    panel3.remove(a);
                    panel3.validate();
                    panel3.remove(datePicker2);
                    panel3.validate();
                    panel3.remove(labdiff);
                    panel3.validate();
                    panel3.remove(textdif);
                    panel3.validate();
                    panel3.remove(labdiffj);
                    panel3.validate();
                    panel3.remove(textdifj);
                    panel3.validate();
                    panel3.remove(calculer);
                    panel3.validate();                  

                    /////////

                    coordonne(0,2,gbc,1,1);
                    panel3.add(dee,gbc); 

                    coordonne(2,2,gbc,2,1);
                    panel3.add(datePicker3,gbc); 

                    coordonne(4,2,gbc,1,1);
                    panel3.add(ajout,gbc); 

                    coordonne(5,2,gbc,2,1);
                    panel3.add(soustr,gbc); 

                    coordonne(0,3,gbc,1,1);
                    panel3.add(an,gbc);

                    coordonne(1,3,gbc,1,1);
                    panel3.add(anlist,gbc);

                    coordonne(2,3,gbc,1,1);
                    panel3.add(mois,gbc);

                    coordonne(3,3,gbc,1,1);
                    panel3.add(moilist,gbc);

                    coordonne(4,3,gbc,1,1);
                    panel3.add(jour,gbc);

                    coordonne(5,3,gbc,1,1);
                    panel3.add(jourlist,gbc);

                    coordonne(0,4,gbc,6,1);
                    panel3.add(datee,gbc);

                    coordonne(0,5,gbc,6,1);
                    panel3.add(textdate,gbc);

                    coordonne(5,6,gbc,1,1);
                    panel3.add(calcul1,gbc);
                    panel3.validate();
                }
            }
        });

        inv.addActionListener(this);
        final JButton[] tab_button = {b1, b2, b3, b4, b5, b6, b7, b8, b9, b0,c,ce,calcul1,calculer,carre,carrecubx,cos,plus,moins,slash,etoil,egal,mc,mmns,mpls,efface,unsurx,plusmoins};

        for(int i = 0; i < tab_button.length; i++)
        {
            tab_button[i].addActionListener(this);
        }

        final JMenuItem[] tab_items = {apropos,coller,copier,fermer};
        for(int i = 0; i < tab_items.length; i++)
        {
            tab_items[i].addActionListener(this);
        }

    }


    public static void coordonne(int x, int y, GridBagConstraints g,int gw,int gh){
        g.gridx= x;
        g.gridy= y ;
        g.insets=new Insets(2,2,2,2);
        g.gridwidth = gw;
        g.gridheight = gh;
        g.fill=GridBagConstraints.BOTH;
    } 



    public static void main(String[] args) {
        new Calculette();
    }


    @Override
    public void actionPerformed(ActionEvent e) {

        String str;
        //Action Corresponding to + button
        if(e.getSource()==plus)
        {
            if(first==1)
            {
                result=num=Double.parseDouble(resultat.getText());
                oper1=1;
            }
            else
            {
                n1=Double.parseDouble(resultat.getText());
                oper2=1;
            }
            //Action corresponding to previous operator
            switch(oper1)
            {
            case 1:add1();
            break;
            case 2:sub();
            break;
            case 3:div();
            break;
            case 4:mul();
            break;
            }
        }
        //Action Corresponding to * button
        if(e.getSource()==etoil)
        {
            if(first==1)
            {
                result=num=Double.parseDouble(resultat.getText());
                oper1=4;
            }
            else
            {
                n1=Double.parseDouble(resultat.getText());
                oper2=4;
            }
            //Action corresponding to previous operator
            switch(oper1)
            {
            case 1:add1();
            break;
            case 2:sub();
            break;
            case 3:div();
            break;
            case 4:mul();
            break;
            }
        }
        //Action Corresponding to / button
        if(e.getSource()==slash)
        {
            if(first==1)
            {
                result=num=Double.parseDouble(resultat.getText());
                oper1=3;
            }
            else
            {
                n1=Double.parseDouble(resultat.getText());
                oper2=3;
            }
            //Action corresponding to previous operator
            switch(oper1)
            {
            case 1:add1();
            break;
            case 2:sub();
            break;
            case 3:div();
            break;
            case 4:mul();
            break;
            }
        }
        //Action Corresponding to - button
        else if(e.getSource()==moins)
        {
            if(first==1)
            {
                result=num=Double.parseDouble(resultat.getText());
                oper1=2;
            }
            else
            {
                n1=Double.parseDouble(resultat.getText());
                oper2=2;
            }
            //Action corresponding to previous operator
            switch(oper1)
            {
            case 1:add1();
            break;
            case 2:sub();
            break;
            case 3:div();
            break;
            case 4:mul();
            break;
            }

        }
        //Action Corresponding to = button
        else if(e.getSource()==egal)
        {
            double n1=Double.parseDouble(resultat.getText());
            if(oper1==1)
                result=result+n1;
            else if(oper1==2)
                result=result-n1;
            else if(oper1==3)
                result=result/n1;
            else if(oper1==4)
                result=result*n1;
            else
                result=Double.parseDouble(resultat.getText());
            num=result;
            str=String.valueOf(result);
            resultat.setText(str);
        }
        //Action Corresponding to MS button
        else if(e.getSource()==ms)
        {
            memory=Double.parseDouble(resultat.getText());
            str=String.valueOf("M");


        }
        //Action Corresponding to M button
        else if(e.getSource()==mpls)
        {
            memory=memory+Double.parseDouble(resultat.getText());
        }
        //Action Corresponding to MC button
        else if(e.getSource()==mc)
        {
            memory=0;
            resultat.setText("0");

        }
        //Action Corresponding to MR button
        else if(e.getSource()==mr)
        {
            str=String.valueOf(memory);
            resultat.setText(str);
        }
        //Action Corresponding to +/- button
        else if(e.getSource()==plusmoins)
        {
            double n1=Double.parseDouble(resultat.getText());
            n1=-n1;
            str=String.valueOf(n1);
            resultat.setText(str);
        }
        //Action Corresponding to 0 button
        else if(e.getSource()==b0)
        {
            if(opt==1)
                str=String.valueOf(resultat.getText())+0;
            else
                str=String.valueOf(" ")+0;
            opt=1;
            resultat.setText(str);  

        }
        //Action Corresponding to 1 button
        else if(e.getSource()==b1)
        {
            if(opt==1)
                str=String.valueOf(resultat.getText())+1;
            else
                str=String.valueOf(" ")+1;
            opt=1;
            resultat.setText(str);

        }
        //Action Corresponding to 2 button
        else if(e.getSource()==b2)
        {
            if(opt==1)
                str=String.valueOf(resultat.getText())+2;
            else
                str=String.valueOf(" ")+2;
            opt=1;
            resultat.setText(str);
        }
        //Action Corresponding to 3 button
        else if(e.getSource()==b3)
        {
            if(opt==1)
                str=String.valueOf(resultat.getText())+3;
            else
                str=String.valueOf(" ")+3;
            opt=1;
            resultat.setText(str);
        }
        //Action Corresponding to 4 button
        else if(e.getSource()==b4)
        {
            if(opt==1)
                str=String.valueOf(resultat.getText())+4;
            else
                str=String.valueOf(" ")+4;
            opt=1;
            resultat.setText(str);
        }
        //Action Corresponding to 5 button
        else if(e.getSource()==b5)
        {
            if(opt==1)
                str=String.valueOf(resultat.getText())+5;
            else
                str=String.valueOf(" ")+5;
            opt=1;
            resultat.setText(str);
        }
        //Action Corresponding to 6 button
        else if(e.getSource()==b6)
        {
            if(opt==1)
                str=String.valueOf(resultat.getText())+6;
            else
                str=String.valueOf(" ")+6;
            opt=1;
            resultat.setText(str);
        }
        //Action Corresponding to 7 button
        else if(e.getSource()==b7)
        {
            if(opt==1)
                str=String.valueOf(resultat.getText())+7;
            else
                str=String.valueOf(" ")+7;
            opt=1;
            resultat.setText(str);
        }
        //Action Corresponding to 8 button
        else if(e.getSource()==b8)
        {
            if(opt==1)
                str=String.valueOf(resultat.getText())+8;
            else
                str=String.valueOf(" ")+8;
            opt=1;
            resultat.setText(str);
        }
        //Action Corresponding to 9 button
        else if(e.getSource()==b9)
        {
            if(opt==1)
                str=String.valueOf(resultat.getText())+9;
            else
                str=String.valueOf(" ")+9;
            opt=1;
            resultat.setText(str);
        }
        //Action Corresponding to BACKSPACE button
        else if(e.getSource()==efface)
        {
            int len;
            str=resultat.getText();
            len=str.length();
            if(len>=1)
                str=str.substring(0,len-1);
            resultat.setText(str);
        }
        //Action Corresponding to CE button
        else if(e.getSource()==ce)
        {
            result=0;
            first=1;
            opt=2;
            str=String.valueOf('0');
            resultat.setText(str);

        }
        //Action Corresponding to C button
        else if(e.getSource()==c)
        {
            result=0;
            memory=0;
            first=1;
            opt=2;
            resultat.setText("0");


        }
        //Action Corresponding to . button
        else if(e.getSource()==virgule)
        {
            str=String.valueOf(resultat.getText())+".";
            resultat.setText(str); 
        }
        //Action Corresponding to 1/x button
        else if(e.getSource()==unsurx)
        {
            double n1=Double.parseDouble(resultat.getText());
            n1=1/n1;
            str=String.valueOf(n1);
            resultat.setText(str); 
        }
        //Action Corresponding to sqrt button
        else if(e.getSource()==carre)
        {
            double n1=Double.parseDouble(resultat.getText());
            n1=Math.sqrt(n1);
            str=String.valueOf(n1);
            resultat.setText(str);
        }
        //Action Corresponding to % button
        else if(e.getSource()==percent)
        {
            double n1=Double.parseDouble(resultat.getText());
            n1=(n1*num)/100;
            str=String.valueOf(n1);
            resultat.setText(str);
        }

        else if (e.getSource()==calculer){


            Date date1 = null;
            Date date2 = null;

            int nbSemaines = 0;
            int nbAnnees = 0;
            long nbJours = 0;
            long UNE_HEURE = 60 * 60 * 1000L;
            date1 = (Date) datePicker1.getDate();
            date2 = (Date) datePicker2.getDate();


            long nbj = (date2.getTime() - date1.getTime() + UNE_HEURE) / (UNE_HEURE * 24);


            textdif.setText(nbAnnees +" années; "+ nbSemaines+" semaines; "+ nbJours+" jours"); 
            textdifj.setText(nbj+" jours");



        }

        else if(e.getSource()==apropos){  new Apropos(); }
        else if(e.getSource()==fermer){ System.exit(0);} 
        else if (e.getSource()==inv){

            if (inv.getModel().isSelected()){
                panel2.remove(intt); panel2.remove(ln); panel2.remove(sin); panel2.remove(sinh);
                panel2.remove(dms); panel2.remove(cosh);   panel2.remove(cos);   panel2.remove(pi);
                panel2.remove(tanh);   panel2.remove(tan);
                panel2.validate();

                coordonne(0,3,gbc,1,1);
                panel2.add(frac,gbc);

                coordonne(1,3,gbc,1,1);
                panel2.add(sinhun,gbc);

                coordonne(2,3,gbc,1,1);
                panel2.add(sinmun,gbc);

                coordonne(0,4,gbc,1,1);
                panel2.add(deg,gbc);

                coordonne(1,4,gbc,1,1);
                panel2.add(coshun,gbc);

                coordonne(2,4,gbc,1,1);
                panel2.add(cosun,gbc);

                coordonne(0,5,gbc,1,1);
                panel2.add(deuxpi,gbc);

                coordonne(1,5,gbc,1,1);
                panel2.add(tanhun,gbc);

                coordonne(2,5,gbc,1,1);
                panel2.add(tanun,gbc);

                coordonne(2,2,gbc,1,1);
                panel2.add(ex,gbc);
                panel2.validate();}
            else { 
                panel2.remove(frac);
                panel2.remove(sinhun);
                panel2.remove(sinmun);
                panel2.remove(deg); 
                panel2.remove(coshun);
                panel2.remove(cosun);
                panel2.remove(deuxpi);
                panel2.remove(tanhun);
                panel2.remove(tanun);
                panel2.remove(ex);
                panel2.validate();

                coordonne(0,3,gbc,1,1);
                panel2.add(intt,gbc);

                coordonne(1,3,gbc,1,1);
                panel2.add(sinh,gbc);

                coordonne(2,3,gbc,1,1);
                panel2.add(sin,gbc);

                coordonne(0,4,gbc,1,1);
                panel2.add(dms,gbc);

                coordonne(1,4,gbc,1,1);
                panel2.add(cosh,gbc);

                coordonne(2,4,gbc,1,1);
                panel2.add(cos,gbc);

                coordonne(0,5,gbc,1,1);
                panel2.add(pi,gbc);

                coordonne(1,5,gbc,1,1);
                panel2.add(tanh,gbc);

                coordonne(2,5,gbc,1,1);
                panel2.add(tan,gbc);

                coordonne(2,2,gbc,1,1);
                panel2.add(ln,gbc);
                panel2.validate();
            }

        }

    }

    /////// Operations

    void add1()
    {
        if(first==0)
            result=num=result+n1;
        String str=String.valueOf(result);
        resultat.setText(str);
        opt=2;
        if(oper2!=0)
        {
            oper1=oper2;
            oper2=0;
        }
        first=0;
    }
    //Sub called according to previous operator
    void sub()
    {
        if(first==0)
            result=num=result-n1;
        String str=String.valueOf(result);
        resultat.setText(str);
        opt=2;
        if(oper2!=0)
            oper1=oper2;
        first=0;
    }
    //Div called according to previous operator
    void div()
    {
        if(first==0)
        {
            if(n1==0)
                resultat.setText("Cannot divide by zero");
            else
                result=num=result/n1;
        }
        String str=String.valueOf(result);
        resultat.setText(str);
        opt=2;
        if(oper2!=0)
            oper1=oper2;
        first=0;
    }
    //Mul called according to previous operator
    void mul()
    {
        if(first==0)
            result=num=result*n1;
        String str=String.valueOf(result);
        resultat.setText(str);
        opt=2;
        if(oper2!=0)
            oper1=oper2;
        first=0;
    }



}

1 个答案:

答案 0 :(得分:2)

您在构造函数开始时调用setVisible(true) 在JFrame中添加组件,因此您需要在添加所有组件之前显示GUI。不要这样做,在构造函数的末尾调用此方法,或者在将所有组件添加到GUI时完成。

再次,永远不要做出这样的假设,""the code is all correct..." - 特别是在&gt;的大型行为不端的程序中。 1000行。我知道 - 每次我做到这一点,我都看起来很愚蠢,所以我已经吸取了教训。

此外,您在代码中交换了很多组件,并且应该考虑使用CardLayout来帮助您以更简洁的方式进行交换。