JFrame不重绘

时间:2015-03-18 16:50:54

标签: java swing jframe jbutton repaint

我对重绘JFrame有一个问题。如果我手动调整它,它会重新绘制。我通过将其可见性设置为false然后再次设置为true来以某种方式重新绘制它,但这种情况发生得太慢而不被用户注意并且非常烦人。 你有什么建议可以解决这个问题吗? 谢谢!

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);

    }

}

1 个答案:

答案 0 :(得分:3)

在删除或添加容器上的组件后调用revalidate(),然后通过调用repaint()进行此操作。 revalidate调用告诉容器的布局管理器以层叠方式重新布局它所拥有的所有组件。

即,

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

更好的解决方案:如果您的目标是交换JButton,请保持相同的按钮,但只需交换AbstractActions。或者,如果您的目标是使用多个组件交换更复杂的GUI,则使用CardLayout。

如,

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();
         }
      });
   }
}