首页 \ 问答 \ 作为参数的函数(Functions as arguments)

作为参数的函数(Functions as arguments)

我发现自己处于需要将函数作为参数传递给另一个函数的情况。

int callSomeFunction(int &func){
  func();
}

如果它有任何区别, callSomeFunction是一个类成员。

class A{
  A(){}
  int callSomeFunction(int &func){
    func();
  }
  ~A(){}
};
A a();
a.callSomeFunction(func);

理想情况下, callSomeFunction可以采用任何类型的功能。

template<typename T>
T callSomeFunction(T &func){
  func();
}

我已经尝试了很多事情,谷歌搜索几个小时,所有标准的东西。 我找到了这些东西,但发现它们对于实现这一目标的最佳方式是不确定的,或者更恰当地说是最有效的。

资源1

资源2

我喜欢在适用的地方使用引用而不是指针,主要是因为它们在任何情况下都不是记忆混乱,也不是语法混乱。 但是,如果指针更适用或更好的解决方案,我也欢迎这些答案。 谢谢你,如果你认为它可能对其他人也有帮助,那么对于如何改进这个问题的任何帮助或指示也会受到赞赏。


I have found myself to be in a situation where I need to pass a function to another function as an argument.

int callSomeFunction(int &func){
  func();
}

If it makes any difference, callSomeFunction is a class member.

class A{
  A(){}
  int callSomeFunction(int &func){
    func();
  }
  ~A(){}
};
A a();
a.callSomeFunction(func);

Ideally, callSomeFunction would be able to take any kind of function.

template<typename T>
T callSomeFunction(T &func){
  func();
}

I have tried many things to do this, Googled for several hours, all the standard stuff. I found these things but found them inconclusive as to the best way to accomplish this, or more appropriately the most efficient.

Resource 1

Resource 2

I like to use references over pointers where applicable, mostly because they are not a memory mess nor a syntactical mess in any cases. However, if pointers would be more applicable or a better solution, I welcome those answers as well. Thank you, any help or pointers on how to improve the question are also appreciated should you think it may help other people as well.


原文:https://stackoverflow.com/questions/27117468
更新时间:2022-05-11 09:05

最满意答案

一种选择是将JTextArea和JScrollPane与GroupLayout一起使用。

jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(jPanel1Layout.createSequentialGroup().
    addComponent(jScrollPane1,
    javax.swing.GroupLayout.PREFERRED_SIZE, 328,
    javax.swing.GroupLayout.PREFERRED_SIZE)
);
jPanel1Layout.setVerticalGroup(jPanel1Layout.createSequentialGroup().
    addComponent(jScrollPane1,
    javax.swing.GroupLayout.PREFERRED_SIZE, 79,
    javax.swing.GroupLayout.PREFERRED_SIZE)
);

One option is to use the JTextArea and the JScrollPane together with a GroupLayout.

jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(jPanel1Layout.createSequentialGroup().
    addComponent(jScrollPane1,
    javax.swing.GroupLayout.PREFERRED_SIZE, 328,
    javax.swing.GroupLayout.PREFERRED_SIZE)
);
jPanel1Layout.setVerticalGroup(jPanel1Layout.createSequentialGroup().
    addComponent(jScrollPane1,
    javax.swing.GroupLayout.PREFERRED_SIZE, 79,
    javax.swing.GroupLayout.PREFERRED_SIZE)
);

相关问答

更多
  • 这里有三个问题: 1)在area初始化之前,将area添加到JScrollPane 。 所以你最终得到一个包含null组件的JScrollPane 。 要解决此问题,请在将其添加到JScrollPane之前实例化area 。 2)将area添加到JFrame ,然后添加包含JScrollPane area 。 这是错误的,一个Component不能被添加多次。 最后一个添加会赢,所以最终你的JFrame包含JTextArea和现在包含null的JScrollPane之间的混合。 要解决这个问题,请删除add ...
  • 这条线看起来很复杂,但我认为这样做会。 JTextArea c = (JTextArea) (((JViewportView) (((JScrollPane) jTabbedPane1.getComponentAt(i)).getViewport()))).getView(); 但我认为将TextArea存储在ArrayList会更有趣。 所以你可以这样做: List listAreas = new ArrayList(); ... JTextArea c = ...
  • 一种选择是将JTextArea和JScrollPane与GroupLayout一起使用。 jTextArea1.setColumns(20); jTextArea1.setRows(5); jScrollPane1.setViewportView(jTextArea1); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel ...
  • 工作很好,当我删除 - text.setPreferredSize import java.awt.Dimension; import java.awt.HeadlessException; import javax.swing.BoxLayout; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax. ...
  • 尝试将textArea的PreferedSize设置为与您的JScrollPane相同: textArea.setPreferedSize(Dimension(300, 230)); 你也可以禁用水平滚动条: scrollPane.setHorizonalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 您可以像这样设置Scrollspeed: scrollPane.getVerticalScrollBar().setUnitIncreme ...
  • 首先请学习Java命名约定 ,这让对方更容易理解Java代码。 现在到实际的事情:-) 为什么不简单地使用JTextArea.setLineWrap(true)和JTextArea.setWrapStyleWord(true)而不是定义JScrollBar策略,这在视图上甚至会看起来不错:-) 此外,不用指定setSize()/setLocation()方法,只需使用frameReference.pack()和frame.setLocationByPlatform(true) ,关于后者的好处的一个非常精彩 ...
  • E.setColumns(10); E.setRows(5); E.setPreferredSize(new Dimension(10,5)); // delete this 不要硬编码首选大小。 首选大小将覆盖您设置行/列的尝试。 所以摆脱那条线。 注意,您还可以在创建文本区域时指定行/列: JTextArea textArea = new JTextArea(5, 10); 提供文本区域的初始大小的提示。 现在,文本区域可以在添加或删除文本时更改大小,并在需要时显示滚动条。 还要遵循标准的Java命 ...
  • 您将JTextArea添加到JScrollPane您无需将其添加到JFrame 。 另外setPreferedSize()用于JScrollPane ,而不是直接用于JTextArea ,因为你的JTextArea将不可滚动。 我已经更改了你的代码,试试看: public class Class extends JFrame { public Class () { super("Title"); getContentPane().setLayout( ...
  • 使用JTextArea(int rows,int columns) 不要设置和删除chatdailog.setSize(300, 400); 不要设置和删除chatHistory.setPreferredSize(new Dimension(150,100)); 不要设置和删除mScrollMessage.add(chatHistory); 使用JScrollPane scrollPane = new JScrollPane(textArea); 代替 不要设置和删除mScrollMessage.setBo ...
  • 试试这个: text.setLineWrap(true); try this: text.setLineWrap(true);

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。