当前位置: 技术问答>java相关
jbuilder中,菜单中字母下划线该怎么弄呢?
来源: 互联网 发布时间:2015-05-13
本文导语: jbuilder有没有什么好的书籍可以帮助入门的 | 试一试这个: import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; /** * Title: * Description: * Copyright: Copyright (c) 2001 * C...
jbuilder有没有什么好的书籍可以帮助入门的
|
试一试这个:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
/**
*
*
*
*
* @author unascribed
* @version 1.0
*/
public class UnderLineMenu extends JFrame
{
JPanel jPanel1 = new JPanel();
public UnderLineMenu()
{
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
UnderLineMenu underLineMenu1 = new UnderLineMenu();
}
private void jbInit() throws Exception
{
this.getContentPane().add(jPanel1, BorderLayout.WEST);
Container contentPane = getContentPane();
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
fileMenu.add(new UnderlineElement("item one"));
fileMenu.add(new UnderlineElement("item two"));
fileMenu.add(new UnderlineElement("item three"));
menuBar.add(fileMenu);
setJMenuBar(menuBar);
this.setSize(200,250);
this.setLocation(200,300);
this.setVisible(true);
this.show();
this.validate();
}
}
class UnderlineElement extends JButton implements MenuElement {
private boolean drawUnderline = false;
public UnderlineElement(String s) {
super(s);
setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
}
public Component getComponent() {
return this;
}
public MenuElement[] getSubElements() {
return new MenuElement[0];
}
public void menuSelectionChanged(boolean b) {
drawUnderline = b;
repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Insets insets = getInsets();
if(drawUnderline) {
FontMetrics fm = g.getFontMetrics();
g.drawLine(insets.left, insets.top + fm.getHeight(),
fm.stringWidth(getText()),
insets.top + fm.getHeight());
}
}
public void processKeyEvent(KeyEvent me,
MenuElement[] element,
MenuSelectionManager msm) {
}
public void processMouseEvent(MouseEvent me) {
super.processMouseEvent(me);
MenuSelectionManager.defaultManager().processMouseEvent(
me);
}
public void processMouseEvent(MouseEvent me,
MenuElement[] element,
MenuSelectionManager msm) {
if(me.getID() == MouseEvent.MOUSE_CLICKED ||
me.getID() == MouseEvent.MOUSE_RELEASED) {
msm.setSelectedPath(null);
doClick();
}
else
msm.setSelectedPath(getPath());
}
public MenuElement[] getPath() {
MenuSelectionManager defaultManager =
MenuSelectionManager.defaultManager();
MenuElement oldPath[] = defaultManager.getSelectedPath();
MenuElement newPath[];
int len = oldPath.length;
if(len > 0) {
MenuElement lastElement = oldPath[len-1];
Component parent = getParent();
if (lastElement == parent) {
newPath = new MenuElement[len+1];
System.arraycopy(oldPath, 0, newPath, 0, len);
newPath[len] = this;
}
else {
int j;
for (j = len-1; j >= 0; j--) {
if (oldPath[j].getComponent() == parent)
break;
}
newPath = new MenuElement[j+2];
System.arraycopy(oldPath, 0, newPath, 0, j+1);
newPath[j+1] = this;
}
}
else
return new MenuElement[0];
return newPath;
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
/**
*
Title:
*
Description:
*
Copyright: Copyright (c) 2001
*
Company:
* @author unascribed
* @version 1.0
*/
public class UnderLineMenu extends JFrame
{
JPanel jPanel1 = new JPanel();
public UnderLineMenu()
{
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
UnderLineMenu underLineMenu1 = new UnderLineMenu();
}
private void jbInit() throws Exception
{
this.getContentPane().add(jPanel1, BorderLayout.WEST);
Container contentPane = getContentPane();
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
fileMenu.add(new UnderlineElement("item one"));
fileMenu.add(new UnderlineElement("item two"));
fileMenu.add(new UnderlineElement("item three"));
menuBar.add(fileMenu);
setJMenuBar(menuBar);
this.setSize(200,250);
this.setLocation(200,300);
this.setVisible(true);
this.show();
this.validate();
}
}
class UnderlineElement extends JButton implements MenuElement {
private boolean drawUnderline = false;
public UnderlineElement(String s) {
super(s);
setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
}
public Component getComponent() {
return this;
}
public MenuElement[] getSubElements() {
return new MenuElement[0];
}
public void menuSelectionChanged(boolean b) {
drawUnderline = b;
repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Insets insets = getInsets();
if(drawUnderline) {
FontMetrics fm = g.getFontMetrics();
g.drawLine(insets.left, insets.top + fm.getHeight(),
fm.stringWidth(getText()),
insets.top + fm.getHeight());
}
}
public void processKeyEvent(KeyEvent me,
MenuElement[] element,
MenuSelectionManager msm) {
}
public void processMouseEvent(MouseEvent me) {
super.processMouseEvent(me);
MenuSelectionManager.defaultManager().processMouseEvent(
me);
}
public void processMouseEvent(MouseEvent me,
MenuElement[] element,
MenuSelectionManager msm) {
if(me.getID() == MouseEvent.MOUSE_CLICKED ||
me.getID() == MouseEvent.MOUSE_RELEASED) {
msm.setSelectedPath(null);
doClick();
}
else
msm.setSelectedPath(getPath());
}
public MenuElement[] getPath() {
MenuSelectionManager defaultManager =
MenuSelectionManager.defaultManager();
MenuElement oldPath[] = defaultManager.getSelectedPath();
MenuElement newPath[];
int len = oldPath.length;
if(len > 0) {
MenuElement lastElement = oldPath[len-1];
Component parent = getParent();
if (lastElement == parent) {
newPath = new MenuElement[len+1];
System.arraycopy(oldPath, 0, newPath, 0, len);
newPath[len] = this;
}
else {
int j;
for (j = len-1; j >= 0; j--) {
if (oldPath[j].getComponent() == parent)
break;
}
newPath = new MenuElement[j+2];
System.arraycopy(oldPath, 0, newPath, 0, j+1);
newPath[j+1] = this;
}
}
else
return new MenuElement[0];
return newPath;
}
}
|
JMenuItem.setMnemonic( int key )
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。