开发者

Swing window arbitrarily adding margins in Windows and Linux, but not Mac

For a school project, I have to make a Library management app in Java. Since, Reeder for Mac came out recently, I thought I might try to recreate the interface (see sample here http://cl.ly/3VAn ), using an undecorated window.

After many attempts I finally got the base of it, working on the Mac. for image go to http://cl.ly/3VNU/ScreenshotMac.png

Bu开发者_如何学Pythont in Windows and Ubuntu I get this strange margin. for image go to http://cl.ly/3V4I/ScreenshotLinux.png

The window is a JFrame with three JPanels which have their paintComponent overrided. Here is the compiled .jar, http://cl.ly/3VCG to try it yourselves. If you think you need the source code, here it is http://cl.ly/3VSz .

Thanks in advance!


A couple of things to note:

  1. You only need one mouse handler for both panels. As this may needlessly complicate event handling in the tool bars, I'd let the OS do the decorating instead.

  2. You should localize your background drawing.

  3. You can use setPreferredSize() and override getPreferredSize() as needed.

  4. Your main() method should build the GUI on "on the event dispatch thread."

  5. The default layout of JFrame is BorderLayout with no gaps between components.

Here's an example that suggests some of the principles:

public class WelcomeWindow extends JFrame {

    private ToolPanel top = new ToolPanel("/guiresources/BgTop.png");
    private PaperPanel middle = new PaperPanel("/guiresources/BgPaper.png");
    private ToolPanel bottom = new ToolPanel("/guiresources/BgBottom.png");

    public WelcomeWindow() throws IOException {
        initComponents();
    }

    private void initComponents() throws IOException {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setUndecorated(true);

        middle.setPreferredSize(new Dimension(640, 480));
        this.add(top, BorderLayout.NORTH);
        this.add(middle, BorderLayout.CENTER);
        this.add(bottom, BorderLayout.SOUTH);

        MouseHandler mouseHandler = new MouseHandler();
        top.addMouseListener(mouseHandler);
        top.addMouseMotionListener(mouseHandler);
        bottom.addMouseListener(mouseHandler);
        bottom.addMouseMotionListener(mouseHandler);

        this.pack();
        this.setLocationRelativeTo(null);
    }

    private class MouseHandler extends MouseAdapter {

        private Point point = new Point();

        @Override
        public void mousePressed(MouseEvent e) {
            point.x = e.getX();
            point.y = e.getY();
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            Point p = getLocation();
            setLocation(p.x + e.getX() - point.x, p.y + e.getY() - point.y);
        }
    }
}

class PaperPanel extends JPanel {

    protected Image image;

    PaperPanel(String name) {
        try {
            image = ImageIO.read(getClass().getResource(name));
        } catch (IOException ex) {
            ex.printStackTrace(System.err);
        }
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
    }
}

class ToolPanel extends PaperPanel {

    ToolPanel(String name) {
        super(name);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(image.getWidth(null), image.getHeight(null));
    }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜