2012-09-05 137 views
2

enter image description here添加的JPanel到JFrame中的NetBeans

大家好,我是在我的大學小型項目的開發。這是一個圖書館管理系統,我應該在使用Net-beans IDE的Java swing中進行。首先我正在做手動編碼。這需要時間。

在手動編碼中,我使用菜單欄和項目創建了單個JFrame,執行的所有操作都是爲所有Jpanel編寫代碼。它使文件和代碼巨大。也令人困惑。

現在我需要你的幫助。 我創建了一個主要的JFrame與菜單 一個JPanel - 新增圖書 另一個JPanel的 - 在新增圖書成功(!演示,對操作發生在地址簿)

我做出的動作監聽

addBook addbk = new addBook(); 
this.getContentPane().add(addbk); 

寫了這段代碼。 我沒有道理。 朋友,作爲一個新的java的,我要研究的是

1)如何CAL和顯示外部的JPanel的動作進行 2)如何顯示其他的JPanel,以同根JFrame中如果發生任何事件在外部JPanel中。

有點像iframe中的HTML 謝謝大家。

http://compilr.com/abelkbil/openlib/OpenLibMainGUI.java

http://compilr.com/abelkbil/openlib/addBook.java

http://compilr.com/abelkbil/openlib/bookAdded.java

+0

只需將其設置爲可見即可。 –

回答

5

CardLayout,正是您所需要的這種情況。並且學習Java Naming Conventions並堅持下去,因爲你是初學者,所以從一開始就在正確的軌道上總是很好。

下面是一個例子,你可以看看:

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class CardLayoutExample 
{ 
    private JPanel contentPane; 
    private MyPanel panel1; 
    private MyPanel panel2; 
    private MyPanel panel3; 
    private JComboBox choiceBox; 
    private String[] choices = { 
           "Panel 1", 
           "Panel 2", 
           "Panel 3" 
           }; 

    private void displayGUI() 
    { 
     JFrame frame = new JFrame("Card Layout Example"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     JPanel contentPane = new JPanel(); 
     contentPane.setBorder(
      BorderFactory.createEmptyBorder(5, 5, 5, 5)); 
     contentPane.setLayout(new CardLayout()); 

     choiceBox = new JComboBox(choices);   

     panel1 = new MyPanel(contentPane 
       , Color.RED.darker().darker(), this); 
     panel2 = new MyPanel(contentPane 
       , Color.GREEN.darker().darker(), this); 
     panel3 = new MyPanel(contentPane 
       , Color.DARK_GRAY, this); 

     contentPane.add(panel1, "Panel 1"); 
     contentPane.add(panel2, "Panel 2"); 
     contentPane.add(panel3, "Panel 3");   

     frame.getContentPane().add(choiceBox, BorderLayout.PAGE_START); 
     frame.getContentPane().add(contentPane, BorderLayout.CENTER);  
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public JComboBox getChoiceBox() 
    { 
     return choiceBox; 
    } 

    public static void main(String... args) 
    { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       new CardLayoutExample().displayGUI(); 
      } 
     }); 
    } 
} 

class MyPanel extends JPanel 
{ 

    private JButton jcomp1; 
    private JPanel contentPane; 
    private Color backgroundColour; 
    private JComboBox choiceBox; 

    public MyPanel(JPanel panel, Color c, CardLayoutExample cle) 
    { 
     contentPane = panel; 
     backgroundColour = c; 
     choiceBox = cle.getChoiceBox(); 

     setOpaque(true); 
     setBackground(backgroundColour); 

     //construct components 
     jcomp1 = new JButton ("Show New Panel"); 
     jcomp1.addActionListener(new ActionListener() 
     { 
      public void actionPerformed(ActionEvent e) 
      { 
       String changeToPanel = (String) choiceBox.getSelectedItem(); 
       CardLayout cardLayout = (CardLayout) contentPane.getLayout(); 
       cardLayout.show(contentPane, changeToPanel); 
      } 
     }); 

     add(jcomp1); 
    } 

    @Override 
    public Dimension getPreferredSize() 
    { 
     return (new Dimension(500, 500)); 
    } 
} 

否則,你可以看看這個example也。

+1

或使用'驗證()'和'repaint()''JFrame' – mKorbel

+0

EeeeeeeeeeeeeeJACTLY :-) –

+0

使用驗證() –