2012-09-08 27 views
1
public class POSToolBar extends JFrame { 

/** 
* Launch the application. 
*/ 
private BrowserToolBar toolBar; 

public POSToolBar() { 
    super("POS"); 
    Container content = getContentPane(); 
    content.setBackground(Color.white); 
    toolBar = new BrowserToolBar(); 
    content.add(toolBar, BorderLayout.NORTH); 
    pack(); 
    setVisible(true); 
} 
} 

以上代碼爲我生成工具欄。現在我想在其他每個擺動頁面上使用此工具欄。我繼承/擴展此類和使用frame.add(新POSToolbar()),但它讓我看到一個異常「java.lang.IllegalArgumentException異常:添加一個窗口添加到容器」 我怎麼能在我的其他揮杆頁面添加此工具欄?有關在非常搖擺頁面上添加容器的問題頁面

回答

2

如果你想在其他類中使用它,它必須擴展JToolBar而不是JFrame。

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import javax.swing.*; 

public class TestApplication extends JFrame { 
    public static void main(String [] a){ 
     TestApplication ta = new TestApplication(); 
     TBar t = new TBar(); 
     ta.setLayout(new BorderLayout()); 
     ta.add(t,BorderLayout.NORTH); 
     ta.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     ta.setPreferredSize(new Dimension(400,300)); 
     ta.pack(); 
     ta.setVisible(true); 
    } 
} 

class TBar extends JToolBar{ 
    JButton b = new JButton("Hello"); 

    public TBar(){ 
     add(b); 
    } 
} 
+0

It works Thanks Brano88 .. –

+0

@FaizanAhmed不客氣,只是接受回答:) –

相關問題