Holey_yan

[Java] 20150924_Swing

Sep 30th, 2015
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.79 KB | None | 0 0
  1. /***************************************/
  2. /** 20150924 Swing                    **/
  3. /** Maker  : Yan                      **/
  4. /** E-Mail : [email protected] **/
  5. /** Date   : 2015/09/24 am.11:30      **/
  6. /***************************************/
  7.  
  8. import java.awt.*;
  9. import java.awt.event.*;
  10. import javax.swing.*;
  11.  
  12. public class Source {
  13.  
  14.     public static void main(String[] args) {
  15.         JFrame demo = new JFrame();
  16.         demo.setSize(400, 300);
  17.         demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  18.         demo.getContentPane().setLayout(new FlowLayout());
  19.  
  20.         // Add Combobox
  21.         JComboBox combo = new JComboBox();
  22.         combo.addItem("50");
  23.         combo.addItem("100");
  24.         combo.addItem("150");
  25.         combo.addItem("200");
  26.         demo.getContentPane().add(BorderLayout.SOUTH, combo);
  27.  
  28.         // Add Button
  29.         JButton button = new JButton("Sum");
  30.         demo.getContentPane().add(BorderLayout.SOUTH, button);
  31.  
  32.         // Add Label
  33.         JLabel label = new JLabel("-------------");
  34.         demo.getContentPane().add(BorderLayout.SOUTH, label);
  35.  
  36.  
  37.         button.addActionListener(new buttonHandler(label, combo));
  38.         demo.setVisible(true);
  39.     }
  40. }
  41.  
  42. class buttonHandler implements ActionListener{
  43.     private JLabel label;
  44.     private JComboBox combo;
  45.     public buttonHandler(JLabel label, JComboBox combo) {
  46.         this.label = label;
  47.         this.combo = combo;
  48.     }
  49.     @Override
  50.     public void actionPerformed(ActionEvent e) {
  51.         // TODO Auto-generated method stub
  52.         int sum = 0;
  53.         for (int i = 1; i <= Integer.parseInt(String.valueOf(combo.getSelectedItem())); i++) {
  54.             sum += i;
  55.         }
  56.         label.setText("Sum(1-" + String.valueOf(combo.getSelectedItem()) + ") = " + sum);
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment