Advertisement
devankransteuber

Untitled

Mar 2nd, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.56 KB | None | 0 0
  1. package jframe;
  2.  
  3. import java.awt.GridBagConstraints;
  4. import java.awt.GridLayout;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7.  
  8. import javax.swing.JButton;
  9. import javax.swing.JFrame;
  10. import javax.swing.JPanel;
  11.  
  12. //creating a new JFrame
  13.  
  14. public class FrameTest{
  15.  
  16.     static int i;
  17.     JButton buttons;
  18.    
  19.     public static void main(String args[]){
  20.    
  21.     JFrame frame = new JFrame("Test frame");
  22.    
  23.     //bunch of frame stuff
  24.  
  25.     frame.setSize(500,500);
  26.     frame.setVisible(true);
  27.     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  28.     //adding panel to the frame
  29.     JPanel panel = new JPanel();
  30.     frame.getContentPane().add(panel);
  31.     panel.setLayout(new GridLayout(0,5));
  32.     panel.setVisible(true);
  33.  
  34. //rough button code..
  35.         JButton[] buttons = new JButton[5];
  36. //for loop for making buttons
  37.  
  38.  
  39.  
  40.     for(int i=0; i<=buttons.length; i++){
  41.         JButton[] button = new JButton[i];  
  42.     }
  43.  
  44.  
  45. //new action listener
  46.     ActionListener listener = new ActionListener() {
  47.  
  48.     public void actionPerformed(ActionEvent e) {
  49.    
  50.     JButton source = (JButton) e.getSource();
  51.  
  52.     if(source.getText().equals("button text")) {
  53.         //stuff to be done
  54.     }
  55.     }
  56.     };
  57.  
  58.    
  59. //adding the listener to buttons
  60. for(i=0; i<=buttons.length; i++){
  61.     buttons[i].addActionListener(listener);
  62. }
  63.  
  64. //making a grid bag layout to put buttons in
  65. GridBagConstraints c = new GridBagConstraints();
  66.  
  67. //adding buttons to the grid layout
  68. for(i=0; i<=buttons.length; i++){
  69.     panel.add(buttons[i], c);
  70. }
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement