Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //creating a new JFrame
- 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[5] buttons = new JButton[5];
- //for loop for making buttons.. (does this even work?, i dont think my action in the for loop is right..)
- int[5] buttonNames = {"1","2","3","4","5"};
- for(i=0; i<=buttons.length; i++){
- JButton button[i] = new JButton(names[i])
- }
- //new action listener
- ActionListener listener = new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- JButton source = 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
- GridBagContraints c = new GridBagContraints();
- for(i=0; i<=buttons.length; i++){
- panel.add(buttons[i], c)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement