调用重画后,JButton出现在错误的位置 [英] JButton showing up in the wrong spot after repaint is called

查看:73
本文介绍了调用重画后,JButton出现在错误的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Java中的GUI程序中添加几个JButton对象,并将它们添加到JPanel(程序的主文件)中,但是它出现在错误的位置.它在点[0,0]处显示op,并且与之链接的动作在正确的位置发生.面板上的其余元素是图像",因此经常调用repaint方法.

I am trying to add a couple of JButton objects to my GUI program in Java and it is adding them to the JPanel (which is the main file of the program) but it is appearing in the incorrect spot. It is showing op at point [0, 0] and the action that is linked to it is happening at the correct spot. The rest of the elements on the panel are Images so the repaint method is called very often.

   private void init()
 {
      setLayout(null);
      setBackground(new Color(r, g, b));
      addMouseListener(this);
      addMouseMotionListener(this);

      setSize(681, 700);
      setPreferredSize(new Dimension(681, 700));
      Insets insets = getInsets();

      //wrong spot (click at [302, 5])
      undoButton = new JButton("Undo");
       undoButton.addActionListener(this);        //button 1
       undoButton.setActionCommand("undo");
       undoButton.setVisible(true);
      add(undoButton);

      pause = new JButton("pause");
      pause.addActionListener(this);
      pause.setActionCommand("pause");          //button 2
      pause.setVisible(true);
      pause.setEnabled(true);
      add(pause);

       Dimension size = undoButton.getPreferredSize();
       undoButton.setBounds(100 + insets.left, 15 + insets.top, size.width, size.height);

       size = pause.getPreferredSize();
       pause.setBounds(100 + insets.left + undoButton.getPreferredSize().width, 15 + insets.top, size.width, size.height);


       try{
            undoButton.setMultiClickThreshhold(500);
       }
       catch (Exception e)
       {}
       //... more code ...
}

   public void paint (Graphics g)
{
      //...paint the rest of the elements...

      undoButton.update(g);
      pause.update(g);
 }

暂停"按钮显示在撤消"按钮顶部的原点,但是点击在正确的位置开始. 这两个按钮应显示在蓝色框的位置,其他所有卡均为图像.

The "pause" button is showing up at the origin on top of the undo button but the clicks is woking at the correct spots. The 2 buttons should be showing up where the blue box is and all of the other cards are Images.

推荐答案

您不应覆盖paint,而应覆盖paintComponent.如果您的组件已经添加到面板中,它们将自动更新:

You should not be overriding paint, you should be overriding paintComponent. If your components have already been added to the panel, they will update themselves:

public void paintComponent(Graphics g)
{
      super.paintComponent(g);
      //...paint the rest of the elements...

      //undoButton.update(g); BAD!
      //pause.update(g); BAD!
 }

您不必担心按钮的更新方式,如果使用正确,面板将为您处理.也不要使用setLayout(null).我们之所以拥有布局管理器是有原因的,请使用具有所需功能的布局管理器或编写自己的功能.

You don't have to worry about how the buttons get updated, the panel will handle that for you if you use it properly. Also do not use setLayout(null). We have layout managers for a reason, use one that has the features you need or write your own.

这篇关于调用重画后,JButton出现在错误的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆