簡體   English   中英

如何顯示工具欄?

[英]How do I get my toolbar to show?

我有這個GUI,它運行完美,但是現在我想添加一個工具欄,該工具欄將帶有“文件”按鈕,該按鈕帶有兩個選項,分別是“退出”和“關於”。 應該使用Exit關閉GUI,而About應該給出另一個JFrame框,其中包含有關分配的字符串。 由於某種原因,我無法在運行程序時讓JMenubar出現在此處。 我可以輸入一些東西,讓我知道單詞或字符串是否是回文,但工具欄沒有運氣。 我應該如何實現它以便我的工具欄顯示“文件”按鈕,下面將有兩個選項? 我對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);
    }


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

您無需在菜單欄中添加任何內容。 您需要創建一個JMenu並將其添加到菜單欄中。 然后,您需要將菜單項添加到菜單中。

閱讀Swing教程中有關如何使用菜單的部分, 獲取更多信息和工作示例。

該技術是創建包含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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM