简体   繁体   中英

How do I get my toolbar to show?

I have this GUI and it works perfectly however now I wanted to add a toolbar which will have a "File" button that will go down with two options, which are Exit and About. Exit is supposed to close the GUI while About should give out another JFrame box that contains a string about the assignment. For some reason, I could not get my JMenubar to show up there when I run the program. I can type stuff which lets me know if a word or a string is palindrome or not but did not have any luck with toolbar. How should I implement it so that my toolbar shows with "file" button that will have two options below? I am not so good when it comes to GUI's so I would be happy if you walk me through this. Thanks Here is my code below

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Owner
 */
public class PalindromGUI extends JFrame {

    private JTextField TextField;
    private JTextArea textArea;
    private JMenuBar menubar;
    private JMenuItem exit;
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {
        PalindromGUI gui = new PalindromGUI();

        gui.setVisible(true);

    }

    public PalindromGUI() {

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(50,50,90,650);

//Create a panel and add components to it.
        JPanel contentPane = new JPanel();
        BorderLayout experimentLayout = new BorderLayout();

        contentPane.setLayout(experimentLayout);

        contentPane.setPreferredSize(new Dimension(800, 500));

        TextField = new JTextField();
        TextField.setForeground(new Color(0, 0, 0));
        TextField.setOpaque(true);
        TextField.setBackground(new Color(255, 255, 255));
        TextField.setFont(new Font("Verdana", Font.PLAIN, 30));
        TextField.setMargin(new Insets(20, 20, 20, 20));//add margin 30
        TextField.setText("Enter a word to see if it is a palindrome");
        TextField.setEditable(false);

        //toolbar
        menubar = new JMenuBar();
        setJMenuBar(menubar);

        exit = new JMenuItem();   


        //    exit.addActionListener(new exitApp());
            contentPane.add(exit);




//         menubar.setForeground(new Color(0, 0, 0));
//        menubar.setOpaque(true);
//        menubar.setBackground(new Color(255, 255, 255));
//        menubar.setFont(new Font("Verdana", Font.PLAIN, 40));
//        menubar.setMargin(new Insets(20, 20, 20, 20));
//        



        textArea = new JTextArea();
        textArea.setForeground(new Color(0, 0, 0));
        textArea.setOpaque(true);
        textArea.setBackground(new Color(255, 255, 255));
        textArea.setFont(new Font("Verdana", Font.PLAIN, 40));
        textArea.setMargin(new Insets(20, 20, 20, 20));//add margin 30
        JButton enter = new JButton();
        enter.setText("GO");
        enter.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                String pal = textArea.getText();
                String temp = pal.replaceAll("\\W", "");

                Palindrome myPalindrome = new Palindrome(temp);

                if (myPalindrome.isPalindrome()) {
                    TextField.setText(pal + " is a palindrome");
                } else {
                    TextField.setText(pal + " is not a palindrome");
                }
                textArea.setText("");
            }
        });



        contentPane.add(enter, BorderLayout.EAST);
        contentPane.add(textArea, BorderLayout.SOUTH);
        contentPane.add(TextField, BorderLayout.CENTER);
        contentPane.add(menubar, BorderLayout.PAGE_START);

        setContentPane(contentPane);

//        this.add(contentPane);

        this.pack();

        this.setVisible(true);
    }


}
    menubar = new JMenuBar();
    setJMenuBar(menubar);
    exit = new JMenuItem(); 

You don't add anything to the menubar. You need to create a JMenu and add it to the menubar. Then you need to add your menuitem to the menu.

Read the section from the Swing tutorial on How to Use Menus for more information and working examples.

The technique is to create the JMenuBar which contains JMenu's which in turn contain JMenuItem's, eg:

//toolbar
JMenu fileMenu = new JMenu("File");

exit = new JMenuItem("Exit");

fileMenu.add(new JMenuItem("About"));
fileMenu.add(exit);

menubar = new JMenuBar();
menubar.add(fileMenu);
setJMenuBar(menubar);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM