import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.awt.image.*;class myIcon implements Icon//实现图标 { //大框图 private double scale; private int startX=3; private int startY=3; private double width; private double height; //网络故障二层 private double nWidth; private double nHeight; private int nStartx; private int nStarty; //电力故障三层 private double eWidth; private double eHeight; private int eStartx; private int eStarty; //顶层 //硬件故障一列 private double hWidth; private double hHeight; private int hStartx; private int hStarty; //软件故障二列 private double sWidth; private double sHeight; private int sStartx; private int sStarty; //更新故障二列 private double uWidth; private double uHeight; private int uStartx; private int uStarty;
//和个模块的颜色 private Color OK_COLOR=Color.green; private Color NULL_COLOR=Color.lightGray; private Color eColor= Color.black; private Color nColor=Color.red; private Color hColor=Color.orange; private Color sColor=Color.white; private Color uColor=Color.pink;
myIcon(double a) { //大框图 scale=a; width=100.0/scale; height=200.0/scale; //网络故障二层 nWidth=width; nHeight=height/3; nStartx=startX; nStarty=startY+(int)nHeight; //电力故障三层 eWidth=width; eHeight=height-height*2/3+2; eStartx=startX; eStarty=nStarty+(int)nHeight; //顶层//硬件故障一列 hWidth=width/3; hHeight=nHeight; hStartx=startX; hStarty=startY; //软件故障二列 sWidth=width/3; sHeight=nHeight; sStartx=hStartx+(int)hWidth; sStarty=startY; //更新故障二列 uWidth=width-hWidth-sWidth+2; uHeight=nHeight; uStartx=sStartx+(int)hWidth; uStarty=startY; } public int getIconWidth(){return (int)width+3;} public int getIconHeight(){return (int)height+3;} public void paintIcon(Component c,Graphics g,int x,int y) { g.drawRect(startX,startY,(int)width,(int)height);
我要在一幅图上放一些图。我是用在JLabel方JLabel来实现的。如果不改变缺省的布局管理器的话。正常。但我的小的JLabel在初始化的时候就不能再大的JLabel上。我是好改成null了。可这时却要出问题。我服了
import java.awt.event.*;
import javax.swing.*;
import java.awt.image.*;class myIcon implements Icon//实现图标
{
//大框图
private double scale;
private int startX=3;
private int startY=3;
private double width;
private double height;
//网络故障二层
private double nWidth;
private double nHeight;
private int nStartx;
private int nStarty;
//电力故障三层
private double eWidth;
private double eHeight;
private int eStartx;
private int eStarty;
//顶层
//硬件故障一列
private double hWidth;
private double hHeight;
private int hStartx;
private int hStarty;
//软件故障二列
private double sWidth;
private double sHeight;
private int sStartx;
private int sStarty;
//更新故障二列
private double uWidth;
private double uHeight;
private int uStartx;
private int uStarty;
//和个模块的颜色
private Color OK_COLOR=Color.green;
private Color NULL_COLOR=Color.lightGray;
private Color eColor= Color.black;
private Color nColor=Color.red;
private Color hColor=Color.orange;
private Color sColor=Color.white;
private Color uColor=Color.pink;
myIcon(double a)
{
//大框图
scale=a;
width=100.0/scale;
height=200.0/scale;
//网络故障二层
nWidth=width;
nHeight=height/3;
nStartx=startX;
nStarty=startY+(int)nHeight;
//电力故障三层
eWidth=width;
eHeight=height-height*2/3+2;
eStartx=startX;
eStarty=nStarty+(int)nHeight;
//顶层//硬件故障一列
hWidth=width/3;
hHeight=nHeight;
hStartx=startX;
hStarty=startY;
//软件故障二列
sWidth=width/3;
sHeight=nHeight;
sStartx=hStartx+(int)hWidth;
sStarty=startY;
//更新故障二列
uWidth=width-hWidth-sWidth+2;
uHeight=nHeight;
uStartx=sStartx+(int)hWidth;
uStarty=startY;
}
public int getIconWidth(){return (int)width+3;}
public int getIconHeight(){return (int)height+3;}
public void paintIcon(Component c,Graphics g,int x,int y)
{
g.drawRect(startX,startY,(int)width,(int)height);
g.setColor(nColor);
g.fillRect(nStartx,nStarty,(int)nWidth,(int)nHeight);
g.setColor(eColor);
g.fillRect(eStartx,eStarty,(int)eWidth,(int)eHeight); g.setColor(hColor);
g.fillRect(hStartx,hStarty,(int)hWidth,(int)hHeight);
g.setColor(sColor);
g.fillRect(sStartx,sStarty,(int)sWidth,(int)sHeight);
g.setColor(uColor);
g.fillRect(uStartx,uStarty,(int)uWidth,(int)uHeight);
g.draw3DRect(startX-1,startY-1,(int)width+1,(int)height+1,true);
g.draw3DRect(startX-2,startY-2,(int)width+2,(int)height+2,true);
g.draw3DRect(startX-3,startY-3,(int)width+3,(int)height+3,true);
}
}
class myPanel extends JPanel
{
private Graphics g;
private ImageIcon icon;
private Toolkit tool=Toolkit.getDefaultToolkit();
private myIcon myicon=new myIcon(2.0);
private Point first,last;
int i=0;
private int width,height;
JLabel mmm//小的
,lll;//大的
myPanel()
{
setLayout(null);
icon=new ImageIcon("D8-2.JPG");
mmm=new JLabel(myicon);
lll=new JLabel(icon);
lll.setBounds(0,0,icon.getIconWidth(),icon.getIconHeight());
mmm.setBounds(0,0,myicon.getIconWidth(),myicon.getIconHeight());
add(mmm);
add(lll);
width=icon.getIconWidth();
height=icon.getIconHeight();
setSize(width,height);
mmm.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
first=e.getPoint();
System.out.println("first="+first);
}
public void mouseReleased(MouseEvent e)
{
} }
);
//实现小图标的拖动
mmm.addMouseMotionListener(new MouseMotionAdapter()
{
public void mouseDragged(MouseEvent e)
{
JLabel ppp=(JLabel)e.getSource();
last=e.getPoint();
System.out.println("last="+last);
ppp.setLocation(ppp.getLocation().x+last.x-first.x,ppp.getLocation().y+last.y-first.y);
}
}
);
}
public int getWidth(){return width;}
public int getHeight(){return height;}
}
class myView extends JViewport implements AdjustmentListener
{
//实现滚屏
public void adjustmentValueChanged(AdjustmentEvent e)
{
setViewPosition(new Point(getViewPosition().x,e.getValue()));
}
}
public class temp1 extends JFrame
{
public
temp1()
{
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e){System.exit(0);}
}
);
}
public static void main(String args[])
{
Toolkit tool=Toolkit.getDefaultToolkit();
Dimension screen=tool.getScreenSize();
temp1 myform=new temp1();
myPanel my=new myPanel();
myView view=new myView();
view.setView(my);
JScrollBar ver=new JScrollBar(JScrollBar.VERTICAL,0,2,0,my.getHeight()-(int)screen.getHeight());
JScrollBar hor=new JScrollBar(JScrollBar.HORIZONTAL,0,2,0,my.getWidth()-(int)screen.getWidth());
ver.addAdjustmentListener(view);
hor.addAdjustmentListener(view);
myform.getContentPane().add(view,"Center");
myform.getContentPane().add(ver,"East");
myform.getContentPane().add(hor,"South");
myform.show();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.image.*;class myIcon implements Icon//实现图标
{
//大框图
private double scale;
private int startX=3;
private int startY=3;
private double width;
private double height;
//网络故障二层
private double nWidth;
private double nHeight;
private int nStartx;
private int nStarty;
//电力故障三层
private double eWidth;
private double eHeight;
private int eStartx;
private int eStarty;
//顶层
//硬件故障一列
private double hWidth;
private double hHeight;
private int hStartx;
private int hStarty;
//软件故障二列
private double sWidth;
private double sHeight;
private int sStartx;
private int sStarty;
//更新故障二列
private double uWidth;
private double uHeight;
private int uStartx;
private int uStarty; //和个模块的颜色
private Color OK_COLOR=Color.green;
private Color NULL_COLOR=Color.lightGray;
private Color eColor= Color.black;
private Color nColor=Color.red;
private Color hColor=Color.orange;
private Color sColor=Color.white;
private Color uColor=Color.pink; myIcon(double a)
{
//大框图
scale=a;
width=100.0/scale;
height=200.0/scale;
//网络故障二层
nWidth=width;
nHeight=height/3;
nStartx=startX;
nStarty=startY+(int)nHeight;
//电力故障三层
eWidth=width;
eHeight=height-height*2/3+2;
eStartx=startX;
eStarty=nStarty+(int)nHeight;
//顶层//硬件故障一列
hWidth=width/3;
hHeight=nHeight;
hStartx=startX;
hStarty=startY;
//软件故障二列
sWidth=width/3;
sHeight=nHeight;
sStartx=hStartx+(int)hWidth;
sStarty=startY;
//更新故障二列
uWidth=width-hWidth-sWidth+2;
uHeight=nHeight;
uStartx=sStartx+(int)hWidth;
uStarty=startY;
}
public int getIconWidth(){return (int)width+3;}
public int getIconHeight(){return (int)height+3;}
public void paintIcon(Component c,Graphics g,int x,int y)
{
g.drawRect(startX,startY,(int)width,(int)height); g.setColor(nColor);
g.fillRect(nStartx,nStarty,(int)nWidth,(int)nHeight); g.setColor(eColor);
g.fillRect(eStartx,eStarty,(int)eWidth,(int)eHeight); g.setColor(hColor);
g.fillRect(hStartx,hStarty,(int)hWidth,(int)hHeight); g.setColor(sColor);
g.fillRect(sStartx,sStarty,(int)sWidth,(int)sHeight); g.setColor(uColor);
g.fillRect(uStartx,uStarty,(int)uWidth,(int)uHeight); g.draw3DRect(startX-1,startY-1,(int)width+1,(int)height+1,true);
g.draw3DRect(startX-2,startY-2,(int)width+2,(int)height+2,true);
g.draw3DRect(startX-3,startY-3,(int)width+3,(int)height+3,true);
}
}
class myPanel extends JPanel
{
private Graphics g;
private ImageIcon icon;
private Toolkit tool=Toolkit.getDefaultToolkit();
private myIcon myicon=new myIcon(2.0); private Point first,last;
int i=0;
private int width,height; JLabel mmm//小的
,lll;//大的
myPanel()
{
setLayout(null);
icon=new ImageIcon("d.jpg");
mmm=new JLabel(myicon);
lll=new JLabel(icon);
lll.setBounds(0,0,icon.getIconWidth(),icon.getIconHeight());
mmm.setBounds(0,0,myicon.getIconWidth(),myicon.getIconHeight());
add(mmm);
add(lll);
width=icon.getIconWidth();
height=icon.getIconHeight();
setSize(width,height);
mmm.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
first=e.getPoint();
System.out.println("first="+first);
}
public void mouseReleased(MouseEvent e)
{
} }
); //实现小图标的拖动
mmm.addMouseMotionListener(new MouseMotionAdapter()
{
public void mouseDragged(MouseEvent e)
{
JLabel ppp=(JLabel)e.getSource();
last=e.getPoint();
System.out.println("last="+last);
ppp.setLocation(ppp.getLocation().x+last.x-first.x,ppp.getLocation().y+last.y-first.y);
}
}
); }
public int getWidth(){return width;}
public int getHeight(){return height;}
}
class myView extends JViewport implements AdjustmentListener
{
//实现滚屏
public void adjustmentValueChanged(AdjustmentEvent e)
{
setViewPosition(new Point(getViewPosition().x,e.getValue()));
((JPanel)super.getParent()).repaint();
}
}
public class temp1 extends JFrame
{
public temp1()
{
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e){System.exit(0);}
}
);
}
public static void main(String args[]) {
Toolkit tool=Toolkit.getDefaultToolkit();
Dimension screen=tool.getScreenSize();
temp1 myform=new temp1();
myPanel my=new myPanel();
myView view=new myView();
view.setView(my);
JScrollBar ver=new JScrollBar();
JScrollBar hor=new JScrollBar();
ver.addAdjustmentListener(view);
hor.addAdjustmentListener(view);
myform.setSize(800,600);
myform.setLocation(myform.getToolkit().getScreenSize().width/2-myform.getWidth()/2,myform.getToolkit().getScreenSize().height/2-myform.getHeight()/2);
myform.getContentPane().add(view,"Center");
myform.getContentPane().add(ver,"East");
myform.getContentPane().add(hor,"South");
myform.show();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.image.*;class myIcon implements Icon//实现图标
{
//大框图
private double scale;
private int startX=3;
private int startY=3;
private double width;
private double height;
//网络故障二层
private double nWidth;
private double nHeight;
private int nStartx;
private int nStarty;
//电力故障三层
private double eWidth;
private double eHeight;
private int eStartx;
private int eStarty;
//顶层
//硬件故障一列
private double hWidth;
private double hHeight;
private int hStartx;
private int hStarty;
//软件故障二列
private double sWidth;
private double sHeight;
private int sStartx;
private int sStarty;
//更新故障二列
private double uWidth;
private double uHeight;
private int uStartx;
private int uStarty; //和个模块的颜色
private Color OK_COLOR=Color.green;
private Color NULL_COLOR=Color.lightGray;
private Color eColor= Color.black;
private Color nColor=Color.red;
private Color hColor=Color.orange;
private Color sColor=Color.white;
private Color uColor=Color.pink; myIcon(double a)
{
//大框图
scale=a;
width=100.0/scale;
height=200.0/scale;
//网络故障二层
nWidth=width;
nHeight=height/3;
nStartx=startX;
nStarty=startY+(int)nHeight;
//电力故障三层
eWidth=width;
eHeight=height-height*2/3+2;
eStartx=startX;
eStarty=nStarty+(int)nHeight;
//顶层//硬件故障一列
hWidth=width/3;
hHeight=nHeight;
hStartx=startX;
hStarty=startY;
//软件故障二列
sWidth=width/3;
sHeight=nHeight;
sStartx=hStartx+(int)hWidth;
sStarty=startY;
//更新故障二列
uWidth=width-hWidth-sWidth+2;
uHeight=nHeight;
uStartx=sStartx+(int)hWidth;
uStarty=startY;
}
public int getIconWidth(){return (int)width+3;}
public int getIconHeight(){return (int)height+3;}
public void paintIcon(Component c,Graphics g,int x,int y)
{
g.drawRect(startX,startY,(int)width,(int)height); g.setColor(nColor);
g.fillRect(nStartx,nStarty,(int)nWidth,(int)nHeight); g.setColor(eColor);
g.fillRect(eStartx,eStarty,(int)eWidth,(int)eHeight); g.setColor(hColor);
g.fillRect(hStartx,hStarty,(int)hWidth,(int)hHeight); g.setColor(sColor);
g.fillRect(sStartx,sStarty,(int)sWidth,(int)sHeight); g.setColor(uColor);
g.fillRect(uStartx,uStarty,(int)uWidth,(int)uHeight); g.draw3DRect(startX-1,startY-1,(int)width+1,(int)height+1,true);
g.draw3DRect(startX-2,startY-2,(int)width+2,(int)height+2,true);
g.draw3DRect(startX-3,startY-3,(int)width+3,(int)height+3,true);
}
}
class myPanel extends JPanel
{
private Graphics g;
private ImageIcon icon;
private Toolkit tool=Toolkit.getDefaultToolkit();
private myIcon myicon=new myIcon(2.0); private Point first,last;
int i=0;
private int width,height; JLabel mmm//小的
,lll;//大的
myPanel()
{
setLayout(null);
icon=new ImageIcon("d.jpg");
mmm=new JLabel(myicon);
lll=new JLabel(icon);
lll.setBounds(0,0,icon.getIconWidth(),icon.getIconHeight());
mmm.setBounds(0,0,myicon.getIconWidth(),myicon.getIconHeight());
add(mmm);
add(lll);
width=icon.getIconWidth();
height=icon.getIconHeight();
setSize(width,height);
mmm.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
first=e.getPoint();
System.out.println("first="+first);
}
public void mouseReleased(MouseEvent e)
{
} }
); //实现小图标的拖动
mmm.addMouseMotionListener(new MouseMotionAdapter()
{
public void mouseDragged(MouseEvent e)
{
JLabel ppp=(JLabel)e.getSource();
last=e.getPoint();
System.out.println("last="+last);
ppp.setLocation(ppp.getLocation().x+last.x-first.x,ppp.getLocation().y+last.y-first.y);
}
}
); }
public int getWidth(){return width;}
public int getHeight(){return height;}
}
class myView extends JViewport implements AdjustmentListener
{
//实现滚屏
public void adjustmentValueChanged(AdjustmentEvent e)
{
if(((JScrollBar)e.getSource()).getName().equals("a"))
setViewPosition(new Point(getViewPosition().x,e.getValue()));
else if(((JScrollBar)e.getSource()).getName().equals("b"))
setViewPosition(new Point(e.getValue(),getViewPosition().y));
((JPanel)super.getParent()).repaint();
}
}
public class temp1 extends JFrame
{
public temp1()
{
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e){System.exit(0);}
}
);
}
public static void main(String args[]) {
Toolkit tool=Toolkit.getDefaultToolkit();
Dimension screen=tool.getScreenSize();
temp1 myform=new temp1();
myPanel my=new myPanel();
myView view=new myView();
view.setView(my);
JScrollBar ver=new JScrollBar();
JScrollBar hor=new JScrollBar();
hor.setOrientation(JScrollBar.HORIZONTAL);
ver.addAdjustmentListener(view);
hor.addAdjustmentListener(view);
ver.setName("a");
hor.setName("b");
myform.setSize(800,600);
myform.setLocation(myform.getToolkit().getScreenSize().width/2-myform.getWidth()/2,myform.getToolkit().getScreenSize().height/2-myform.getHeight()/2);
myform.getContentPane().add(view,"Center");
myform.getContentPane().add(ver,"East");
myform.getContentPane().add(hor,"South");
myform.show();
}
}
还有点问题。不知beyond_xiruo拖动我的小图标没有。为什么只能滚动之前的视口里拖动呢??如果滚动条向下滚动了。我的小图标却不能再向下拖了。这是我的小JLabel连鼠标消息都接受不到了
用jcreator编译没问题,可是执行什么也没有,除了一片白色,怎么回事啊
我的jcreator问题么?
结分了