Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.nm.gui.swing;
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Container;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- public class EasyWindow extends JFrame {
- public static void main(String[] args) {
- new EasyWindow();
- }
- private final JLabel label;
- public EasyWindow() {
- super();
- setTitle("easy window");
- addWindowListener (new WindowAdapter() {
- public void windowClosing(final WindowEvent e) {
- exit();
- }
- });
- final Container con = getContentPane();
- con.setLayout(new BorderLayout());
- //components
- label = new JLabel("Dies ist ein Test", JLabel.CENTER);
- final JButton red = new JButton("rot");
- final JButton yellow = new JButton("gelb");
- final JButton exit = new JButton("Ende");
- //put the components on the frame
- con.add(label, BorderLayout.CENTER);
- con.add(red, BorderLayout.WEST);
- con.add(yellow, BorderLayout.EAST);
- con.add(exit, BorderLayout.SOUTH);
- //add the action for the buttons
- exit.addActionListener(new ActionListener() {
- public void actionPerformed(final ActionEvent e) {
- exit();
- }
- });
- yellow.addActionListener(new ActionListener() {
- public void actionPerformed(final ActionEvent e) {
- label.setForeground(Color.YELLOW);
- }
- });
- red.addActionListener(new ActionListener() {
- public void actionPerformed(final ActionEvent e) {
- label.setForeground(Color.RED);
- }
- });
- //set the size for the window
- setSize(400, 100);
- //make it visible
- setVisible(true);
- }
- private void exit() {
- System.exit(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement