Question

I have homework with Java GUI, and i'm a new with java programming. Anyone can help my problems ?.

my screenshot : http://cdn.kaskus.com/images/2014/03/21/2556566_20140321011737.png

If I press the "+" button, the Textfield will show numbers 0,1,2,3,4,5,6,... and if I press "-" button, the textfield will decrease the numbers properly, but not below 0. Here is my source code:

    package Main.Code;

public class NewJFrame extends javax.swing.JFrame {

    public NewJFrame() {
        initComponents();
    }

    @SuppressWarnings("unchecked")                         
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("-");

        jButton2.setText("+");

        jTextField1.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jTextField1.setText("0");
        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(68, 68, 68)
                .addComponent(jButton1)
                .addGap(18, 18, 18)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton2)
                .addContainerGap(68, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(101, 101, 101)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2)
                    .addComponent(jButton1))
                .addContainerGap(80, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
    }                                           

    public static void main(String args[]) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }

    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JTextField jTextField1;                  
}
Was it helpful?

Solution

Try This :

      class NewJFrame extends javax.swing.JFrame implements ActionListener {
      int total=0;
      /*...
      ....
      code ...
     */
    jButton1.setText("-");
    jButton2.setText("+");
    jButton1.addActionListener(this);
    jButton2.addActionListener(this);

   }

Button Action Event Here :

   public  void  actionPerformed(ActionEvent e)
    {
        if(e.getSource()==jButton2)
                 { 
                    total +=1;
                    jTextField1.setText(""+total);
                 }     
        if(e.getSource()==jButton1)
                 { 
                    total -=1;
                    jTextField1.setText(""+total);
                 }  
      } 

OTHER TIPS

You need to supply an ActionListener for each button, which will perform the required task (add/subtract).

Take a look at How to Use Buttons, Check Boxes, and Radio Buttons and How to Write an Action Listener for more details.

Each time your actionPerformed method is called, you will need to get the value from the text field (jTextField1.getText()) and parse it to an int value, which can be achieved by using Integer#parseInt.

You then need to apply your modifier and set the value back to the text field (jTextField1.setText(String)). Not that setText takes a String and not an int. You will need to convert it first, using something like Integer.toString(int) for example...

Take a look at How to Use Text Fields for more details about JTextFields

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top