gpt4 book ai didi

java - 如何使用扩展类的 JPanel 对象?

转载 作者:行者123 更新时间:2023-11-29 05:43:25 25 4
gpt4 key购买 nike

我有一个类 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)。但是,我不反对那个类(class)。

最佳答案

I created an ActionListener.

使用Action封装您的 JToolBar 组件的功能,如 example 中所建议的那样.

image

关于java - 如何使用扩展类的 JPanel 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16681759/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com