Guest User

Untitled

a guest
Jul 18th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3.  
  4. public class PanelDemo extends JFrame implements ActionListener {
  5. public static final int WIDTH = 300;
  6. public static final int HEIGHT = 200;
  7.  
  8. private JPanel bluePanel;
  9. private JPanel whitePanel;
  10. private JPanel grayPanel;
  11.  
  12. public static void main(String[] args) {
  13. PanelDemo gui = new PanelDemo();
  14. gui.setVisible(true);
  15. }
  16.  
  17. public PanelDemo() {
  18. super("Panel Demostration");
  19. setSize(WIDTH, HEIGHT);
  20. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  21. setLayout(new BorderLayout());
  22.  
  23. JPanel biggerPanel = new JPanel();
  24. biggerPanel.setLayout(new GridLayout(1,3));
  25.  
  26. bluePanel = new JPanel();
  27. bluePanel.setBackground(Color.LIGHT_GRAY);
  28. biggerPanel.add(grayPanel);
  29.  
  30. whitePanel = new JPanel();
  31. whitePanel.setBackground(Color.LIGHT_GRAY);
  32. biggerPanel.add(whitePanel);
  33.  
  34. grayPanel = new JPanel();
  35. grayPanel.setBackground(Color.LIGHT_GRAY);
  36. biggerPanel.add(grayPanel);
  37.  
  38. add(biggerPanel, BorderLayout.CENTER);
  39.  
  40. JPanel buttonPanel = new JPanel();
  41. buttonPanel.setBackground(Color.LIGHT_GRAY);
  42. buttonPanel.setLayout(new FlowLayout());
  43.  
  44. JButton blueButton = new JButton("Blue");
  45. blueButton.setBackground(Color.BLUE);
  46. blueButton.addActionListener(this);
  47. buttonPanel.add(blueButton);
  48.  
  49. JButton whiteButton = new JButton("White");
  50. whiteButton.setBackground(Color.WHITE);
  51. whiteButton.addActionListener(this);
  52. buttonPanel.add(whiteButton);
  53.  
  54.  
  55. JButton grayButton = new JButton("Gray");
  56. grayButton.setBackground(Color.GRAY);
  57. grayButton.addActionListener(this);
  58. buttonPanel.add(grayButton);
  59.  
  60. add(buttonPanel, BorderLayout.SOUTH);
  61. }
  62.  
  63. public void actionPerformed(Action e) {
  64. String buttonString = e.getActionCommand();
  65.  
  66. if (buttonString.equals("Blue"))
  67. bluePanel.setBackground(Color.BLUE);
  68. else if (buttonString.equals("White"))
  69. whitePanel.setBackground(Color.WHITE);
  70. else if (buttonString.equals("Gray"))
  71. grayPanel.setBackground(Color.GRAY);
  72. else
  73. System.out.println("Unexpected error.");
  74. }
  75. }
Add Comment
Please, Sign In to add comment