Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***************************************/
- /** 20150924 Swing **/
- /** Maker : Yan **/
- /** E-Mail : [email protected] **/
- /** Date : 2015/09/24 am.11:30 **/
- /***************************************/
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class Source {
- public static void main(String[] args) {
- JFrame demo = new JFrame();
- demo.setSize(400, 300);
- demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- demo.getContentPane().setLayout(new FlowLayout());
- // Add Combobox
- JComboBox combo = new JComboBox();
- combo.addItem("50");
- combo.addItem("100");
- combo.addItem("150");
- combo.addItem("200");
- demo.getContentPane().add(BorderLayout.SOUTH, combo);
- // Add Button
- JButton button = new JButton("Sum");
- demo.getContentPane().add(BorderLayout.SOUTH, button);
- // Add Label
- JLabel label = new JLabel("-------------");
- demo.getContentPane().add(BorderLayout.SOUTH, label);
- button.addActionListener(new buttonHandler(label, combo));
- demo.setVisible(true);
- }
- }
- class buttonHandler implements ActionListener{
- private JLabel label;
- private JComboBox combo;
- public buttonHandler(JLabel label, JComboBox combo) {
- this.label = label;
- this.combo = combo;
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
- int sum = 0;
- for (int i = 1; i <= Integer.parseInt(String.valueOf(combo.getSelectedItem())); i++) {
- sum += i;
- }
- label.setText("Sum(1-" + String.valueOf(combo.getSelectedItem()) + ") = " + sum);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment