Jump to content

[JAVA]Interfata Calculator


Shirogane
 Share

Recommended Posts

  • Descriere: Calculator pe principiul topicului anterior, doar că am folosit GridBagLayout în loc de GridLayout
  • Poze / Video: image.png.6dcd4bc3e1ee8215915d8bc38fc2303c.png
  • Link de descărcare: În sursă.
  • Sursa (opțional): 
    Quote
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    
    public class Main extends JFrame implements ActionListener {
    
        private static final long serialVersionUID = 1L;
        private JTextField dsp;
        private JPanel btn_pan;
        private String[] btn_lab = {
                "7", "8", "9", "/",
                "4", "5", "6", "X",
                "1", "2", "3", "-",
                "0", "C", "=", "+"
        };
        private JButton[] btn = new JButton[btn_lab.length];
        private String opr = "";
        private double nr1, nr2, rez;
    
        private Color[] culori = {
                Color.RED, Color.BLUE, Color.GREEN, Color.YELLOW,
                Color.ORANGE, Color.PINK, Color.CYAN, Color.MAGENTA
        };
    
        public Main() {
            super("Calculator");
            setResizable(false);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLayout(new BorderLayout());
            interf();
        }
    
        private void interf() {
            dsp = new JTextField();
            dsp.setEditable(false);
            add(dsp, BorderLayout.NORTH);
    
            btn_pan = new JPanel();
            btn_pan.setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets(10, 10, 10, 10);
    
            int row = 0;
            int col = 0;
            for (int i = 0; i < btn_lab.length; i++) {
                btn[i] = new JButton(btn_lab[i]);
                btn[i].addActionListener(this);
                btn[i].setBackground(culori[i % culori.length]);
                gbc.gridx = col;
                gbc.gridy = row;
                btn_pan.add(btn[i], gbc);
                col++;
                if (col > 3) {
                    col = 0;
                    row++;
                }
            }
    
            btn_pan.setBackground(culori[culori.length - 1]);
    
            add(btn_pan, BorderLayout.CENTER);
            pack();
        }
    
        public void actionPerformed(ActionEvent azione) {
            String com = azione.getActionCommand();
    
            if ((com.charAt(0) >= '0' && com.charAt(0) <= '9') || com.equals(".")) {
                dsp.setText(dsp.getText() + com);
            } else if (com.equals("C")) {
                dsp.setText("");
            } else if (com.equals("=")) {
                nr2 = Double.parseDouble(dsp.getText());
                switch (opr) {
                    case "+":
                        rez = nr1 + nr2;
                        break;
                    case "-":
                        rez = nr1 - nr2;
                        break;
                    case "X":
                        rez = nr1 * nr2;
                        break;
                    case "/":
                        rez = nr1 / nr2;
                        break;
                }
                dsp.setText(String.valueOf(rez));
                opr = "";
            } else {
                if (!dsp.getText().isEmpty()) {
                    nr1 = Double.parseDouble(dsp.getText());
                    opr = com;
                    dsp.setText("");
                }
            }
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(() -> {
                Main calc = new Main();
                calc.setVisible(true);
            });
        }
    }
    

     

  • Alte precizări: Se poate testa. Dacă aveţi întrebări le aştept în comentarii.

 

Edited by Shirogane
Link to comment
Share on other sites

  • Shirogane changed the title to [JAVA]Interfata Calculator

in my opinion you shouldnt have made two topics for the same thing, they both gives the same results at the end ! 
Overall GridBagLayout is good to use for bigger projects as it is more complex to set up. 

Link to comment
Share on other sites

2 hours ago, AdeM nWo said:

Overall GridBagLayout is good to use for bigger projects as it is more complex to set up. 

Agreed. That's why I decided to use that instead. I really thought of changing the first topic with this version, but I changed my mind and I chose to create a new topic with the idea updated. With this comment I also want to let the viewers know that GridBagLayout is one of the most complex and flexible layout managers from the Java Swing. It also allows you to have a better control regarding the precision and dimension of each chosen component. 

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.