Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BorderLayout;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.KeyEvent;
- import javax.swing.*;
- public class MainWindow extends JFrame{
- private JButton button1;
- private JTextField textField;
- private JMenuBar menuBar;
- private JPanel contentPane;
- public MainWindow () {
- super("Main window");
- initComponents();
- }
- private void initComponents(){
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- contentPane = new JPanel(new GridLayout(1,2));
- contentPane.setOpaque(true);
- //We add one button
- initButton1();
- contentPane.add(button1);
- //We add another one button
- textField = new JTextField("Enable");
- contentPane.add(textField);
- //We add a MenuBar
- initMenuBar();
- this.setJMenuBar(menuBar);
- //Now, we can add the contentPane to the JFrame
- this.setContentPane(contentPane);
- }
- private void initButton1() {
- button1 = new JButton("Disable Frame");
- button1.addActionListener(new ActionListener (){
- public void actionPerformed(ActionEvent arg0) {
- MainWindow.this.setEnabled(false);
- MainWindow.this.textField.setText("Disabled");
- // MainWindow.this.contentPane.setEnabled(false); //This don't disable any component.
- MainWindow.this.menuBar.setEnabled(true); //This doesn't allow to use menuBar
- }
- }
- );
- }
- private void initMenuBar(){
- menuBar = new JMenuBar();
- JMenu file = new JMenu("File");
- file.add(new JSeparator());
- JMenuItem quit = new JMenuItem("QUIT",KeyEvent.VK_Q);
- quit.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {System.exit(0);}}
- );
- file.add(quit);
- menuBar.add(file);
- }
- /**
- * Create the GUI and show it. For thread safety,
- * this method should be invoked from the
- * event-dispatching thread.
- */
- private static void createAndShowGUI() {
- //Create and set up the window.
- MainWindow test = new MainWindow();
- //Display the window.
- test.pack();
- test.setVisible(true);
- }
- public static void main(String[] args) {
- //Schedule a job for the event-dispatching thread:
- //creating and showing this application's GUI.
- javax.swing.SwingUtilities.invokeLater(new Runnable() {
- public void run() {
- createAndShowGUI();
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement