简体   繁体   English

如何刷新JFrame

[英]How to refresh JFrame

I know there were similar questions but I have some different problem... 我知道有类似的问题,但我有一些不同的问题...

I'd like to remove all elements of JFrame after clicking a button: 我想在单击按钮后删除JFrame的所有元素:

It works: 有用:

button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0)
    {
        frame.getContentPane().removeAll();
        frame.revalidate();
        frame.repaint();
    }
});

All elements disappeared. 所有元素都消失了。 But after that I need to putelements on this JFrame... After these 3 lines above (below frame.repaint() ) I call method initialize (method that I call when I create my window at the beginning): 但是之后,我需要在此JFrame上放置元素...在以上三行之后(在frame.repaint()之下),我调用方法initialize (在开始创建窗口时调用的方法):

private void initialize()
{
    frame = new JFrame();
    frame.setBounds(100, 100, 1454, 860);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JButton btnNewSubject = new JButton("New subject");
    btnNewSubject.setBounds(647, 788, 137, 23);
    frame.getContentPane().add(btnNewSubject);

    JButton btnRefresh = new JButton("Refresh");
    btnRefresh.setBounds(1339, 788, 89, 23);
    frame.getContentPane().add(btnRefresh);

    JLabel lblNewLabel = new JLabel("Subject");
    lblNewLabel.setBounds(235, 11, 75, 14);
    frame.getContentPane().add(lblNewLabel);

    JLabel lblOwner = new JLabel("Owner");
    lblOwner.setBounds(662, 11, 46, 14);
    frame.getContentPane().add(lblOwner);

    JLabel lblStatus = new JLabel("Status");
    lblStatus.setBounds(883, 11, 46, 14);
    frame.getContentPane().add(lblStatus);

    JLabel lblDateOfAdded = new JLabel("Date of added");
    lblDateOfAdded.setBounds(1104, 11, 116, 14);
    frame.getContentPane().add(lblDateOfAdded);
}

Nothing happens. 什么都没发生。 :( JFrame is still empty. Even if I call revalidate and repaint(). :( JFrame仍然是空的。即使我调用revalidate和repaint()。

What is wrong? 怎么了?

You are creating a completely new JFrame in your method here 您将在这里的方法中创建一个全新的JFrame

frame = new JFrame();

and you never display it, you never call setVisible(true) on it, and so it will remain invisible. 而且您永远不会显示它,也永远不会在其上调用setVisible(true) ,因此它将保持不可见。 It almost sounds as if you're creating two JFrames without realizing it, that you are adding components to the second non-displayed JFrame, but are leaving displaying just the first one, the one without new components. 听起来好像您是在创建两个JFrame却没有意识到它一样,您正在向第二个未显示的JFrame中添加组件,但是只显示第一个,而没有新组件。

More importantly, you will want to use a CardLayout to help you swap your JPanel views as this situation is exactly what it's built for. 更重要的是,您将需要使用CardLayout来帮助您交换JPanel视图,因为这种情况正是其建立的目的。 Also, Your program uses null layout and setBounds(...) something that results in a rigid GUI that may look good on one system but will usually look poor on any other system or screen resolution. 另外,您的程序使用null布局和setBounds(...),这会导致刚性的GUI在一个系统上看起来不错,但在任何其他系统或屏幕分辨率下通常看起来很差。 Programs created this way are very hard to debug, maintain and upgrade. 用这种方法创建的程序很难调试,维护和升级。 Instead use the layout managers as this is what they excel at: at creating complex flexible GUI's that can be enhanced and changed easily. 而是使用布局管理器,因为这是它们的优势:创建复杂的灵活GUI时,可以轻松地对其进行增强和更改。

Note that your removeAll() call does not remove the root pane as Ludovic is stating because you're calling this on the contentPane not the JFrame, and the contentPane does not contain the root pane. 请注意,您的removeAll()调用不会删除Ludovic所说的根窗格,因为您是在contentPane而不是JFrame上调用它的,并且contentPane不包含根窗格。


Edit 编辑
For example, 例如,

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public class CardLayoutEg extends JPanel {
   private CardLayout cardlayout = new CardLayout();
   private TitlePanel titlePanel = new TitlePanel(this);
   private SubjectPanel subjectPanel = new SubjectPanel(this);

   public CardLayoutEg() {
      setLayout(cardlayout);
      add(titlePanel, titlePanel.getName());
      add(subjectPanel, subjectPanel.getName());
   }

   public void nextCard() {
      cardlayout.next(this);
   }

   public void showCard(String key) {
      cardlayout.show(this, key);
   }

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

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

}

class TitlePanel extends JPanel {
   public static final String TITLE_PANEL = "title panel";
   private static final int PREF_W = 900;
   private static final int PREF_H = 750;
   private static final String TITLE = "My Application Title";
   private static final float POINTS = 46f;
   private CardLayoutEg cardLayoutEg;

   public TitlePanel(CardLayoutEg cardLayoutEg) {
      setName(TITLE_PANEL);
      this.cardLayoutEg = cardLayoutEg;

      JLabel titleLabel = new JLabel(TITLE, SwingConstants.CENTER);
      titleLabel.setFont(titleLabel.getFont().deriveFont(Font.BOLD, POINTS));

      JButton subjectButton = new JButton(new SubjectAction("Subjects"));
      JPanel buttonPanel = new JPanel();
      buttonPanel.add(subjectButton);

      setLayout(new BorderLayout());
      add(titleLabel, BorderLayout.CENTER);
      add(buttonPanel, BorderLayout.PAGE_END);
   }

   @Override
   public Dimension getPreferredSize() {
      if (isPreferredSizeSet()) {
         return super.getPreferredSize();
      }
      return new Dimension(PREF_W, PREF_H);
   }

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

      @Override
      public void actionPerformed(ActionEvent e) {
         cardLayoutEg.showCard(SubjectPanel.SUBJECT_PANEL);
      }
   }
}

class SubjectPanel extends JPanel {
   public static final String SUBJECT_PANEL = "subject panel";
   private static final String[] COLUMN_NAMES = {"Subject", "Owner", "Status", "Date Added"};
   DefaultTableModel tableModel = new DefaultTableModel(COLUMN_NAMES, 10);
   private JTable table = new JTable(tableModel);
   private CardLayoutEg cardLayoutEg;

   public SubjectPanel(CardLayoutEg cardLayoutEg) {
      setBorder(BorderFactory.createTitledBorder("Subject Panel"));
      setName(SUBJECT_PANEL);
      this.cardLayoutEg = cardLayoutEg;

      JPanel buttonPanel = new JPanel();
      buttonPanel.setLayout(new GridLayout(1, 0, 10, 0));
      buttonPanel.add(new JButton("New Subject"));
      buttonPanel.add(new JButton("Refresh"));
      buttonPanel.add(new JButton(new TitleAction("Title")));
      buttonPanel.add(new JButton(new ExitAction("Exit")));

      JPanel bottomPanel = new JPanel();
      bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.LINE_AXIS));
      bottomPanel.add(Box.createHorizontalGlue());
      bottomPanel.add(buttonPanel);

      setLayout(new BorderLayout());
      add(new JScrollPane(table), BorderLayout.CENTER);
      add(bottomPanel, BorderLayout.PAGE_END);
   }

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

      @Override
      public void actionPerformed(ActionEvent e) {
         cardLayoutEg.showCard(TitlePanel.TITLE_PANEL);
      }
   }

   private class ExitAction extends AbstractAction {
      public ExitAction(String name) {
         super(name);
         int mnemonic = KeyEvent.VK_X;
         putValue(MNEMONIC_KEY, mnemonic);
      }

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

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

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