darklordnorge

Frame with JTabbedPane

Mar 7th, 2012
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.19 KB | None | 0 0
  1. package project;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.util.*;
  6. import javax.swing.*;
  7.  
  8. /**
  9.  *
  10.  * @author stefan Klaus
  11.  * @version 1.0
  12.  *
  13.  */
  14.      
  15.     public class NewClass extends JFrame implements ActionListener {
  16.        
  17.         private JFrame frame;
  18.         private JTabbedPane tabbedPane;
  19.         private JPanel nameTab, namePanel, instantTab, instantPanel, methodTab, methodPanel;
  20.         private JLabel nameLabel;
  21.         private GridLayout nameGrid, instantGrid, methodGrid;
  22.         private JTextField nameInput, instantInput, methodInput, methodParameter;
  23.         private JComboBox fieldModifier, fieldType, methodModifier, methodType;
  24.         private JButton addName, addInstant, addMethod, doneButton;
  25.         private JTextArea instantArea, methodArea;
  26.         private InstantTabListener instantListener;
  27.         private DoneButtonListener doneButtonListener;
  28.         private MethodTabListener methodListener;
  29.         private NameTabListener nameListener;
  30.         private ArrayList<String> instantArray, methodArray, nameArray;
  31.        
  32.         public void actionPerformed(ActionEvent e) {
  33.             instantArray = new ArrayList<String>();
  34.             methodArray = new ArrayList<String>();
  35.            
  36.            
  37.             //create the Frame
  38.             frame = new JFrame();
  39.             doneButton = new JButton("Done");
  40.             frame.setSize(500, 400);
  41.             frame.setResizable(false);
  42.             frame.setVisible(true);
  43.             frame.setTitle("Create a new Class");
  44.             tabbedPane = new JTabbedPane();
  45.             frame.add(tabbedPane, BorderLayout.CENTER);
  46.             frame.add(doneButton, BorderLayout.SOUTH);
  47.            
  48.             //actionLitener
  49.             doneButtonListener = new DoneButtonListener();
  50.             doneButton.addActionListener(doneButtonListener);
  51.         //##########################################################################################   
  52.             //creating the namePanel
  53.             nameTab = new JPanel();
  54.             namePanel = new JPanel();
  55.             nameGrid = new GridLayout(0, 2);
  56.             nameLabel = new JLabel("Name of Class: ");
  57.             nameInput = new JTextField();  
  58.             addName = new JButton("Add");
  59.            
  60.             //Instant Listener
  61.             nameListener = new NameTabListener();
  62.             addName.addActionListener(nameListener);
  63.            
  64.             //add Stuff ti panel
  65.             namePanel.add(nameLabel);
  66.             namePanel.add(nameInput);
  67.             namePanel.setLayout(nameGrid);
  68.            
  69.             nameTab.add(namePanel, BorderLayout.NORTH);
  70.             nameTab.add(addName, BorderLayout.CENTER);
  71.     //#########################################################################################################    
  72.             //creating the InstantPanel
  73.             instantTab = new JPanel();
  74.             instantPanel = new JPanel();
  75.             instantGrid = new GridLayout(1, 4);
  76.             instantInput = new JTextField();
  77.             addInstant = new JButton("Add");
  78.             instantArea = new JTextArea(10, 30);
  79.                    
  80.             instantInput.setToolTipText("Name of Instant Variable");
  81.             instantPanel.setLayout(instantGrid);
  82.             instantArea.setEditable(false);
  83.            
  84.             //Array of Strings for JComboBox
  85.             String[] modifiers = {"private", "public", "protected"};
  86.             fieldModifier = new JComboBox(modifiers);
  87.            
  88.             String[] types = {"int", "String", "double", "boolean", "char", "byte","short","long","float"};
  89.             fieldType = new JComboBox(types);
  90.            
  91.             //actionListener
  92.             instantListener = new InstantTabListener();
  93.             addInstant.addActionListener(instantListener);
  94.            
  95.            
  96.             //add stuff to panel
  97.             instantPanel.add(fieldModifier);
  98.             instantPanel.add(fieldType);
  99.             instantPanel.add(instantInput);
  100.             instantPanel.add(addInstant);
  101.            
  102.             instantTab.add(instantPanel, BorderLayout.NORTH);      
  103.             instantTab.add(instantArea, BorderLayout.CENTER);
  104.            
  105.             //#########################################################################################
  106.             //creating the method tab
  107.             methodTab = new JPanel();
  108.             methodPanel = new JPanel();
  109.             methodGrid = new GridLayout(1, 5);
  110.             methodInput = new JTextField();
  111.             methodParameter = new JTextField();
  112.             methodArea = new JTextArea(10, 30);
  113.             addMethod = new JButton("Add");
  114.            
  115.             methodInput.setToolTipText("Method Name");
  116.             methodParameter.setToolTipText("Method Parameter");
  117.             methodPanel.setLayout(methodGrid);
  118.             methodArea.setEditable(false);
  119.            
  120.             //Array of Strings for JComboBox
  121.             String[] methodmodifier = {"private", "public", "none"};
  122.             methodModifier = new JComboBox(methodmodifier);
  123.            
  124.             String[] methodtype = {"void", "String", "int"};
  125.             methodType = new JComboBox(methodtype);
  126.            
  127.             //ActionListener
  128.             methodListener = new MethodTabListener();
  129.             addMethod.addActionListener(methodListener);
  130.            
  131.             //add stuff to panel
  132.             methodPanel.add(methodModifier);
  133.             methodPanel.add(methodType);
  134.             methodPanel.add(methodInput);
  135.             methodPanel.add(methodParameter);
  136.             methodPanel.add(addMethod);
  137.            
  138.             methodTab.add(methodPanel, BorderLayout.NORTH);
  139.             methodTab.add(methodArea, BorderLayout.CENTER);
  140.             //#######################################################################
  141.            
  142.             //adding panel's to the tabbedPane
  143.            
  144.             tabbedPane.add("Name", nameTab);
  145.             tabbedPane.add("Fields", instantTab);
  146.             tabbedPane.add("Methods", methodTab);
  147.             tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);      
  148.         }
  149.         class InstantTabListener implements ActionListener{
  150.                 String modifier = null;
  151.                 String type = null;
  152.                 String name = null;
  153.                 String array = null;
  154.             public void actionPerformed(ActionEvent e) {
  155.            
  156.                 if(fieldModifier.getSelectedIndex() == 0){
  157.                      modifier = "-";
  158.                 }else if(fieldModifier.getSelectedIndex() == 1){
  159.                      modifier = "+";
  160.                 }else if(fieldModifier.getSelectedIndex() == 2){
  161.                      modifier = "#";
  162.                 }
  163.                 //////////////////////////
  164.                 if(fieldType.getSelectedIndex() == 0){
  165.                     type = "int";
  166.                 }else if(fieldType.getSelectedIndex() == 1){
  167.                     type = "String";
  168.                 }else if(fieldType.getSelectedIndex() == 2){
  169.                     type = "double";
  170.                 }else if(fieldType.getSelectedIndex() == 3){
  171.                     type = "boolean";
  172.                 }else if(fieldType.getSelectedIndex() == 4){
  173.                     type = "char";
  174.                 }else if(fieldType.getSelectedIndex() == 5){
  175.                     type = "byte";
  176.                 }else if(fieldType.getSelectedIndex() == 6){
  177.                     type = "short";
  178.                 }else if(fieldType.getSelectedIndex() == 7){
  179.                     type = "long";
  180.                 }else if(fieldType.getSelectedIndex() == 8){
  181.                     type = "float";
  182.                 }
  183.                 /////////////////////
  184.            
  185.                 name = instantInput.getText();
  186.                 array = modifier+" "+type+" " +name;
  187.                 instantArray.add(array);
  188.                 instantArea.append(array+"\n ");
  189.                 instantInput.setText("");
  190.            
  191.             }
  192.         }
  193.         //#############################################################################################
  194.         class MethodTabListener implements ActionListener{
  195.                 String array = null;
  196.                 String type = null;
  197.                 String modifier = null;
  198.                 String name = null;
  199.                 String parameter = null;
  200.             public void actionPerformed(ActionEvent arg0) {
  201.                 if(methodModifier.getSelectedIndex() == 0){
  202.                     modifier = "private";
  203.                 }else if(methodModifier.getSelectedIndex() == 1){
  204.                     modifier = "public";
  205.                 }else if(methodModifier.getSelectedIndex() == 2){
  206.                     modifier = " ";
  207.                 }
  208.                 /////////////////////
  209.                 if(methodType.getSelectedIndex() == 0){
  210.                     type = "void";
  211.                 }
  212.                 else if(methodType.getSelectedIndex() == 1){
  213.                     type = "String";
  214.                 }
  215.                 else if(methodType.getSelectedIndex() == 2){
  216.                     type = "int";
  217.                 }
  218.                 ///////////////
  219.                
  220.                 name = methodInput.getText();
  221.                 parameter = methodParameter.getText();
  222.                 array = modifier+" "+type+" "+name+"( "+parameter+" )";
  223.                 methodArray.add(array);
  224.                 methodArea.append(array+"\n");
  225.                 methodInput.setText("");
  226.                 methodParameter.setText("");
  227.                
  228.             }
  229.            
  230.         }
  231.         //#################################################################################################
  232.         class NameTabListener implements ActionListener{
  233.                 String name = null;
  234.             public void actionPerformed(ActionEvent arg0) {
  235.                
  236.                 name = nameInput.getText();
  237.                 nameArray = new ArrayList<String>();
  238.                 nameArray.add(name);
  239.                 nameInput.setText("Name Set!");
  240.             }
  241.            
  242.         }
  243.         //#################################################################################################
  244.         class DoneButtonListener implements ActionListener{
  245.  
  246.             public void actionPerformed(ActionEvent arg0) {
  247.                 System.out.println("Name is: "+nameArray );
  248.                 System.out.println("Instants are: "+instantArray);
  249.                 System.out.println("Methods are: "+methodArray);
  250.             }
  251.            
  252.         }
  253.  
  254.     }
Advertisement
Add Comment
Please, Sign In to add comment