简体   繁体   中英

JFrame does not repaint

I have an issue with the repaint of a 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? 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() . The revalidate call tells the container's layout managers to re-lay out all components it holds in a cascading fashion.

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. Or if your goal is to swap out a more complex GUI with multiple components, then use a 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();
         }
      });
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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