Mixing heavyweight and lightweight components - Java Swing

Java examples for Swing:Introduction

Description

Mixing heavyweight and lightweight components

Demo Code

import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;

public class Main extends JFrame {

  public Main() {
    this.setTitle("Example");
    this.setSize(200, 100);
    this.setLocationRelativeTo(null);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLayout(new FlowLayout());

    Button exitButton = new Button("Exit");
    exitButton.addActionListener( event->System.exit(0));
    this.add(exitButton);

    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("Overlapping Menu");
    JMenuItem menuItem = new JMenuItem("Overlapping Item");
    menu.add(menuItem);//  w w w  .j  av a2 s.  c o m
    menuBar.add(menu);
    this.setJMenuBar(menuBar);
    this.validate();

  }

  public static void main(String[] args) {

    SwingUtilities.invokeLater(()->{
        Main window = new Main();
        window.setVisible(true);
    });

  }
}

Related Tutorials