📄 textbutton.java
字号:
package com.vimas.interfaceapplet.buttons;import java.applet.*;import java.lang.*;import java.util.*;import java.awt.*;import java.awt.event.*;/** * OpenlookButton - a class that produces a lightweight button. * * Lightweight components can have "transparent" areas, meaning that * you can see the background of the container behind them. * */public class TextButton extends Component { //static int capWidth = 20; // The width of the Button's endcap String label; // The Button's text protected boolean pressed = false; // true if the button is detented. ActionListener actionListener; // Post action events to listeners boolean isEnabled=true; int param=20; /** * Constructs an OpenlookButton with no label. */ public TextButton() { this("",20); } /** * Constructs an OpenlookButton with the specified label. * @param label the label of the button */ public TextButton(String label,int param) { this.label = label; this.param=param; enableEvents(AWTEvent.MOUSE_EVENT_MASK); } /** * gets the label * @see setLabel */ public String getLabel() { return label; } /** * sets the label * @see getLabel */ public void setLabel(String label) { this.label = label; invalidate(); repaint(); } /** * paints the button */ public void paint(Graphics g) { int width = getSize().width - 1; int height = getSize().height - 1; Color interior; Color highlight1; Color highlight2; interior = getBackground(); // ***** determine what colors to use if(pressed) { highlight1 = interior.darker(); highlight2 = interior.brighter(); } else { highlight1 = interior.brighter(); highlight2 = interior.darker(); } // ***** paint the interior of the button g.setColor(interior); g.fillRect(0,0,width, height); // ***** highlight the perimeter of the button // draw upper and lower highlight lines g.setColor(highlight1); g.drawLine(0, 0, width, 0); g.drawLine(0, 0, 0, height); g.setColor(highlight2); g.drawLine(0, height, width, height); g.drawLine(width, 0,width, height); if(!isEnabled) g.setColor(interior.darker()); else g.setColor(getForeground()); //this.drawMyImage(g,param); // ***** draw the label centered in the button Font f = getFont(); if(f != null) { FontMetrics fm = getFontMetrics(getFont()); g.drawString(label, width/2 - fm.stringWidth(label)/2, height/2 + fm.getHeight()/2 - fm.getMaxDescent() ); } } /** * The preferred size of the button. */ public Dimension getPreferredSize() { Font f = getFont(); if(f != null) { FontMetrics fm = getFontMetrics(getFont()); return new Dimension(fm.stringWidth(label)+10, param); } else { return new Dimension(100, param); } } /** * The minimum size of the button. */ public Dimension getMinimumSize() { return new Dimension(100, param); } /** * Adds the specified action listener to receive action events * from this button. * @param listener the action listener */ public void addActionListener(ActionListener listener) { actionListener = AWTEventMulticaster.add(actionListener, listener); enableEvents(AWTEvent.MOUSE_EVENT_MASK); } /** * Removes the specified action listener so it no longer receives * action events from this button. * @param listener the action listener */ public void removeActionListener(ActionListener listener) { actionListener = AWTEventMulticaster.remove(actionListener, listener); } /** * Paints the button and sends an action event to all listeners. */ public void processMouseEvent(MouseEvent e) { if(!isEnabled)return; Graphics g; switch(e.getID()) { case MouseEvent.MOUSE_PRESSED: // render myself inverted.... pressed = true; // Repaint might flicker a bit. To avoid this, you can use // double buffering (see the Gauge example). repaint(); break; case MouseEvent.MOUSE_RELEASED: if(actionListener != null) { actionListener.actionPerformed(new ActionEvent( this, ActionEvent.ACTION_PERFORMED, label)); } // render myself normal again if(pressed == true) { pressed = false; // Repaint might flicker a bit. To avoid this, you can use // double buffering (see the Gauge example). repaint(); } break; case MouseEvent.MOUSE_ENTERED: break; case MouseEvent.MOUSE_EXITED: if(pressed == true) { // Cancel! Don't send action event. pressed = false; // Repaint might flicker a bit. To avoid this, you can use // double buffering (see the Gauge example). repaint(); // Note: for a more complete button implementation, // you wouldn't want to cancel at this point, but // rather detect when the mouse re-entered, and // re-highlight the button. There are a few state // issues that that you need to handle, which we leave // this an an excercise for the reader (I always // wanted to say that!) } break; } super.processMouseEvent(e); } public boolean isEnabled() { return isEnabled; } public void setEnabled(boolean isEnabled) { this.isEnabled = isEnabled; this.repaint(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -