JFrame和JInternalFrame示例学习(转)

Posted tiger_yj

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JFrame和JInternalFrame示例学习(转)相关的知识,希望对你有一定的参考价值。

1. 创建主窗体并简单建立菜单项,示例代码如下:

package internalFrame;

import javax.swing.JInternalFrame;
import javax.swing.JDesktopPane;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import javax.swing.JFrame;
import javax.swing.KeyStroke;
 
import java.awt.event.*;
import java.awt.*;
 
/*
 * InternalFrameDemo.java requires:
 *   MyInternalFrame.java
 */
public class InternalFrameDemo extends JFrame
                               implements ActionListener {
    JDesktopPane desktop;
 
    public InternalFrameDemo() {
        super("InternalFrameDemo");
 
        //让大窗口在屏幕的每一个边缘缩进50个像素。
        int inset = 50;
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds(inset, inset,
                  screenSize.width  - inset*2,
                  screenSize.height - inset*2);
 
        //建立GUI。
        desktop = new JDesktopPane(); //定义一个桌面面板
        createFrame(); //调用自定义方法,创建第一个窗体
        setContentPane(desktop);
        setJMenuBar(createMenuBar());  //创建菜单栏
 
        //拖放模式
        desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
    }
 
    protected JMenuBar createMenuBar() {
        JMenuBar menuBar = new JMenuBar();
 
        //定义并实例化菜单栏项目
        JMenu menu = new JMenu("Document");
        menu.setMnemonic(KeyEvent.VK_D); //定义热键 
        menuBar.add(menu);
 
        //创建第一个子菜单项
        JMenuItem menuItem = new JMenuItem("New");
        menuItem.setMnemonic(KeyEvent.VK_N);
        menuItem.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_N, ActionEvent.ALT_MASK));
        menuItem.setActionCommand("new");
        menuItem.addActionListener(this);
        menu.add(menuItem);
 
        //创建第二个子菜单项
        menuItem = new JMenuItem("Quit");
        menuItem.setMnemonic(KeyEvent.VK_Q);
        menuItem.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_Q, ActionEvent.ALT_MASK));
        menuItem.setActionCommand("quit");
        menuItem.addActionListener(this);
        menu.add(menuItem);
 
        return menuBar;
    }
 
    //对菜单的选择操作
    public void actionPerformed(ActionEvent e) {
        if ("new".equals(e.getActionCommand())) { //新增
            createFrame();
        } else { //退出
            quit();
        }
    }
 
    //创建一个新的内置窗体
    protected void createFrame() {
        MyInternalFrame frame = new MyInternalFrame();
        frame.setVisible(true); 
        desktop.add(frame);
        try {
            frame.setSelected(true);
        } catch (java.beans.PropertyVetoException e) {}
    }
 
    //退出应用
    protected void quit() {
        System.exit(0);
    }
 
    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Make sure we have nice window decorations.
        JFrame.setDefaultLookAndFeelDecorated(true);
 
        //Create and set up the window.
        InternalFrameDemo frame = new InternalFrameDemo();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        //Display the window.
        frame.setVisible(true);
    }
 
    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application‘s GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

2. 定义类置窗体类MyInternalFrame,继承父类JInternalFrame。示例代码如下:

package internalFrame;

import javax.swing.JInternalFrame;

import java.awt.event.*;
import java.awt.*;
 
/* Used by InternalFrameDemo.java. */
public class MyInternalFrame extends JInternalFrame {
    static int openFrameCount = 0;
    static final int xOffset = 30, yOffset = 30;
 
    public MyInternalFrame() {
        super("Document #" + (++openFrameCount), 
              true, //resizable
              true, //closable
              true, //maximizable
              true);//iconifiable
 
        //...Create the GUI and put it in the window...
 
        //...Then set the window size or call pack...
        setSize(300,300);
 
        //Set the window‘s location.
        setLocation(xOffset*openFrameCount, yOffset*openFrameCount);
    }
}

 

以上是关于JFrame和JInternalFrame示例学习(转)的主要内容,如果未能解决你的问题,请参考以下文章

JFrame 行为怪异

KeyListener 在 JApplet 中不起作用

第十二周课程总结

Java——其他容器

第十二周总结

如何在 JDesktopPane 上默认选择 InternalFrame?