java - 如何显示我的工具栏?

标签 java swing user-interface

我有这个 GUI,它工作得很好,但是现在我想添加一个工具栏,其中有一个"file"按钮,该按钮下有两个选项,即“退出”和“关于”。 Exit 应该关闭 GUI,而 About 应该给出另一个 JFrame 框,其中包含有关分配的字符串。由于某种原因,当我运行程序时,我无法让 JMenubar 显示在那里。我可以输入一些东西,让我知道一个单词或字符串是否是回文,但工具栏没有任何运气。我应该如何实现它,以便我的工具栏显示"file"按钮,该按钮将有下面两个选项?我不太擅长 GUI,所以如果你能引导我完成这个,我会很高兴。 谢谢 下面是我的代码

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


}

最佳答案

该技术是创建 JMenuBar,其中包含 JMenu,而 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);

关于java - 如何显示我的工具栏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26515829/

相关文章:

java - 禁止 doctype 功能时出现 NullPointerException

java - 如何在 JComboBox(actionPerformed)中输入时触发 Java Swing InputVerifier?

java - 如何自定义ActionEvent的事件源?

JavaFX 当按钮按下时在文本字段中绘制具有尺寸的矩形并查找它们是否相交

java - JTable 对整数值排序的问题

android - AnySoftKeyboard 在我隐藏它之后保留垃圾数据

java - java中是否存在函数中的变量未链接的情况?

java - void 方法应使用哪个测试替身

java - 你能让一个对象在运行时可序列化吗?

java - JFrame 未执行! (Eclipse 编辑器)