我已經加入我的所有UI組件到我的JPanel並補充說,面板的JFrame當特定事件 從Jframe.and我的JFrame是有JTollbar也發生JMenu的。如何添加更多的Jpanel到JFrame?
然而,問題是,當我試圖添加一個Panel類對象我需要調用removeAll()以刪除以前添加的Jpanel。但這種方法也是刪除我的Jtoolbar。 我應該爲這個問題做些什麼。
我已經加入我的所有UI組件到我的JPanel並補充說,面板的JFrame當特定事件 從Jframe.and我的JFrame是有JTollbar也發生JMenu的。如何添加更多的Jpanel到JFrame?
然而,問題是,當我試圖添加一個Panel類對象我需要調用removeAll()以刪除以前添加的Jpanel。但這種方法也是刪除我的Jtoolbar。 我應該爲這個問題做些什麼。
你應該看看layouts,甚至可能在card layout。卡片佈局允許您堆疊面板,然後決定哪一個可見。
使用Swing庫提供的佈局,例如BorderLayout
甚至GroupLayout
(http://docs.oracle.com/javase/tutorial/uiswing/layout/group.html)
正如其他人所說,使用佈局來安排您的面板,並可能會添加一個新的面板到視圖。 例如,您可以使用BorderLayout將工具欄分配給PageStart,並將面板分配給JFrame的中心。後來如果新面板的佈局是完全不同的卡片佈局可以幫助你可以改變你的佈局
的中心位置的內容。
一個非常簡單的(可能是壞的)解決方案,可以先對你的JFrame添加大量的JPanel爲主要內容的面板。然後將您的面板添加到該面板,稍後刪除該主內容面板中的所有組件。在這種情況下,您的工具欄是安全的。
我會告訴你的短代碼,我希望你能夠把它集成到應用程序..
class MyClass extends JFrame implements ActionListener,ItemListener
{
JMenuBar menubar1;
JMenu menu1,menu2;
JMenuItem item1,item2,item3;
JCheckBoxMenuItem jcbmi;
PanelFont pf = new PanelFont();
PanelShape ps = new PanelShape();
PanelIcon pi = new PanelIcon();
................
MyClass()
{
menubar1=new JMenuBar();
menu1=new JMenu("Application");
menu2=new JMenu("Window");
item1=new JMenuItem("Font & Color");
item2=new JMenuItem("Shape");
item3=new JMenuItem("Image & IconButton");
jcbmi=new JCheckBoxMenuItem("Resize");
menu1.add(item1);
menu1.add(item2);
menu1.add(item3);
menu2.add(jcbmi);
menubar1.add(menu1);
menubar1.add(menu2);
setJMenuBar(menubar1);
getContentPane().add(pf);
item1.addActionListener(this);
item2.addActionListener(this);
item3.addActionListener(this);
jcbmi.addItemListener(this);
}
...............
public void actionPerformed(ActionEvent e)
{
Object source =(JMenuItem) e.getSource();
if(source == item1)
{
System.out.println("Font & Color...");
pf=new PanelFont();
getContentPane().removeAll();
getContentPane().add(pf);
validate();
}
else if(source == item2)
{
System.out.println("Shape...");
ps =new PanelShape();
getContentPane().removeAll();
getContentPane().add(ps);
validate();
}
else if(source == item3)
{
pi =new PanelIcon();
getContentPane().removeAll();
getContentPane().add(pi);
Image picture = pi.myFrameImage();
setIconImage(picture);
validate();
}
}
............
}//End of class
class PanelFont extends JPanel implements ItemListener
{
..........
JComboBox fontSize = new JComboBox (size);
JComboBox fontColor = new JComboBox (color);
JComboBox fontFamily;
JCheckBox ckBold = new JCheckBox ("Bold");
JCheckBox ckItalic = new JCheckBox ("Italic");
PanelFont()
{
GraphicsEnvironment ge ;
ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] family = ge.getAvailableFontFamilyNames();
fontFamily = new JComboBox (family);
add(fontFamily);
add(fontSize);
add(fontColor);
add(ckBold);
add(ckItalic);
}//End of class
//Start of PanelShape
class PanelShape extends JPanel implements ItemListener
{
JComboBox cbShape;
String[] shape1 = {"Rectangle","RoundRectangle","Line","Polygon","Oval","Arc"};
Shape sp = null;
PanelShape()
{
cbShape = new JComboBox(shape1);
add(cbShape);
............
}//End of PanelShape
//Start of Panel Icon & Image
class PanelIcon extends JPanel
{
Image picture;
ImageIcon btnImage,FrameImage;
JButton btnHello = new JButton("BHUSHAN");
Toolkit tk ;
PanelIcon()
{
tk = Toolkit.getDefaultToolkit();
picture = tk.getImage("Image\\JavaLogo1.jpg");
btnImage =new ImageIcon("Image\\Icon.jpg");
btnHello.setIcon(btnImage);
add(btnHello);
repaint();
}
.....
In this example , I used three different Panels & It is also be changed at menu change event occurs...
PanelFont pf = new PanelFont(); PanelShape ps = new PanelShape(); PanelIcon pi = new PanelIcon(); 這些是稍後宣佈的不同面板的對象... –
爲了更好地幫助越早,張貼[SSCCE(http://sscce.org/) 。 –