作为参数的函数(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(); }
我已经尝试了很多事情,谷歌搜索几个小时,所有标准的东西。 我找到了这些东西,但发现它们对于实现这一目标的最佳方式是不确定的,或者更恰当地说是最有效的。
我喜欢在适用的地方使用引用而不是指针,主要是因为它们在任何情况下都不是记忆混乱,也不是语法混乱。 但是,如果指针更适用或更好的解决方案,我也欢迎这些答案。 谢谢你,如果你认为它可能对其他人也有帮助,那么对于如何改进这个问题的任何帮助或指示也会受到赞赏。
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.
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
最满意答案
一种选择是将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 = ... -
JScrollPane中的JTextArea(JTextArea in JScrollPane)[2023-08-08]
一种选择是将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 ... -
JTextArea与JPanel内部的JScrollPane不兼容(JTextArea not working well with JScrollPane inside JPanel)[2023-11-08]
工作很好,当我删除 - 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 ...
-
使用GridBagLayout在JTextArea中添加JScrollPane(Adding JScrollPane in JTextArea using GridBagLayout)[2022-04-03]
首先请学习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);