jnmenu.java
来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 369 行
JAVA
369 行
package org.jnode.wt.components;
import org.jnode.wt.events.JNActionEvent;
import org.jnode.wt.events.JNodeMouseEvent;
import org.jnode.wt.layouts.JNLayoutManager;
import org.jnode.wt.events.JNActionListener;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.MenuItem;
import java.awt.peer.MenuPeer;
import java.util.Iterator;
/**
* @author Kishore
*/
public class JNMenu extends JNPanel implements MenuPeer, JNActionListener {
/* all Child components are set to same height */
private int childCompHeight = 0;
//private org.jnode.wt.components.JNComponent currentHighlightedComp = null;
private final Color highlightedColor = new Color(0, 0, 127);
private org.jnode.wt.components.JNComponent currentMouseEnteredComponent = null;
static final boolean MENU_USED_AS_MENU = false;
static final boolean MENU_USED_IN_LIST = true;
private boolean type_Of_Menu_Usage = MENU_USED_AS_MENU;
private int menu_style = JNDefaultLookAndFeel.WIN_STYLE_MENU;
// private String menuLabel = null;
protected JNMenuBar menuBar = null;
private JNButton menuButton = null;
class Seperator extends org.jnode.wt.components.JNComponent {
public void internallyPaint(Graphics g) {
g.setColor(Color.black);
g.drawRect(0, 0, this.getWidth(), 2);
}
public void resize() {
}
}
public JNMenu(String title) {
super();
setPreferredSize(-1, -1);
menuButton = new JNButton(title);
menuButton.setButtonStyle(JNDefaultLookAndFeel.MENU_BUTTON);
Font f = menuButton.getFont();
menuButton.setFont(new Font(f.getName(), Font.PLAIN, f.getSize()));
menuButton.getButtonLabel().setEnableBorderLine(false);
// menuButton.getButtonLabel().setInsets(new Insets(2,2,2,2));
menuButton.setSize((int) menuButton.getPreferredSize().getWidth(), 30);
menuButton.setBackground(Color.lightGray);
/* Bug : if text is setting after creating a button, it is not recalculating the preferedsize */
// menuButton.setText( title );
init();
}
/*
* This constructor is only to be used inside this package,
* by components like List .
*/
JNMenu(String menutitle, boolean b) {
this(menutitle);
type_Of_Menu_Usage = b;
}
/*
* This constructor is only to be used inside this package,
* by components like List .
*/
JNMenu(boolean b) {
this("");
type_Of_Menu_Usage = b;
}
public void actionPerformed(JNActionEvent ae) {
if (null != menuBar) {
menuBar.setVisibleMenu(false, this);
}
}
public org.jnode.wt.components.JNComponent add(String s) {
JNMenuItem alb = new JNMenuItem(s);
alb.setSize(alb.getPreferredSize());
alb.setAlignment(JNLabel.LEFT);
// alb.setMouseOverColors( highlightedColor, Color.white );
return add(alb);
}
public org.jnode.wt.components.JNComponent add(org.jnode.wt.components.JNComponent comp) {
// set Component location based on already added components.
int numofalreadyaddedcomponents = getComponentCount();
int ty = getInsets().top + (childCompHeight * numofalreadyaddedcomponents);
comp.setLocation(getInsets().left, ty);
if (comp instanceof JNLabel)
((JNLabel) comp).setSelectedColors(highlightedColor, Color.white);
JNAbstractButton ab = (JNAbstractButton) comp;
ab.addActionListener(this);
// add to linkedlist
super.add(comp);
// change the size of menu
if (type_Of_Menu_Usage == MENU_USED_AS_MENU) {
increaseSize(comp);
comp.setEnableBorderLine(false);
}
return comp;
}
public void addSeparator() {
/* JNLabel sepertor = new JNLabel("");
sepertor.setEnableBorderLine(true);
*/
// super.add( new Seperator() );
}
private void changeEachChildItem_Width(int wid) {
int numofchildcomps = getComponentCount();
org.jnode.wt.components.JNComponent[] comps = getComponents();
for (int i = 0; i < numofchildcomps; i++) {
comps[i].setSize(wid, comps[i].getHeight());
}
// System.out.println(wid);
}
protected void drawJavaStyleMenu(Graphics g) {
g.setColor(Color.black);
g.drawRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(Color.white);
g.drawRect(1, 1, this.getWidth() - 2, this.getHeight() - 2);
}
protected void drawMenu(Graphics g) {
if (menu_style == JNDefaultLookAndFeel.WIN_STYLE_MENU) {
drawWinStyleMenu(g);
} else if (menu_style == JNDefaultLookAndFeel.JAVA_STYLE_MENU) {
drawJavaStyleMenu(g);
}
}
protected void drawWinStyleMenu(Graphics g) {
/* g.setColor(Color.black);
g.drawRect(0,0, this.getWidth(),this.getHeight());
g.setColor(Color.white);
g.drawRect(0,0, this.getWidth(),this.getHeight());
g.setColor(Color.darkGray);
g.drawRect(0,0, this.getWidth(),this.getHeight());
g.setColor(Color.gray);
g.drawRect(0,0, this.getWidth(),this.getHeight());
*/
// ----
int x1 = 0;
int y1 = 0;
int x2 = x1 + this.getWidth();
int y2 = y1 + this.getHeight();
g.setColor(JNDefaultLookAndFeel.MenuGray);// gray
g.drawLine(x1, y1, x2 - 1, y1);
g.drawLine(x1, y1, x1, y2 - 1);
g.setColor(Color.white);// white
g.drawLine(x1 + 1, y1 + 1, x2 - 2, y1 + 1);
g.drawLine(x1 + 1, y1 + 1, x1 + 1, y2 - 2);
g.setColor(Color.black);
g.drawLine(x1, y2, x2, y2);
g.drawLine(x2, y1, x2, y2);
g.setColor(Color.gray);//
g.drawLine(x1 + 1, y2 - 1, x2 - 2, y2 - 1);
g.drawLine(x2 - 1, y1 + 1, x2 - 1, y2 - 1);
}
public String getLabel() {
return menuButton.getText();
}
public int getMenuStyle() {
return menu_style;
}
JNButton getMenuTitleButton() {
return menuButton;
}
private void increaseSize(org.jnode.wt.components.JNComponent comp) {
int newCompWidth = (int) comp.getPreferredSize().getWidth();
int newCompHeight = (int) comp.getPreferredSize().getHeight();
Dimension thisPsize = this.getPreferredSize();
// int tw = (int)thisPsize.getWidth();
// int ncwWithInsets = newCompWidth + (getInsets().left + getInsets().right);
int thisWidthNoInsets = (int) thisPsize.getWidth() - (getInsets().left + getInsets().right);
// if( ncwWithInsets > thisWidth )
if (newCompWidth > thisWidthNoInsets) {// increase the width to comp width.
// tw = ncwWithInsets;
// tw = newCompWidth;
thisWidthNoInsets = newCompWidth;
changeEachChildItem_Width(thisWidthNoInsets);
} else {
comp.setSize(thisWidthNoInsets, comp.getHeight());
// int thiswidthWithInsets = thisWidth + (getInsets().left + getInsets().right);
// comp.setSize(thiswidthWithInsets, comp.getHeight());
}
if (newCompHeight > childCompHeight) {
childCompHeight = newCompHeight;
}
/* int th = (int)thisPsize.getHeight() + cpH;
All components are of same size. */
int th = getInsets().top + getInsets().bottom + (childCompHeight * getComponentCount());
int thiswidthWithInsets = thisWidthNoInsets + (getInsets().left + getInsets().right);
// setPreferredSize(tw, th);
// setSize(tw, th);
setPreferredSize(thiswidthWithInsets, th);
setSize(thiswidthWithInsets, th);
// repaint();
}
private void init() {
// this.setInsets(new Insets(10,10,10,10));
this.setInsets(new Insets(2, 2, 1, 1));
}
protected void internallyPaint(Graphics g) {
super.internallyPaint(g);
drawMenu(g);
}
/**
* kishore
* @param event
*/
public void processMouseMotionEvent(JNodeMouseEvent event) {
super.processMouseEvent(event);
}
/**
* kishore
* @param event
*/
public void processMouseMotionEvent2(JNodeMouseEvent event) {
// synchronized (this)
{
// System.out.println("----------------");
for (Iterator it = getComponentList().iterator(); it.hasNext();) {
JNLabel c = (JNLabel) it.next();
// System.out.print("\n"+c.getText()+" visible= "+c.isVisible());
if (!c.isVisible())
continue;
if (c.getBounds().contains(event.getX(), event.getY())) {
// System.out.print(" [mouse inside Bounds]");
c.setSelected(true);
if (type_Of_Menu_Usage == MENU_USED_AS_MENU) {
c.repaint();
}
// ++++++++++++++++++
if (c != currentMouseEnteredComponent) {
if (currentMouseEnteredComponent != null) {
((JNLabel) currentMouseEnteredComponent).setSelected(false);
currentMouseEnteredComponent.repaint();
}
// [3] assign currentMouseEnteredComponent to 'c'
// ((JNLabel) c ).setMouseOver( true );
// c.repaint();
currentMouseEnteredComponent = c;
}
// ++++++++++++++++++
}
}//For
}// Synchronized
super.processMouseEvent(event);
}
public void setLabel(String str) {
menuButton.setText(str);
}
/*
* setLayout is not supported by menu, menu is always null Layout.
*/
public void setLayout(JNLayoutManager lm) {
// super.setLayout(null);
}
public void setMenuBar(JNMenuBar mb) {
menuBar = mb;
}
public void setMenuStyle(int s) {
menu_style = s;
}
public void setVisible(boolean b) {
super.setVisible(b);
repaint();
}
public void addItem(MenuItem item) {
//TODO: implement it
}
public void delItem(int index) {
//TODO: implement it
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?