开发者

Internal JFrames

I want to know how to show an internal frame in swing. That means,when a JFrame is needed, normally what I do is,

new MyJFrame().setVisible(true);

Let's say the previous form should be displayed as well. And when this new frame is displayed,another new icon is displayed on the task bar.(it sounds like two separate applications run in one application) I want to avoid showing that icon and display both frames as they are in one app开发者_StackOverflow中文版lication. Thank you


..want to avoid showing that icon and display both frames as they are in one application.

Another solution is to put the 2nd and subsequent free floating elements in a JDialog.

E.G. of using both a frame and dialog to hold extra content.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class FrameTest {

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

    public static void initGui() {
        final JFrame f = new JFrame("Frame Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel gui = new JPanel(new GridLayout(0,1,5,5));
        final Content c = new Content();
        JButton frame = new JButton("Frame");
        frame.addActionListener( new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                JFrame f2 = new JFrame("Content");
                f2.add(c.getContent());
                f2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f2.pack();
                f2.setLocationByPlatform(true);
                f2.setVisible(true);
            }
        });
        gui.add(frame);

        JButton dialog = new JButton("Dialog");
        dialog.addActionListener( new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                JDialog d = new JDialog(f);
                d.add(new Content().getContent());
                d.pack();
                d.setLocationByPlatform(true);
                d.setVisible(true);
            }
        });
        gui.add(dialog);

        f.add(gui);
        f.pack();
        f.setVisible(true);
    }
}

class Content {

    public Component getContent() {
        JPanel p = new JPanel();
        p.add(new JLabel("Hello World!"));
        return p;
    }
}


You have one JFrame for an application.

You can display multiple JPanels within a JFrame.

Or, as trashgod pointed out, you can have multiple JInternalFrames within a JDesktopFrame.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜