简体   繁体   English

如何显示工具栏?

[英]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. 我有这个GUI,它运行完美,但是现在我想添加一个工具栏,该工具栏将带有“文件”按钮,该按钮带有两个选项,分别是“退出”和“关于”。 Exit is supposed to close the GUI while About should give out another JFrame box that contains a string about the assignment. 应该使用Exit关闭GUI,而About应该给出另一个JFrame框,其中包含有关分配的字符串。 For some reason, I could not get my JMenubar to show up there when I run the program. 由于某种原因,我无法在运行程序时让JMenubar出现在此处。 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. 我对GUI的了解不是很好,因此如果您能逐步了解我,我将非常高兴。 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. 您需要创建一个JMenu并将其添加到菜单栏中。 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. 阅读Swing教程中有关如何使用菜单的部分, 获取更多信息和工作示例。

The technique is to create the JMenuBar which contains JMenu's which in turn contain JMenuItem's, eg: 该技术是创建包含JMenu的JMenuBar,而JMenu又包含JMenuItem,例如:

//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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Quaqua中获得统一的工具栏? - How do I get a unified toolbar in Quaqua? 如何获得Java Applet显示名片图像? - How do I get my Java Applet to show the card images? 如何让我的自定义 JPanel 显示在另一个 class 中? - How do I get my custom JPanel to show in another class? 如何显示我的 Etherpad 更改? - How do I get my Etherpad changes to show up? 如何显示我的MenuActivity? - How do I get my MenuActivity to show up? 我如何摆脱工具栏和回收站视图之间的这种差距? - How do i get rid of this gap between the toolbar and the recycler view? 从JComboBox中选择一个名称后,如何获得积分显示在程序上? - How do I get points to show up on my program after selecting a name from my JComboBox? 如何获取对话框以显示程序的输出? - How do I get my dialog box to show the output of my program? 当我在工具栏标题和副标题中加载徽标时不显示 - When I load logo in toolbar title and subtitle do not show 如何获取用户输入并将其成功存储在 ArrayList 中? 那么我如何让我的程序向我显示 ArrayList 中的所有元素? - How do i take user input and store it successfully in an ArrayList? Then how do i get my program to show me all the elements in the ArrayList?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM