Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Feb 9th, 2010 | Syntax: Java | Size: 2.66 KB | Hits: 35 | Expires: Never
Copy text to clipboard
  1. import java.awt.*;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import java.util.Enumeration;
  5. import java.util.regex.Matcher;
  6. import java.util.regex.Pattern;
  7.  
  8. import javax.swing.*;
  9.  
  10. public class Menu extends JFrame{
  11.         private static final int WIDTH=300;
  12.         private static final int HEIGHT=300;
  13.         private JRadioButton searchRB=new JRadioButton("Search");
  14.         private JRadioButton sellRB=new JRadioButton("Sell");
  15.         private JRadioButton printRB=new JRadioButton("Print");
  16.         public static ButtonGroup group=new ButtonGroup();
  17.         //public static String action="";
  18.                
  19.         public Menu(){
  20.                 setupWindow();
  21.                 Container pane=getContentPane();
  22.                 pane.setLayout(null);
  23.                 pane.add(radioButtons()).setBounds(0,10,200,25);
  24.                
  25.                 JButton submitB=new JButton("Submit");
  26.                 SubmitButton sb=new SubmitButton();
  27.                 submitB.addActionListener(sb);
  28.                 pane.add(submitB).setBounds(90,60,75,30);
  29.         }
  30.        
  31.         private JComponent radioButtons(){
  32.                 group.add(searchRB);
  33.                 group.add(sellRB);
  34.                 group.add(printRB);
  35.                 JPanel pane=new JPanel();
  36.                 pane.add(searchRB);
  37.                 pane.add(sellRB);
  38.                 pane.add(printRB);
  39.  
  40.                 return pane;
  41.         }
  42.        
  43.         private void setupWindow(){
  44.                 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  45.                 setResizable(false);
  46.                 setVisible(true);
  47.                 setSize(WIDTH, HEIGHT);
  48.                 setTitle("Inventory");
  49.         }
  50.        
  51.         public static void main(String args[]){
  52.                 new Menu();
  53.         }
  54. }
  55.  
  56. class SubmitButton implements ActionListener{
  57.  
  58.         @Override
  59.         public void actionPerformed(ActionEvent e) {
  60.                 String action=parseSelection(getSelection(Menu.group));
  61.                
  62.                 if(action.equals("Sell"))
  63.                         new Sell();
  64.                 if(action.equals("Print"))
  65.                         new Print();
  66.                 if(action.equals("Search"))
  67.                         new Search();
  68.         }
  69.        
  70.         private String parseSelection(JRadioButton button){
  71.                 String regex=".*?text=(.*?)\\]";
  72.                 Pattern p=Pattern.compile(regex);
  73.                 Matcher m=p.matcher(button.toString());
  74.                 String result="";
  75.                
  76.                 if(m.find())
  77.                         result=m.group(1);
  78.                 return result;
  79.         }
  80.        
  81.         private static JRadioButton getSelection(ButtonGroup group) {
  82.                 for (Enumeration e=group.getElements(); e.hasMoreElements(); ){
  83.                         JRadioButton b = (JRadioButton)e.nextElement();
  84.                         if (b.getModel() == group.getSelection())
  85.                                 return b;
  86.                 }
  87.                 return null;
  88.         }
  89.  
  90. }
  91.  
  92. /*****************
  93. Sell.java
  94. ******************/
  95. public class Sell {
  96.         public Sell(){
  97.                 System.out.println("Sell");
  98.         }
  99. }
  100. /*****************
  101. Print.java
  102. ******************/
  103. public class Print {
  104.         public Print(){
  105.                 System.out.println("Print");
  106.         }
  107. }
  108. /*****************
  109. Search.java
  110. ******************/
  111. public class Search {
  112.         public Search(){
  113.                 System.out.println("Search");
  114.         }
  115. }
  116. /*****************
  117. Sample Output
  118. ******************/
  119. Sell
  120. Print
  121. Search