Java 关于在Swing页面上添加容器的问题
上面的代码为我生成了一个工具栏。现在我想在每一个swing页面上使用这个工具栏。我继承/扩展这个类并使用frame.add(new POSToolbar()),但它向我显示了一个异常“java.lang.IllegalArgumentException:向容器添加窗口”Java 关于在Swing页面上添加容器的问题,java,swing,jframe,jcomponent,Java,Swing,Jframe,Jcomponent,上面的代码为我生成了一个工具栏。现在我想在每一个swing页面上使用这个工具栏。我继承/扩展这个类并使用frame.add(new POSToolbar()),但它向我显示了一个异常“java.lang.IllegalArgumentException:向容器添加窗口” 如何在其他swing页面上添加此工具栏?如果要在其他类中使用它,它必须扩展JToolBar而不是JFrame public class POSToolBar extends JFrame { /** * Launch the
如何在其他swing页面上添加此工具栏?如果要在其他类中使用它,它必须扩展JToolBar而不是JFrame
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);
}
}
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);
}
}