无邪231
2015-01-17 15:25
采纳率: 100%
浏览 4.9k
已采纳

java swing组件拖动和显示问题

两个问题(1)做出来的窗体组件运行后点击“添加”按钮,应该再窗体上生成一个JLabel标签,点击后能够生成但显示不出来,改变窗体大小才能显示出来。(2)生成的JLabel能够改变大小和拖动,先拖动到另一个位置,改变窗体大小后,又回到原来的位置去了。求大神知道下。

看了以前的帖子@raistlic说是重绘问题,但是按帖子方法把setVisible放在添加控件后面不起作用。使用getContentPane()invalidate.()总是提示找不到符号。

import java.awt.*;
import java.awt.event.*;
import static java.awt.BorderLayout.*;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.SwingUtilities;
import javax.swing.plaf.ButtonUI;
class Win extends Frame 
{   
    JButton jb1,jb2,jb3,jb4,jb5;//编辑、添加、修改、删除、调度
    JPanel jp_button;//放置按钮的面板
    JPanel jp_xianlu;//放置线路图的面板
    JTextField jt; //添加 时用的text
    String str1;

    Win()
    { 
      super("我的面板");
      setExtendedState( Frame.MAXIMIZED_BOTH );
      setLayout(new BorderLayout(30,5));

      jp_button =new JPanel();
      jp_button.setVisible(true);
      jp_button.setOpaque(true);
          jp_button.setBackground(Color.BLUE);
      add(jp_button,SOUTH);


      jp_xianlu =new JPanel();
      jp_xianlu.setVisible(true);
      jp_xianlu.setOpaque(true);
          jp_xianlu.setBackground(Color.GREEN);
      add(jp_xianlu,CENTER);

      JTextField jt= new JTextField("我的标签"); 
        jp_button.add(jt);
        jt.setOpaque(true);
        jt.setBackground(Color.RED);

      jb1 = new JButton("编辑");
      jb2 = new JButton("添加");
      jb3 = new JButton("修改");
      jb4 = new JButton("删除");
      jb5 = new JButton("调度");
      jp_button.add(jb1);
      jp_button.add(jb2);
      jp_button.add(jb3);
      jp_button.add(jb4);
      jp_button.add(jb5);

      //Frame上添加windows监听,使之关闭按钮可用      
      addWindowListener(new WindowAdapter(){   
           public void windowClosing(WindowEvent e){ 
                System.exit(0);
           }
       });
      //点击“添加”按钮时在jp_xianlu面板上生成JLabel
         jb2.addActionListener(new ActionListener()
         {
              public void actionPerformed(ActionEvent e) {
                                str1 = jt.getText();
                                setLab(str1,100,100);
              }
            });
     }




    public void setLab(String str,int a,int b){
          try
          {
    //        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                if(str==null)
                {
                    System.out.println("输入不能不能为空");
                    }
                else
                {
                    JLabel lab= new JLabel(str); //输入标签显示内容
                    lab.setOpaque(true);    //label 是透明的
                    lab.setBackground(Color.RED); //设置背景色
                    lab.setBounds(a,b,80,80);

                    //给生成的JLabel添加监听,使之可以自由改变大小并可以拖动
                    DnDAdapter dda =new DnDAdapter();
                    lab.addMouseMotionListener(dda);
                    jp_xianlu.add(lab);
                    }
          }
          catch(Exception e){
              System.out.println(e.getMessage());
          }

    }   

    public static void main(String args[])
   { 
            SwingUtilities.invokeLater(new Runnable() {

                  @Override
                  public void run() {
                    Win win=new Win();
                    //win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    win.setLocationRelativeTo(null);
                    win.setVisible(true);
                  }
            });
   }
}

//自定义的可以自由实现组件拖动的监听器(摘自网络,还未完全理解)
class DnDAdapter extends MouseAdapter  
{  
    //每个组件的信息,在进入组件时获得。  
    private JComponent jcom;  
    private Point jcomOriginPoint;//组件在容器内的坐标  
    private int jcomWidth,jcomHeight;  
    //与定位、缩放相关信息  
    private int a=10;//鼠标检测边沿大小  
    private    Point pointMoved;//移动时,容器内坐标  
    private CurType curCuror;//  
    private enum  CurType {LeftTop,LeftBottom,RightTop,RightBottom,Top,Bottom,Left,Right,Center}  
    //光标区域判断  
    private  CurType getCurType(MouseEvent e)  
    {  
        Point p=e.getPoint();//getPoint返回组件内的坐标  
        //左上角 0<x<a,0<y<a  
        if((p.x>=0 && p.x<=a)&&(p.y>=0&&p.y<=a))  
            return CurType.LeftTop;  
        //右下角 w-a<x<w  h-a<y<h  
        else if((p.x>=jcomWidth-a&&p.x<=jcomWidth)&&(p.y>=jcomHeight-a&&p.y<=jcomHeight))  
            return CurType.RightBottom;  
        //右上角w-a<x<w,0<y<a  
        else if((p.x>=jcomWidth-a&&p.x<=jcomWidth)&&(p.y>=0&&p.y<=a))  
            return CurType.RightTop;  
        //左下角0<x<a,h-a<y<h  
        else if((p.x>=0&&p.x<=a)&&(p.y>=jcomHeight-a&&p.y<=jcomHeight))  
            return CurType.LeftBottom;  
        //垂直:w-a<x<w && a<y<h-a 右边  
        else if( p.x>=jcomWidth-a && p.x<=jcomWidth && p.y>=a && p.y<=jcomHeight-a)  
            return CurType.Right;  
        //垂直:0<x<a,a<y<h-a 左边  
        else if(p.x>=0 && p.x<=a && p.y>=a && p.y<=jcomHeight-a)  
            return CurType.Left;  
        //水平 a<x<w-a && 0<y<a  上边  
        else if((p.x>=a&&p.x<=jcomWidth-a) &&p.y>=0&&p.y<=a)  
            return CurType.Top;  
        //水平 a<x<w-a &&h-a<y<h  下边  
        else if((p.x>=a&&p.x<=jcomWidth-a) &&p.y>=jcomHeight-a&&p.y<=jcomHeight)  
            return CurType.Bottom;  
        //中间  
        else  
            //jcom.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));//没有这句鼠标会一直保持形状  
            return CurType.Center;  

    }  
    private void setCurType(MouseEvent e)  
    {  
        switch(getCurType(e))  
        {  
        case LeftTop:  
            jcom.setCursor(Cursor.getPredefinedCursor(Cursor.NW_RESIZE_CURSOR));  
            curCuror=CurType.LeftTop;  
            break;  
        case LeftBottom:  
            jcom.setCursor(Cursor.getPredefinedCursor(Cursor.SW_RESIZE_CURSOR));  
            curCuror=CurType.LeftBottom;  
            break;  
        case RightTop:  
            jcom.setCursor(Cursor.getPredefinedCursor(Cursor.NE_RESIZE_CURSOR));  
            curCuror=CurType.RightTop;  
            break;  
        case RightBottom:  
            jcom.setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));  
            curCuror=CurType.RightBottom;  
            break;  
        case Top:  
            jcom.setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR));  
            curCuror=CurType.Top;  
            break;  
        case Bottom:  
            jcom.setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR));  
            curCuror=CurType.Bottom;  
            break;  
        case Left:  
            jcom.setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));  
            curCuror=CurType.Left;  
            break;  
        case Right:  
            jcom.setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));  
            curCuror=CurType.Right;  
            break;  
        default:  
            jcom.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));  
            curCuror=CurType.Center;  
            break;              
        }  
    }  
    private void setPosition(MouseEvent e)  
    {  
        Point pointNow=e.getLocationOnScreen();  
        int incX=pointNow.x-pointMoved.x;//增量是相对于最后一次移动的点。  
        int incY=pointNow.y-pointMoved.y;  
        ///  
        //System.out.print("PointNow:("+pointNow.x+","+pointNow.y+")\t");  
        //System.out.println("PointMoved:("+pointMoved.x+","+pointMoved.y+")\t");  
        Rectangle rect=new Rectangle();  
        switch(curCuror)  
        {  
        case LeftTop:  
            rect.x=jcomOriginPoint.x+incX;  
            rect.y=jcomOriginPoint.y+incY;  
            rect.width=jcomWidth-incX;  
            rect.height=jcomHeight-incY;  
            break;  
        case LeftBottom:  
            rect.x=jcomOriginPoint.x+incX;  
            rect.y=jcomOriginPoint.y;  
            rect.width=jcomWidth-incX;  
            rect.height=jcomHeight+incY;  
            break;  
        case RightTop:  
            rect.x=jcomOriginPoint.x;  
            rect.y=jcomOriginPoint.y+incY;  
            rect.width=jcomWidth+incX;  
            rect.height=jcomHeight-incY;  
            break;  
        case RightBottom:  
            rect.x=jcomOriginPoint.x;  
            rect.y=jcomOriginPoint.y;  
            rect.width=jcomWidth+incX;  
            rect.height=jcomHeight+incY;  
            break;  
            ////////////////////////////////////////////  
        case Top:  
            rect.x=jcomOriginPoint.x;  
            rect.y=jcomOriginPoint.y+incY;  
            rect.width=jcomWidth;  
            rect.height=jcomHeight-incY;  
            break;  
        case Bottom:  
            rect.x=jcomOriginPoint.x;  
            rect.y=jcomOriginPoint.y;  
            rect.width=jcomWidth;  
            rect.height=jcomHeight+incY;  
            break;  
        case Left:  
            rect.x=jcomOriginPoint.x+incX;  
            rect.y=jcomOriginPoint.y;  
            rect.width=jcomWidth-incX;  
            rect.height=jcomHeight;  
            break;  
        case Right://右缩放  
            rect.x=jcomOriginPoint.x;  
            rect.y=jcomOriginPoint.y;  
            rect.width=jcomWidth+incX;  
            rect.height=jcom.getHeight();  
            break;  
            ///////////////////////////////////////////////  
        case Center://移动  
            rect.x=jcomOriginPoint.x+incX;  
            rect.y=jcomOriginPoint.y+incY;  
            rect.width=jcom.getWidth();//防止出现恢复原大小问题。  
            rect.height=jcom.getHeight();  
            break;      
        default:  
            break;              
        }  
        jcom.setBounds(rect);  
    }  
    @Override  
    public void mouseMoved(MouseEvent e)   
    {  
        pointMoved=e.getLocationOnScreen();  
        jcom=(JComponent)e.getSource();//防止抖动关键在这一点  
        jcomOriginPoint=jcom.getLocation();//获取组件在父窗口的位置  
        jcomWidth=jcom.getWidth();  
        jcomHeight=jcom.getHeight();  

        ///////////////////////////////////////////  
        setCurType(e);  
        //super.mouseMoved(e);  
    }  
    public void mouseDragged(MouseEvent e)   
    {  
        //相对于移动最后一点的增量  
        setPosition(e);  
        super.mouseDragged(e);  
    }  
}
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • devmiao 2015-01-17 15:49
    已采纳
    已采纳该答案
    打赏 评论
  • dushao310 2018-04-24 05:15

    jp_xianlu的layout设置为Null即可解决改变窗体大小添加的空间返回原位的问题

    打赏 评论

相关推荐 更多相似问题