Visualizzazione dei risultati da 1 a 4 su 4

Discussione: pulsante esci

  1. #1
    Utente di HTML.it
    Registrato dal
    Mar 2008
    Messaggi
    137

    pulsante esci

    Ho scritto questo programma in JBuilder 2008(può essere visto anche con un altro compilatore); non riesco a far funzionare il pulsante "OFF", cioè quando clicco su questo pulsante mi esce dal programma.....Qualcuno sa come si fa?



    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;


    public class jfcalc extends JFrame {

    private final JTextField textField = new JTextField();
    private final JButton acButton = new JButton();
    private final JButton acButton_1 = new JButton();
    private final JButton button = new JButton();
    private final JButton acButton_5 = new JButton();
    private final JButton acButton_6 = new JButton();
    private final JButton acButton_2 = new JButton();
    private final JButton acButton_3 = new JButton();
    private final JButton acButton_4 = new JButton();
    private final JButton acButton_7 = new JButton();
    private final JButton acButton_8 = new JButton();
    private final JButton acButton_9 = new JButton();
    private final JButton acButton_10 = new JButton();
    private final JButton acButton_11 = new JButton();
    private final JButton acButton_12 = new JButton();
    private final JButton acButton_13 = new JButton();
    private final JButton acButton_14 = new JButton();
    private final JButton acButton_15 = new JButton();
    private final JButton acButton_16 = new JButton();
    private final JButton acButton_17 = new JButton();
    private final JButton acButton_18 = new JButton();
    private final JButton acButton_19 = new JButton();
    private final JButton acButton_20 = new JButton();
    private final JButton acButton_21 = new JButton();
    private String b;
    private String a;
    private String c;
    /*
    * Launch the application
    * @param args
    */
    public static void main(String args[]) {
    try {
    jfcalc frame = new jfcalc();
    frame.setVisible(true);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    /**
    * Create the frame
    */
    public jfcalc() {
    super();
    setBounds(100, 100, 500, 375);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    try {
    jbInit();
    } catch (Throwable e) {
    e.printStackTrace();
    }

    }
    private void jbInit() throws Exception {
    getContentPane().setLayout(null);
    setTitle("Calcolatrice");
    getContentPane().setBackground(Color.BLACK);

    getContentPane().add(textField);
    textField.setBounds(139, 22, 263, 20);

    getContentPane().add(acButton);
    acButton.setText("OFF");
    acButton.setBounds(250, 60, 57, 26);

    getContentPane().add(acButton_1);
    acButton_1.addActionListener(new AcButton_1ActionListener());
    acButton_1.setText("CANC");
    acButton_1.setBounds(315, 60, 87, 26);

    getContentPane().add(button);
    button.setBounds(139, 60, 105, 26);

    getContentPane().add(acButton_5);
    acButton_5.addActionListener(new AcButton_5ActionListener());
    acButton_5.setText("/");
    acButton_5.setBounds(298, 115, 41, 26);

    getContentPane().add(acButton_6);
    acButton_6.setText("sqrt");
    acButton_6.setBounds(345, 115, 57, 26);

    getContentPane().add(acButton_2);
    acButton_2.addActionListener(new AcButton_2ActionListener());
    acButton_2.setText("8");
    acButton_2.setBounds(190, 115, 41, 26);

    getContentPane().add(acButton_3);
    acButton_3.addActionListener(new AcButton_3ActionListener());
    acButton_3.setText("9");
    acButton_3.setBounds(240, 115, 41, 26);


    getContentPane().add(acButton_4);
    acButton_4.addActionListener(new AcButton_4ActionListener());
    acButton_4.setText("7");
    acButton_4.setBounds(139, 115, 41, 26);

    getContentPane().add(acButton_7);
    acButton_7.addActionListener(new AcButton_7ActionListener());
    acButton_7.setText("4");
    acButton_7.setBounds(140, 155, 41, 26);

    getContentPane().add(acButton_8);
    acButton_8.addActionListener(new AcButton_8ActionListener());
    acButton_8.setText("5");
    acButton_8.setBounds(190, 155, 41, 26);

    getContentPane().add(acButton_9);
    acButton_9.addActionListener(new AcButton_9ActionListener());
    acButton_9.setText("6");
    acButton_9.setBounds(240, 155, 41, 26);

    getContentPane().add(acButton_10);
    acButton_10.addActionListener(new AcButton_10ActionListener());
    acButton_10.setText("*");
    acButton_10.setBounds(298, 155, 41, 26);

    getContentPane().add(acButton_11);
    acButton_11.setText("%");
    acButton_11.setBounds(345, 155, 57, 26);

    getContentPane().add(acButton_12);
    acButton_12.addActionListener(new AcButton_12ActionListener());
    acButton_12.setText("1");
    acButton_12.setBounds(139, 195, 41, 26);

    getContentPane().add(acButton_13);
    acButton_13.addActionListener(new AcButton_13ActionListener());
    acButton_13.setText("2");
    acButton_13.setBounds(190, 195, 41, 26);

    getContentPane().add(acButton_14);
    acButton_14.addActionListener(new AcButton_14ActionListener());
    acButton_14.setText("3");
    acButton_14.setBounds(240, 195, 41, 26);

    getContentPane().add(acButton_15);
    acButton_15.addActionListener(new AcButton_15ActionListener());
    acButton_15.setText("-");
    acButton_15.setBounds(298, 195, 41, 26);

    getContentPane().add(acButton_16);
    acButton_16.setText("M+");
    acButton_16.setBounds(345, 195, 57, 26);

    getContentPane().add(acButton_17);
    acButton_17.addActionListener(new AcButton_17ActionListener());
    acButton_17.setText("0");
    acButton_17.setBounds(140, 235, 41, 26);

    getContentPane().add(acButton_18);
    acButton_18.setText("+/-");
    acButton_18.setBounds(185, 235, 48, 26);

    getContentPane().add(acButton_19);
    acButton_19.setText(",");
    acButton_19.setBounds(240, 235, 41, 26);

    getContentPane().add(acButton_20);
    acButton_20.addActionListener(new AcButton_20ActionListener());
    acButton_20.setText("+");
    acButton_20.setBounds(298, 235, 41, 26);

    getContentPane().add(acButton_21);
    acButton_21.addActionListener(new AcButton_21ActionListener());
    acButton_21.setText("=");
    acButton_21.setBounds(345, 235, 57, 26);
    }
    private class AcButton_12ActionListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    acButton_12_actionPerformed(e);
    }
    }
    private class AcButton_4ActionListener implements ActionListener {
    public void actionPerformed(ActionEvent e)
    {
    acButton_4_actionPerformed(e);
    }
    }
    private class AcButton_13ActionListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    acButton_13_actionPerformed(e);
    }
    }
    private class AcButton_14ActionListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    acButton_14_actionPerformed(e);
    }
    }
    private class AcButton_17ActionListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    acButton_17_actionPerformed(e);
    }
    }
    private class AcButton_7ActionListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    acButton_7_actionPerformed(e);
    }
    }
    private class AcButton_8ActionListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    acButton_8_actionPerformed(e);
    }
    }
    private class AcButton_9ActionListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    acButton_9_actionPerformed(e);
    }
    }
    private class AcButton_2ActionListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    acButton_2_actionPerformed(e);
    }
    }
    private class AcButton_3ActionListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    acButton_3_actionPerformed(e);
    }
    }
    private class AcButton_1ActionListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    acButton_1_actionPerformed(e);
    }
    }
    private class AcButton_20ActionListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    acButton_20_actionPerformed(e);
    }
    }
    private class AcButton_21ActionListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    acButton_21_actionPerformed(e);
    }
    }
    private class AcButton_15ActionListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    acButton_15_actionPerformed(e);
    }
    }
    private class AcButton_10ActionListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    acButton_10_actionPerformed(e);
    }
    }
    private class AcButton_5ActionListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    acButton_5_actionPerformed(e);
    }
    }
    protected void acButton_12_actionPerformed(ActionEvent e)
    {
    b = textField.getText() + acButton_12.getText();
    jfcalc.this.textField.setText(b);
    }
    protected void acButton_4_actionPerformed(ActionEvent e)
    {
    b = textField.getText() + acButton_4.getText();
    jfcalc.this.textField.setText(b);
    }
    protected void acButton_13_actionPerformed(ActionEvent e)
    {
    b = textField.getText() + acButton_13.getText();
    jfcalc.this.textField.setText(b);
    }
    protected void acButton_14_actionPerformed(ActionEvent e)
    {
    b = textField.getText() + acButton_14.getText();
    jfcalc.this.textField.setText(b);
    }
    protected void acButton_17_actionPerformed(ActionEvent e)
    {
    b = textField.getText() + acButton_17.getText();
    jfcalc.this.textField.setText(b);
    }
    protected void acButton_7_actionPerformed(ActionEvent e)
    {
    b = textField.getText() + acButton_7.getText();
    jfcalc.this.textField.setText(b);
    }
    protected void acButton_8_actionPerformed(ActionEvent e)
    {
    b = textField.getText() + acButton_8.getText();
    jfcalc.this.textField.setText(b);
    }
    protected void acButton_9_actionPerformed(ActionEvent e)
    {
    b = textField.getText() + acButton_9.getText();
    jfcalc.this.textField.setText(b);
    }
    protected void acButton_2_actionPerformed(ActionEvent e)
    {
    b = textField.getText() + acButton_2.getText();
    jfcalc.this.textField.setText(b);
    }
    protected void acButton_3_actionPerformed(ActionEvent e)
    {
    b = textField.getText() + acButton_3.getText();
    jfcalc.this.textField.setText(b);
    }
    protected void acButton_1_actionPerformed(ActionEvent e)
    {
    jfcalc.this.textField.setText("");
    }
    protected void acButton_20_actionPerformed(ActionEvent e)
    {
    a = jfcalc.this.textField.getText();
    jfcalc.this.textField.setText("");
    c = acButton_20.getText();
    }
    protected void acButton_15_actionPerformed(ActionEvent e)
    {
    a = jfcalc.this.textField.getText();
    jfcalc.this.textField.setText("");
    c = acButton_15.getText();
    }
    protected void acButton_10_actionPerformed(ActionEvent e)
    {
    a = jfcalc.this.textField.getText();
    jfcalc.this.textField.setText("");
    c = acButton_10.getText();
    }
    protected void acButton_5_actionPerformed(ActionEvent e)
    {
    a = jfcalc.this.textField.getText();
    jfcalc.this.textField.setText("");
    c = acButton_5.getText();
    }
    protected void acButton_21_actionPerformed(ActionEvent e)
    {
    if(c=="+")
    {
    int conva = Integer.parseInt(a);
    int convtext = Integer.parseInt(textField.getText());
    conva = conva + convtext;
    String z = String.valueOf(conva);
    jfcalc.this.textField.setText(z);
    }
    else if(c=="-")
    {
    int conva = Integer.parseInt(a);
    int convtext = Integer.parseInt(textField.getText());
    conva = conva - convtext;
    String z = String.valueOf(conva);
    jfcalc.this.textField.setText(z);
    }
    else if(c=="*")
    {
    int conva = Integer.parseInt(a);
    int convtext = Integer.parseInt(textField.getText());
    conva = conva * convtext;
    String z = String.valueOf(conva);
    jfcalc.this.textField.setText(z);
    }
    else if(c=="/")
    {
    float conva = Integer.parseInt(a);
    int convtext = Integer.parseInt(textField.getText());
    conva = conva / convtext;
    String z = String.valueOf(conva);
    jfcalc.this.textField.setText(z);
    }
    }





    }

  2. #2
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284

    Re: pulsante esci

    Originariamente inviato da jalexb
    non riesco a far funzionare il pulsante "OFF"
    Stando al codice, risulta che per 'acButton' (il pulsante con testo "OFF") non hai registrato alcun listener.

    Come consigli: cerca in futuro di usare il più possibile i layout manager. Inoltre avere una vagonata di inner class e una altrettanto vagonata di metodi protected per gli actionPerformed specifici (che poi sono richiamati dai "veri" actionPerformed dei listener) rende solo lungo e poco comprensibile il codice.
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  3. #3
    Utente di HTML.it
    Registrato dal
    Mar 2008
    Messaggi
    137
    purtroppo JBuilder 2008 le mette in automatico......cmq, come posso fare un pulsante esci?

  4. #4
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Originariamente inviato da jalexb
    purtroppo JBuilder 2008 le mette in automatico......
    Allora hai già sicuramente capito cosa vuol dire farsi generare il codice da un ambiente di sviluppo: otterrai codice complesso, prolisso, poco comprensibile e che è difficile (o quasi impossibile) da gestire manualmente.

    Originariamente inviato da jalexb
    cmq, come posso fare un pulsante esci?
    Beh, scusa, come hai fatto con gli altri pulsanti? Intendo dire ... vedo che gli altri hanno un listener. Fai la stessa cosa anche con quel 'acButton'!

    Volevi fare una calcolatrice? La stessa cosa si può fare in meno della metà del codice che hai scritto.
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.