2014-12-03 126 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(); 
     } 
    } 
}