14 April 2016

Button Swing Eclipse in Java multiplication and add

Example: Button multiplication and add Eclipse in Java
package swing;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;

import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import java.awt.Font;

public class swingapp {

 private JFrame frame;
 private final ButtonGroup buttonGroup = new ButtonGroup();
 private JTextField textFielda;
 private JTextField textFieldb;
 private JTextField textFieldc;

 /**
  * Launch the application.
  */
 public static void main(String[] args) {
  EventQueue.invokeLater(new Runnable() {
   public void run() {
    try {
     swingapp window = new swingapp();
     window.frame.setVisible(true);
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
  });
 }

 /**
  * Create the application.
  */
 public swingapp() {
  initialize();
 }

 /**
  * Initialize the contents of the frame.
  */
 private void initialize() {
  frame = new JFrame();
  frame.setBounds(100, 100, 258, 173);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.getContentPane().setLayout(null);
  
  textFielda = new JTextField();
  textFielda.setBounds(10, 11, 86, 20);
  frame.getContentPane().add(textFielda);
  textFielda.setColumns(10);
  
  textFieldb = new JTextField();
  textFieldb.setBounds(10, 52, 86, 20);
  frame.getContentPane().add(textFieldb);
  textFieldb.setColumns(10);
  
  textFieldc = new JTextField();
  textFieldc.setBounds(10, 90, 86, 20);
  frame.getContentPane().add(textFieldc);
  textFieldc.setColumns(10);
  
  JButton btnAdd = new JButton("ADD");
  btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { int num1, num2, ans; try { num1= Integer.parseInt(textFielda.getText()); num2= Integer.parseInt(textFieldb.getText()); ans =num1+ num2; textFieldc.setText(Integer.toString(ans)); } catch (Exception e1) { // TODO: handle exception JOptionPane.showMessageDialog(null, "Please enter valid number!"); } } }); btnAdd.setBounds(119, 11, 89, 23); frame.getContentPane().add(btnAdd); JButton btnMulti = new JButton("Multi"); btnMulti.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { int num1, num2, ans; try { num1= Integer.parseInt(textFielda.getText()); num2= Integer.parseInt(textFieldb.getText()); ans =num1* num2; textFieldc.setText(Integer.toString(ans)); } catch (Exception e1) { // TODO: handle exception JOptionPane.showMessageDialog(null, "Please enter valid number!"); } } }); btnMulti.setBounds(119, 51, 89, 23); frame.getContentPane().add(btnMulti); JLabel lblDesignByMr = new JLabel("Design by Mr.24!"); lblDesignByMr.setFont(new Font("Papyrus", Font.PLAIN, 13)); lblDesignByMr.setBounds(119, 90, 103, 20); frame.getContentPane().add(lblDesignByMr); } }

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang