Advertisement
Guest User

artan sali

a guest
Dec 10th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. package de.nm.gui.swing;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Color;
  5. import java.awt.Container;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import java.awt.event.WindowAdapter;
  9. import java.awt.event.WindowEvent;
  10.  
  11. import javax.swing.JButton;
  12. import javax.swing.JFrame;
  13. import javax.swing.JLabel;
  14.  
  15. public class EasyWindow extends JFrame {
  16.  
  17. public static void main(String[] args) {
  18. new EasyWindow();
  19. }
  20.  
  21. private final JLabel label;
  22.  
  23. public EasyWindow() {
  24.  
  25. super();
  26. setTitle("easy window");
  27.  
  28. addWindowListener (new WindowAdapter() {
  29.  
  30. public void windowClosing(final WindowEvent e) {
  31. exit();
  32.  
  33. }
  34. });
  35. final Container con = getContentPane();
  36.  
  37. con.setLayout(new BorderLayout());
  38.  
  39. //components
  40. label = new JLabel("Dies ist ein Test", JLabel.CENTER);
  41. final JButton red = new JButton("rot");
  42. final JButton yellow = new JButton("gelb");
  43. final JButton exit = new JButton("Ende");
  44.  
  45. //put the components on the frame
  46. con.add(label, BorderLayout.CENTER);
  47. con.add(red, BorderLayout.WEST);
  48. con.add(yellow, BorderLayout.EAST);
  49. con.add(exit, BorderLayout.SOUTH);
  50.  
  51. //add the action for the buttons
  52. exit.addActionListener(new ActionListener() {
  53.  
  54. public void actionPerformed(final ActionEvent e) {
  55. exit();
  56.  
  57. }
  58. });
  59.  
  60. yellow.addActionListener(new ActionListener() {
  61.  
  62. public void actionPerformed(final ActionEvent e) {
  63. label.setForeground(Color.YELLOW);
  64.  
  65. }
  66. });
  67.  
  68. red.addActionListener(new ActionListener() {
  69.  
  70. public void actionPerformed(final ActionEvent e) {
  71. label.setForeground(Color.RED);
  72.  
  73. }
  74. });
  75.  
  76. //set the size for the window
  77. setSize(400, 100);
  78.  
  79. //make it visible
  80. setVisible(true);
  81. }
  82. private void exit() {
  83. System.exit(0);
  84. }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement