Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package jframe;
- import java.awt.GridBagConstraints;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- //creating a new JFrame
- public class FrameTest{
- static int i;
- JButton buttons;
- public static void main(String args[]){
- JFrame frame = new JFrame("Test frame");
- //bunch of frame stuff
- frame.setSize(500,500);
- frame.setVisible(true);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- //adding panel to the frame
- JPanel panel = new JPanel();
- frame.getContentPane().add(panel);
- panel.setLayout(new GridLayout(0,5));
- panel.setVisible(true);
- //rough button code..
- JButton[] buttons = new JButton[5];
- //for loop for making buttons
- for(int i=0; i<=buttons.length; i++){
- JButton[] button = new JButton[i];
- }
- //new action listener
- ActionListener listener = new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- JButton source = (JButton) e.getSource();
- if(source.getText().equals("button text")) {
- //stuff to be done
- }
- }
- };
- //adding the listener to buttons
- for(i=0; i<=buttons.length; i++){
- buttons[i].addActionListener(listener);
- }
- //making a grid bag layout to put buttons in
- GridBagConstraints c = new GridBagConstraints();
- //adding buttons to the grid layout
- for(i=0; i<=buttons.length; i++){
- panel.add(buttons[i], c);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement