[英]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);
}
}
该技术是创建包含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.