我有一堂课Main,我将其称为所有扩展类JPanel。因此,只需执行以下操作:
Frame.add(new JPanel)
知道,这样的类扩展JPanel。但是,我不能JPanel在该类中使用对象。我尝试使用创建类的对象new,但添加一些组件不起作用。只是工作方式:add,因为它是一个扩展类。
主类:
private void initialize()
{
tab = new JTabbedPane();
frame.add(new JScrollPane(tab));
frame.setTitle(TITLE);
frame.setSize(800,500);
frame.add(new ToolBar);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setJMenuBar(getJMenuBar());
frame.setVisible(true);
}
在这堂课上,我被称为我的Toolbar班级并将其添加到JFrame.
我的工具栏类:
public class ToolBar extends JPanel {
private JToolBar toolbar;
private JButton icon;
/**
*
* @param tab
*/
public ToolBar()
{
setLayout(new BorderLayout());
add(setJToolBar(),BorderLayout.NORTH); // add JToolbar to panel
setBackground(getColor()); // sets up color for panel.
}
/**
* Sets setJButtonNewFileIcon with icon
* @return
* JButton
*/
private JButton setJButtonNewFileIcon()
{
icon = new JButton();
icon.setBorder(border);
icon.setBackground(getColor());
icon.setToolTipText("New File");
icon.setIcon(new ImageIcon(getClass().getClassLoader().getResource("icons/new.png")));
return icon;
}
现在,我要创建一个ActionListener搜索。所以,我想把它ActionListener加到那个JPanel(ToolBar)上。但是,我没有反对那个类。
