PDA

View Full Version : سوال: منطق این کد چیست؟



golsa_6
دوشنبه 26 تیر 1391, 19:43 عصر
سلام

اگر میشود یک نفر برای من منطق این کد را بگوید. این که هر قسمتش به چه منظوری نوشته شده و شرط هایش برای چیست و ...

در ضمن این را از توی اینترنت پیدا کردم و شما هم میتوانید از آن استفاده کنید. یک ToolTip جالب تولید میکند.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.PopupFactory;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
public class ExpandableToolTip implements KeyListener, MouseListener, FocusListener {
JFrame popup; // PopUp
JPanel toolTip; // panel holding the tooltip and information how to expand
JScrollPane help; // panel holding the help text
JEditorPane h; // editor containing the help text (hyperlinks)
JComponent owner; // JCompontent the ToolTip was attached to
PopupFactory factory; // factory for generating the different popUps
int x; // X location for popUp
int y; // Y location for popUp
boolean helpActive=false; // switch indicating if the interactiv help popUp is active
Thread t; // sleeping thread

int WIDTH_HTML=250; // width of table in the HTML code
int WIDTH_SC=300; // associated width of the help window
int HEIGHT_SC=200; // height of the help window
int WIDTH_TT=300; // width of the toolTip window
int HEIGHT_TT=50; // height of the toolTip window
public ExpandableToolTip(String toolTipText, String helpText,JComponent owner){
this.owner = owner;

owner.addMouseListener(this) ;

toolTip = new JPanel(new GridLayout(3,1));
toolTip.setPreferredSize(new Dimension(WIDTH_TT, HEIGHT_TT));
toolTip.setBorder(BorderFactory.createEmptyBorder( 5,5,5,5));
toolTip.setBackground(Color.getHSBColor(15, 3, 99));
toolTip.add(new JLabel(toolTipText));
toolTip.add(new JSeparator(SwingConstants.HORIZONTAL));
JLabel more = new JLabel("press 'd' for more details");
more.setForeground(Color.DARK_GRAY);
more.setFont(new Font(null,1,10));
toolTip.add(more);

// generate help panel
JPanel helpContent = new JPanel();
helpContent.setBackground(Color.WHITE);

// generate editor to display html help text and put in scrollpane
h = new JEditorPane();
h.setContentType("text/html");
String context = "<html><body><img src=\"file:"+new ImageIcon("fall.png")+"\"width="+50+" height="+65+" border=2 BORDERCOLOR=#cc0066 align="+"left"+" /><table width='"+WIDTH_HTML+"'><tr><td><p><font size=+1>"+toolTipText+"</font></p>"+helpText+"</td></tr></table></body></html>";
h.setText(context);
h.setEditable(false);
helpContent.add(h);
help = new JScrollPane(helpContent);
help.setVerticalScrollBarPolicy(JScrollPane.VERTIC AL_SCROLLBAR_ALWAYS);
help.setPreferredSize(new Dimension(WIDTH_SC,HEIGHT_SC));

popup=new JFrame();
popup.setUndecorated(true);

}

public void keyPressed(KeyEvent arg0) {
}

public void keyReleased(KeyEvent arg0) {
}

// If 'd' was typed swap toolTip-popUp with help-popup and
// set helpActive to true to prevent that the mouseExited
// event causes the popup to go away.

public void keyTyped(KeyEvent arg0) {
if(arg0.getKeyChar()=='d'){
helpActive=true;
try{
popup.remove(toolTip);
}
catch(Exception e){}
popup.setLocation(x,y);
popup.add(help);
popup.pack();
popup.setVisible(true);

// request Focus in editor so that it can be hidden when focus is lost
h.requestFocus();
h.addFocusListener(this);

}
}

public void mouseClicked(MouseEvent arg0) {
}

public void mouseEntered(MouseEvent arg0) {

get Position of the mouse //
Point pos = new Point(arg0.getX(), arg0.getY());
SwingUtilities.convertPointToScreen(pos, owner);
x=((int)pos.getX()+10);
y=((int)pos.getY()+10);

// ensure that it does not go off the screen
// if the coordinate of the position exceeds the window size of the
// default screen it always opens on the left.
// TODO fix the two screen to be not too strict.
//
Dimension screenSize = Toolkit.getDefaultToolkit ().getScreenSize();
boolean exceed = false;
if((x+this.WIDTH_SC)>screenSize.getWidth()){
x=(x-10-this.WIDTH_SC);
}
if((y+this.HEIGHT_SC)>screenSize.getHeight()){
y=(y-10-this.HEIGHT_SC);
}

// wait for mouse to say ontop of the component for a while to ensure that
// user really wanted to see to tooltip. Generates the sleeping thread
//
t = new Thread(new Runnable(){

public void run() {
boolean cont =true; // indicating if thread was interrupted

sleep //
try {
Thread.sleep(1300);
} catch (InterruptedException e1) {
cont=false;
}

// if mouse stayed ontop of the component (mouse event is not consumed)
// create toolTip popup.
//
if(cont && !helpActive){

try{
popup.remove(help);
}
catch(Exception e){}
popup.setLocation(x,y);
popup.add(toolTip);
popup.pack();
popup.setVisible(true);
}
}
});
t.start();

//keylistener can not be in thread
popup.addKeyListener(this);

}


// Hide popUp if not 'd' was pressed before and the help popUp is now displayed

public void mouseExited(MouseEvent arg0) {

// interrupt sleep because mouse was moved away from the object
arg0.consume();
t.interrupt();

if(!helpActive){
popup.setVisible(false);
}
}

public void mousePressed(MouseEvent arg0) {
t.interrupt();
}

public void mouseReleased(MouseEvent arg0) {
}

public void focusGained(FocusEvent arg0) {
}


public void focusLost(FocusEvent arg0) {
helpActive=false;
popup.setVisible(false);
}





}

spiderman200700
دوشنبه 26 تیر 1391, 20:41 عصر
این قسمتش اشتباهه:
String context = "<html><body><img src=\"file:"+new ImageIcon("fall.png")+"\"width="+50+" height="+65+" border=2 BORDERCOLOR=#cc0066 align="+"left"+" /><table width='"+WIDTH_HTML+"'><tr><td><p><font size=+1>"+toolTipText+"</font></p>"+helpText+"</td></tr></table></body></html>";

باید اینطوری بشه:
String context = "<html><body><img src=\"file:fall.png\" width="+50+" height="+65+" border=2 BORDERCOLOR=#cc0066 align="+"left"+" /><table width='"+WIDTH_HTML+"'><tr><td><p><font size=+1>"+toolTipText+"</font></p>"+helpText+"</td></tr></table></body></html>";