Advertisement
devankransteuber

Untitled

Mar 2nd, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. //creating a new JFrame
  2.  
  3. JFrame frame = new JFrame("Test frame");
  4.  
  5. //bunch of frame stuff
  6.  
  7. frame.setSize(500,500);
  8. frame.setVisible(true);
  9. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  10. //adding panel to the frame
  11. JPanel panel = new JPanel;
  12. frame.getContentPane().add(panel);
  13. panel.setLayout(new GridLayout(0,5);
  14. panel.setVisible(true);
  15.  
  16. //rough button code..
  17. JButton[5] buttons = new JButton[5];
  18. //for loop for making buttons.. (does this even work?, i dont think my action in the for loop is right..)
  19.  
  20. int[5] buttonNames = {"1","2","3","4","5"};
  21.  
  22. for(i=0; i<=buttons.length; i++){
  23. JButton button[i] = new JButton(names[i])
  24. }
  25.  
  26.  
  27. //new action listener
  28. ActionListener listener = new ActionListener() {
  29.  
  30. public void actionPerformed(ActionEvent e) {
  31.  
  32. JButton source = e.getSource();
  33.  
  34. if(source.getText().equals("button text"){
  35. //stuff to be done
  36. }
  37. }
  38. }
  39.  
  40. //adding the listener to buttons
  41. for(i=0; i<=buttons.length; i++){
  42. buttons[i].addActionListener(listener)
  43. }
  44.  
  45. //making a grid bag layout to put buttons in
  46. GridBagContraints c = new GridBagContraints();
  47.  
  48. for(i=0; i<=buttons.length; i++){
  49. panel.add(buttons[i], c)
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement