开发者

How to merge JOptionPane and Frame into one

开发者 https://www.devze.com 2023-01-02 04:18 出处:网络
Currently I have a very basic file viewer working as follows : - in JOptionPane I browse for files, and set some variables to display (colors, line connecting etc)

Currently I have a very basic file viewer working as follows :

- in JOptionPane I browse for files, and set some variables to display (colors, line connecting etc)

- previous windows loads a frame with drawn points alt text http://img190.imageshack.us/img190/4443/104bu.jpg Code : http://paste.pocoo.org/show/220066/

Now I'd like to throw it into one window, with JMenu for selecting files and changing display parameters. How to get started ? Should I rewrite everything to JDialog ? alt text http://img684.imageshac开发者_开发问答k.us/img684/5264/lab10db.jpg


If you want the JOPtionPane as a child of the main JFrame, then add it as a child. Of course it will then cover your dots. Hence you will have to not draw your dots directly in the content pane of the main JFrame, but rather in a new JPanel that you have also added to the JFRame's content pane. Let me know if I've understood the question whatsoever.

Here's some code for how I see the setup (I'm leaving the layout problem out of this, partly because it depends on what you want to see):

    JFrame frame = new JFrame();
    frame.setVisible(true);
    frame.setSize(new Dimension(400,400));
    frame.getContentPane().add(new JOptionPane());
    JPanel canvasForDots = new JPanel();
    frame.getContentPane().add(canvasForDots);


You might also like to look at How to Use Tool Bars and How to Use Menus. ImageApp is a typical implementation that associates menu items with the corresponding Action instances.

private class ClearAction extends AbstractAction {…}
private class ImageOpenAction extends AbstractAction {}
private Action openAction = new ImageOpenAction("Open");
private Action clearAction = new ClearAction("Clear");
…
JMenu menu = new JMenu("File");
menu.add(new JMenuItem(openAction));
menu.add(new JMenuItem(clearAction));

This related example adds the file chooser directly to the main frame. Here's a more elaborate example of connecting lines and shapes using the same principles.

0

精彩评论

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