2014-12-03 47 views
1

我知道有类似的问题,但我有一些不同的问题...如何刷新的JFrame

我想点击一个按钮后删除JFrame的所有元素:

它的工作原理:

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

所有元素消失。但在那之后,我需要putelements这个JFrame的......这些3线高于(低于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); 
} 

什么也没有发生。 :(JFrame的仍然是空的。即使我打电话重新验证并重新绘制()。

有什么不对?

+1

Java的图形用户界面有不同的OS”,屏幕大小,屏幕分辨率等。因此,它们不利于像素完美的布局工作。请使用布局管理器或[它们的组合](http://stackoverflow.com/a/5630271/418556)以及[white space]的布局填充和边框(http://stackoverflow.com/a/17874718/ 418556)。 – 2014-12-04 00:05:49

回答

5

你在你的方法创建一个完全新的JFrame这里

frame = new JFrame(); 
从未

和你显示它,你永远不会打电话给setVisible(true),所以它将保持不可见。这听起来好像你正在创建两个JFrames没有意识到,你正在添加组件到第二个非显示的JFrame,但正在离开只显示第一个,没有新组件的那个

更重要的是,您将需要使用CardLayout来帮助您交换JPanel视图,因为这种情况正是它的目的。此外,您的程序使用空布局和setBounds(...)的东西,导致一个刚性的GUI,可能在一个系统上看起来不错,但通常在任何其他系统或屏幕分辨率上看起来很差。以这种方式创建的程序非常难以调试,维护和升级。而是使用布局管理器,因为这是他们擅长的:创建复杂灵活的GUI,可以轻松增强和更改。

请注意,您的removeAll()调用不会删除根窗格,因为Ludovic声明是因为您在contentPane而不是JFrame上调用此窗口,并且contentPane不包含根窗格。


编辑
例如,

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