Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. package swingapplication;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5.  
  6. public class RozkladyAkcja implements ActionListener {
  7.  
  8. /** Creates a new instance of RozkladyAkcja */
  9. RozkladyAkcja() {
  10. String []layoutNamesTab = { "Flow Layout", "Grid Layout", "Border Layout"};
  11. LayoutManager []rozkladyTab = { new FlowLayout(30), new GridLayout(3,4), new BorderLayout() };
  12. JFrame okno = new JFrame("Okno w Swing");
  13. okno.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  14. Container kontener = okno.getContentPane();
  15. kontener.setLayout(new GridLayout(2,5));
  16. for (int i=0; i<layoutNamesTab.length-1; i++) {
  17. Panel p = new Panel();
  18. p.setLayout(rozkladyTab[i]);
  19. for (int j=0; j<5; j++) {
  20. JButton jb = new JButton("przycisk "+(j+1));
  21. jb.addActionListener(new SetFont());
  22. p.add(jb);
  23. }
  24. kontener.add(p);
  25. }
  26.  
  27. //Border layout
  28. String []kierunki = { "West", "North", "East", "South", "Center"};
  29. JPanel p = new JPanel();
  30. p.setLayout(rozkladyTab[rozkladyTab.length-1]);
  31. for (int j=0; j<5; j++) {
  32. JButton jb = new JButton("przycisk "+(j+1));
  33. jb.addActionListener(this);
  34. jb.setToolTipText("AAAAAAAAAAAA");
  35. p.add(jb,kierunki[j]);
  36. }
  37. kontener.add(p);
  38.  
  39. JPanel jp = new JPanel(new FlowLayout(FlowLayout.TRAILING,30,0));
  40. JButton jbutton = new JButton("Przycisk na JPanel'u");
  41. jbutton.addActionListener(this);
  42. jp.add(jbutton);
  43. kontener.add(jp);
  44.  
  45. okno.pack();
  46. okno.setVisible(true);
  47.  
  48. }
  49.  
  50. public void actionPerformed(ActionEvent ae) {
  51. JComponent jc = (JComponent)ae.getSource();
  52. jc.setToolTipText(ae.getActionCommand());
  53. if (ae.getActionCommand().equals("Przycisk na JPanel'u"))
  54. JOptionPane.showMessageDialog(jc.getParent(),jc.getToolTipText(),
  55. "Podpowiedz na przycisku!",JOptionPane.INFORMATION_MESSAGE);
  56.  
  57. }
  58.  
  59.  
  60. }
  61. class SetFont implements ActionListener {
  62. public void actionPerformed(ActionEvent ae) {
  63. JComponent jc = (JComponent)ae.getSource();
  64. jc.setFont(new Font("DialogInput", Font.BOLD, 16));
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement