import java.awt.*;import java.applet.*;import java.awt.event.*;public class menue2 extends Applet{ public int menuindex =1; public void start(){ } public void init(){ setLayout(new GridLayout(20,0)); setBackground(Color.white); addMouseListener(new MyMouselistener()); paint(); } public void repaint(int test){ add(new menubutton("Button20",1,3)); this.menuindex=test; // paint(); } public void paint(){ menubutton butt1 = new menubutton("Button1",1,1); menubutton butt2 = new menubutton("Button3",1,3); menubutton butt3 = new menubutton("Button4",1,4); menubutton butt4 = new menubutton("Button5",1,5); add(butt1); add(butt2);add(butt3);add(butt4); butt3.setvisible(false); repaint(1); } class MyMouselistener extends MouseAdapter{ public void mousePressed(MouseEvent event){ } }}
/*Klasse zur erstellung von Menü Buttons.Wird Verwendet von : menue2.class (menue2.java)*/import java.awt.*;import java.awt.event.*;import java.applet.*;import java.util.*;import java.net.*;import java.lang.*;class menubutton extends Component{ public boolean isvisible = true; public String Name; static public int submenu; public int menuindex; public int mainmenuindex; private boolean hasfocus; private boolean ismouseover; private boolean ismain; private String caption; private Color ButtonColor; private Color LineColor; private Color MenuTextColor; private Color MouseOverColor; private Color SubMenuColor; private Color SubMenuTextColor; public menubutton(String button_label, int menutype,int index){ super(); //Festlegung der Startwerte für die Globalen Variablen. this.Name = button_label; this.menuindex = index; this.hasfocus = false; this.ismouseover = false; if(menutype ==1){ this.ismain = true; } else { this.ismain = false; } this.caption = button_label; this.ButtonColor = Color.black; this.LineColor = Color.gray; this.MenuTextColor = Color.white; this.MouseOverColor = Color.lightGray; this.SubMenuColor = Color.white; this.SubMenuTextColor = Color.blue; enableEvents(AWTEvent.COMPONENT_EVENT_MASK); enableEvents(AWTEvent.FOCUS_EVENT_MASK); enableEvents(AWTEvent.MOUSE_EVENT_MASK); } public menubutton(String button_label, int menutype,int index,int mainindex){ super(); //Festlegung der Startwerte für die Globalen Variablen. this.Name = button_label; this.menuindex = index; this.mainmenuindex = mainindex; this.hasfocus = false; this.ismouseover = false; if(menutype ==1){ this.ismain = true; this.isvisible = true; } else { this.ismain = false; this.isvisible = false; } this.caption = button_label; this.ButtonColor = Color.black; this.LineColor = Color.gray; this.MenuTextColor = Color.white; this.MouseOverColor = Color.lightGray; this.SubMenuColor = Color.white; this.SubMenuTextColor = Color.blue; enableEvents(AWTEvent.COMPONENT_EVENT_MASK); enableEvents(AWTEvent.FOCUS_EVENT_MASK); enableEvents(AWTEvent.MOUSE_EVENT_MASK); } public int getbuttonMenuIndex(){ return menuindex; } public boolean isFocusTraversable(){ return true; } public void paint(Graphics g) { /* Standardfarbwerte : white lightGray gray darkGray black red blue green yellow magenta cyan orange pink andere Farbwerte können mit RGB Werten erzeugt werden */ if(isvisible){ int dx = getSize().width /20; int dy = getSize().height /6; if(ismouseover){ g.setColor(MouseOverColor); } else { if(ismain){ g.setColor(ButtonColor); } else { g.setColor(SubMenuColor); } } g.fillRect(0,0,getSize().width,getSize().height); g.setColor(Color.gray); g.drawLine(0,0,100,0); if(ismain){ g.setColor(MenuTextColor); } else { g.setColor(SubMenuTextColor); } g.drawString(this.caption,dx*4,dy*4); } } protected void processComponentEvent(ComponentEvent event) { if (event.getID() == ComponentEvent.COMPONENT_SHOWN) { requestFocus(); } super.processComponentEvent(event); } protected void processFocusEvent(FocusEvent event) { if(event.getID()== FocusEvent.FOCUS_GAINED) { hasfocus = true; repaint(); } else if(event.getID() == FocusEvent.FOCUS_LOST){ hasfocus = false; repaint(); } super.processFocusEvent(event); } protected void processMouseEvent(MouseEvent event){ //Komponente wird mit der mit der Maus betreten if(event.getID() == MouseEvent.MOUSE_ENTERED) { ismouseover = true; repaint(); } //Komponente wird mit der mit der Maus verlassen if(event.getID() == MouseEvent.MOUSE_EXITED) { ismouseover = false; repaint(); } //Maustaste wurde gedrückt if(event.getID() == MouseEvent.MOUSE_PRESSED) { this.isvisible = true; requestFocus(); if(!event.isShiftDown()){ if(event.isMetaDown()){ } } //repaint(); } super.processMouseEvent(event); } public void setvisible(boolean visible){ this.isvisible = visible; repaint(); } }