开发者

JLabel is being shown in wrong place

I have a problem with GUI. In my drawing simulator, I created a JLabel that show mouseclicks coordinates. I put it on southwest of my JFrame but after each click, in addition to its first place, mouse coordinates also appear on northwest of the JFrame. I didn't understand what ths problem is.The code is here.

    JLabel statusBar = new JLabel( "Mouse outside JPanel" );
    Container panel;
    JFrame frame = new JFrame();
    panel = frame.getContentPane();
    panel.add(this);
    frame.setJMenuBar(jmb);
    frame.add(statusBar, BorderLayout.SOUTH );
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(700,700);
    frame.setVisible(true);

This is how I edit JLabel

    statusBar.setText( String.format( "Clicked at [%d, %d]", 
          开发者_StackOverflow社区              e.getX(), e.getY() ) );

The whole code can be longer thats why iam copying some important parts

          public class Tester extends JPanel implements MouseMotionListener, MouseListener {
          ....

This is how a draw a single line and I set the label's name here

      else if(lineci == true){
        if(mouseclicks == 0){
            l1.point1.x = e.getX();
            l1.point1.y = e.getY();
            statusBar.setText( String.format( "Clicked at [%d, %d]", 
                    e.getX(), e.getY() ) );
            mouseclicks++;
        }
        else if(mouseclicks == 1){
            l1.point2.x = e.getX();
            l1.point2.y = e.getY();
            statusBar.setText( String.format( "Clicked at [%d, %d]", 
                    e.getX(), e.getY() ) );
            mouseclicks = 0;
            int a = l1.point2.y - l1.point1.y;
            int b = l1.point1.x - l1.point2.x;
            int c = (l1.point2.x * l1.point1.y) - (l1.point1.x * l1.point2.y);
            l1.denklem[0] = a;
            l1.denklem[1] = b;
            l1.denklem[2] = c;
            array.add(l1);
            array3.add(l1);
            repaint();

        }

By the way, I 'm creating JLabel object outside the constructur of the class, just after creating class I mean.

Still same problem exists.


Instead of

frame.add(statusBar, BorderLayout.SOUTH ); 

Try

frame.getContentPane().add(statusBar, BorderLayout.SOUTH ); 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜