Advertisement
Guest User

Creating New Components In Swing

a guest
Apr 3rd, 2013
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.72 KB | None | 0 0
  1.  
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.WindowAdapter;
  7. import java.awt.event.WindowEvent;
  8.  
  9. /**
  10.  * "Code early, Code often."
  11.  */
  12. public class SimpleFrame extends JFrame {
  13.  
  14.     int frameCount = 1;
  15.  
  16.     public SimpleFrame(){
  17.         super("SimpleFrame");
  18.  
  19.         final JPanel centerPanel = new JPanel();//this panel will hold the new components we create.
  20.  
  21.         final JPanel topPanel = new JPanel();
  22.         final String[] choices = new String[]{"Label", "Button", "Frame"};
  23.         final JComboBox comboBox = new JComboBox(choices);
  24.         final JButton addComponentButton = new JButton("Add");
  25.         addComponentButton.addActionListener(new ActionListener(){
  26.             public void actionPerformed(ActionEvent e) {
  27.                 //When the button addComponentButton is pressed, this code will execute.
  28.                 final Object selectedItem = comboBox.getSelectedItem();     // See what's selected in the combobox.
  29.                 if(null != selectedItem){
  30.                     if("Label".equals(selectedItem)){
  31.                         centerPanel.add(new JLabel("This is a label"));
  32.                     }else if("Button".equals(selectedItem)){
  33.                         centerPanel.add(new JButton("This is a Button"));
  34.                     }else if("Frame".equals(selectedItem)){
  35.                         JFrame newFrame = new JFrame("This is Frame#"+(frameCount));
  36.                         frameCount++;
  37.                         newFrame.setLocation(frameCount*20, frameCount*20);
  38.                         newFrame.setSize(new Dimension(100,100));
  39.                         newFrame.setVisible(true);
  40.                     }else{
  41.                         JOptionPane.showMessageDialog(null, "Button was pressed, but nothing was selected!");
  42.                     }
  43.                     topPanel.revalidate();
  44.                 }
  45.             }
  46.         });
  47.  
  48.  
  49.         //add our combobox and button to the top panel
  50.         topPanel.add(comboBox);
  51.         topPanel.add(addComponentButton);
  52.  
  53.  
  54.         //now we'll add our two panels to the frame's content pane, and we're done.
  55.         getContentPane().setLayout(new BorderLayout());
  56.         getContentPane().add(topPanel, BorderLayout.NORTH);
  57.         getContentPane().add(centerPanel, BorderLayout.CENTER);
  58.  
  59.     }
  60.  
  61.  
  62.     public static void main(String[] args) {
  63.         SimpleFrame sf = new SimpleFrame();
  64.         sf.setSize(new Dimension(800,600));
  65.         sf.setVisible(true);
  66.         sf.addWindowListener(new WindowAdapter() {
  67.             @Override
  68.             public void windowClosed(WindowEvent e) {
  69.                 System.exit(1);
  70.             }
  71.         });
  72.     }
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement