简体   繁体   English

JFrame不重绘

[英]JFrame does not repaint

I have an issue with the repaint of a JFrame. 我在重画JFrame时遇到问题。 It repaints if only I resize it manually. 如果仅手动调整大小,它将重新绘制。 I managed somehow to repaint it by setting its visibility to false and then again to true, but this is happening far too slow not to be noticed by the user and is quite annoying.. Do you have any suggestions what could fix the problem? 我设法通过将其可见性设置为false,然后再次设置为true来重绘它,但是这种情况发生得太慢了,用户不会注意到,而且很烦人。.您有什么建议可以解决问题吗? Thanks! 谢谢!

package view;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class CopyOfServerFrame {

    private JFrame frame;
    private final JButton start = new JButton("Start server");
    private final JButton stop = new JButton("Stop");
    private ActionListener listener = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource().equals(start)) {
                frame.remove(start);
                frame.add(stop);
                frame.repaint();
                frame.repaint(1000);
                // frame.setVisible(false);
                // frame.setVisible(true);
                // server.start();
            } else if (e.getSource().equals(stop)) {
                // server.stop();
                frame.dispose();
            }
        }
    };

    public void init() {
        frame = new JFrame();
        Dimension a = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        frame.setLocation(a.width / 2, a.height / 2);
        frame.setResizable(true);
        frame.add(start);
        frame.pack();

        frame.setVisible(true);
        start.addActionListener(listener);
        stop.addActionListener(listener);

    }

}

Call revalidate() after removing or adding components on a container, and then follow this with a call to repaint() . 在容器上删除或添加组件之后,调用revalidate() ,然后再调用repaint() The revalidate call tells the container's layout managers to re-lay out all components it holds in a cascading fashion. revalidate调用告诉容器的布局管理器以层叠的方式重新布局其所拥有的所有组件。

ie,

frame.remove(start);
frame.add(stop);
frame.revalidate();
frame.repaint();

Better solution: If your goal is to swap JButtons, keep the same button, but simply swap AbstractActions. 更好的解决方案:如果您的目标是交换JButton,请保留相同的按钮,而只需交换AbstractActions。 Or if your goal is to swap out a more complex GUI with multiple components, then use a CardLayout. 或者,如果您的目标是换出具有多个组件的更复杂的GUI,请使用CardLayout。

eg, 例如,

import java.awt.Component;
import java.awt.Window;
import java.awt.event.ActionEvent;

import javax.swing.*;

public class MyServerPanel extends JPanel {
   public MyServerPanel() {
      StopAction stopAction = new StopAction("Stop");
      StartAction startAction = new StartAction("Start", stopAction);
      add(new JButton(startAction));
   }

   private class StartAction extends AbstractAction {
      private Action nextAction;

      public StartAction(String name, Action nextAction) {
         super(name);
         int mnemonic = name.charAt(0);
         putValue(MNEMONIC_KEY, mnemonic);
         this.nextAction = nextAction;
      }

      @Override
      public void actionPerformed(ActionEvent e) {
         AbstractButton source = (AbstractButton) e.getSource();
         source.setAction(nextAction);
      }
   }

   private class StopAction extends AbstractAction {
      public StopAction(String name) {
         super(name);
         int mnemonic = name.charAt(0);
         putValue(MNEMONIC_KEY, mnemonic);
      }

      @Override
      public void actionPerformed(ActionEvent e) {
         Component comp = (Component) e.getSource();
         Window win = SwingUtilities.getWindowAncestor(comp);
         if (win != null) {
            win.dispose();
         }
      }
   }

   private static void createAndShowGui() {
      JFrame frame = new JFrame("MyServerPanel");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new MyServerPanel());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM