【问题标题】:How to add toolbar in this frame? [closed]如何在此框架中添加工具栏? [关闭]
【发布时间】:2013-10-05 10:35:21
【问题描述】:

如何在这个框架中添加工具栏?

   import java.awt.*;
   import javax.swing.*;
   import java.awt.event.*;
   import java.net.URL;
   import javax.swing.JDesktopPane;
   import javax.swing.JFrame;
   import javax.swing.JInternalFrame;
   import javax.swing.JMenu;
   import javax.swing.JMenuBar;
   import javax.swing.JMenuItem;
   import javax.swing.JOptionPane;
   import javax.swing.SwingUtilities;

   public class Main1 extends JFrame{

   JDesktopPane jdpDesktop;


   public Main1() {

       JFrame frame = new JFrame("MainForm");

       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       jdpDesktop = new JDesktopPane() {
           @Override
           public Dimension getPreferredSize() {
               return new Dimension(600, 600);
           }
       }; 

       createFrame();

       frame.setContentPane(jdpDesktop);

       frame.setJMenuBar(createMenuBar());

       frame.pack();
       frame.setVisible(true);
   }

    JMenuBar createMenuBar() {
       JMenuBar menuBar = new JMenuBar();
          JMenu menu = new JMenu("Settings");
       JMenuItem menuItem = new JMenuItem("Logout");
       menuItem.setMnemonic(KeyEvent.VK_N);
       menuItem.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(
                       null,
                       "Thank You",
                       "Logout", JOptionPane.PLAIN_MESSAGE
                       );
           }
       }); 
       menu.add(menuItem);
       menuBar.add(menu);
       return menuBar;
   }

   protected void createFrame() {
       MyInternalFrame frame = new MyInternalFrame();
       frame.setVisible(true); jdpDesktop.add(frame);

   }

   class MyInternalFrame extends JInternalFrame {

       static final int xPosition = 150, yPosition = 140;

       public MyInternalFrame() {
           super("Calculator" ,  true,
                   true, 
                   true, 
                   true);
           setSize(300, 300);
           // Set the window's location.
           setLocation(xPosition , yPosition
                   );

       setLayout(null);
       getContentPane().setBackground(Color.white);

       }   
   }




    public static void main(String[] args) {
       SwingUtilities.invokeLater(new Runnable() {
           @Override
           public void run() {
               new Main1();
           }
       });
   } }

【问题讨论】:

    标签: java swing jframe jtoolbar


    【解决方案1】:

    这很容易。看看How to Use Tool Bars。提供的例子包括这个here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-02
      • 2012-06-29
      • 1970-01-01
      • 2010-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多