jntitlebar.java

来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 121 行

JAVA
121
字号
/*
 * $Id: JNTitleBar.java,v 1.4 2004/02/26 21:17:13 lsantha Exp $
 */
package org.jnode.wt.components;

import org.jnode.wt.decorators.JNBorderFactory;
import org.jnode.wt.events.JNWindowEvent;
import org.jnode.wt.events.JNodeMouseEvent;
import org.jnode.wt.events.JNMouseAdapter;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;

public class JNTitleBar extends JNPanel {

    private final JNLabel titleLabel = new JNLabel("");
    //private JNWindow window;

    public JNTitleBar(final JNWindow window) {
        //this.window = window;

        setLocation(0, 0);
        recalculate();
        setBorder(JNBorderFactory.getBorderDecorator(JNBorderFactory.SINGLE_LINE_BORDER));
        getBorder().setBaseCollor(Color.decode("0xB0C4DE"));
        setBackground(JNWindow.COLOR_ACTVE);
        JNButton closeButton = new JNButton("") {
            protected void processMouseEvent(JNodeMouseEvent event) {
                if (event.getID() == JNodeMouseEvent.MOUSE_CLICKED) {
                    super.processMouseEvent(event);
                    getDesktopManager().postEvent(new JNWindowEvent(window, JNWindowEvent.WINDOW_CLOSING));
                } else
                    super.processMouseEvent(event);
            }

            public void recalculate() {
                super.recalculate();
                if (getParent() != null)
                    this.setLocation((getParent().getSize().width - 10) - 3, 3);
            }
        };
        closeButton.setSize(new Dimension(10, 10));
        add(closeButton);
        // add title bar lebel
        titleLabel.setEnableBorderLine(false);
        titleLabel.setOpaque(false);
        titleLabel.setLocation(0, 0);
        titleLabel.setForeground(Color.WHITE);
        titleLabel.setAlignment(JNLabel.LEFT);
        this.add(titleLabel);

        addMouseMotionListener(new org.jnode.wt.components.JNMouseMotionListener() {
            public void mouseMoved(JNodeMouseEvent event) {

            }

            public void mouseDragged(JNodeMouseEvent event) {
                if (lastDragEvent == null) {
                    lastDragEvent = event;
                    return;
                }
                int dx = event.getX() - lastDragEvent.getX();
                int dy = event.getY() - lastDragEvent.getY();

                Point location = window.getLocation();
                location = new Point(location.x + dx, location.y + dy);
                window.setLocation(location.x, location.y);
                lastDragEvent = event;
            }
        });
        addMouseListener(new JNMouseAdapter() {
            public void mousePressed(JNodeMouseEvent event) {
                lastDragEvent = event;
            }

            public void mouseReleased(JNodeMouseEvent event) {
                lastDragEvent = null;
            }
        });
        //            return bar;
    }

    JNodeMouseEvent lastDragEvent = null;

    /*
	public void processMouseEvent(JNodeMouseEvent event) {
		// treat the titlebar mouse events

		if (event.getID() == JNodeMouseEvent.MOUSE_PRESSED && this.getAbsBounds().contains(event.getX(), event.getY())) {
			((JNWindow) this.getParent()).inRelocateMode = true;
		} else if (event.getID() == JNodeMouseEvent.MOUSE_RELEASED && this.getAbsBounds().contains(event.getX(), event.getY())) {
			((JNWindow) this.getParent()).inRelocateMode = false;
		}
		super.processMouseEvent(event);
	}*/


    public void recalculate() {
        final int w = getWidth();
        final int h = getHeight();
        titleLabel.setSize(Math.round(w / 2), h);
        titleLabel.recalculate();
    }

    /**
     * @return Returns the title.
     */
    public String getTitle() {
        return this.titleLabel.getText();
    }

    /**
     * @param title
     *            The title to set.
     */
    public void setTitle(String title) {
        this.titleLabel.setText(title);
    }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?