为什么我的组件不使用FlowLayout包装在这个JPanel中?它们只是在屏幕上运行,只是部分可见.
JPanel panel = new JPanel(new FlowLayout());
panel.add(new JLabel("TEST"));
// ... repeat adding JLabels until they go off screen when they SHOULD wrap
// to the next line...
Run Code Online (Sandbox Code Playgroud)
这是我的全部代码(除了添加和打包框架).我误解了FlowLayout吗?我是否必须在标签或面板上设置某种尺寸?
以下代码打印:
2
1
Run Code Online (Sandbox Code Playgroud)
代替
2
2
Run Code Online (Sandbox Code Playgroud)
为什么我的二传手没有调整价值?
Vector location = camera.get_location();
camera.get_location().set_y(location.get_y() + 1);
std::cout << location.get_y() + 1 << std::endl;
std::cout << camera.get_location().get_y() << std::endl;
Run Code Online (Sandbox Code Playgroud)
#ifndef CAMERA_H
#define CAMERA_H
#include "vector.h"
class Camera {
private:
Vector location;
public:
Vector get_location();
void set_location(Vector);
};
#endif
Run Code Online (Sandbox Code Playgroud)
#include "camera.h"
Vector Camera::get_location() { return location; }
void Camera::set_location(Vector l) { location = l; }
Run Code Online (Sandbox Code Playgroud) 似乎双括号初始化增加了开销.
在方法中使用大括号是否也会降低性能?
例如.
public class DoIReducePerformanceToo {
public void aMethod() {
{
// Is it a bad idea to use these?
}
}
}
Run Code Online (Sandbox Code Playgroud)
我看了一下Java的语法,似乎这被归类为一个块:
Block:
{ BlockStatements }
BlockStatements:
{ BlockStatement }
BlockStatement:
LocalVariableDeclarationStatement
ClassOrInterfaceDeclaration
[Identifier :] Statement
Run Code Online (Sandbox Code Playgroud)
但我不确定语法双括号初始化的位置在哪里.
我的问题:在方法中使用块语句会降低Java中的性能吗?这些块与双支撑初始化具有相同的性质吗?
编辑:
内课即时:
ClassCreatorRest: Arguments [ClassBody]
ClassBody:
{ { ClassBodyDeclaration } }
Run Code Online (Sandbox Code Playgroud) 一个类的公共成员如何在java中造成破坏?有人可以用例子解释一下吗?我试图创造这种情况,但不能成功.我只是发现它们等同于'protected'访问修饰符.
我有以下界面:
public interface Gravy {
public List<Giblet> getGiblets();
public Giblet getGiblet(String id);
public int getNumGiblets();
public void addGiblet();
public void removeGiblet(Giblet giblet);
public List<Carrot> getCarrots();
public Carrot getCarrot(String id);
public int getNumCarrots();
public void addCarrot();
public void removeCarrot(Carrot carrot);
public List<Gravy> getGravies();
public Gravy getGravy(String id);
public int getNumGravies();
public void addGravy();
public void removeGravy(Gravy gravy);
}
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,我的作品中有一个重复出现的模式Gravy.甲Gravy对象可以包含内脏,胡萝卜,和其他(较小)肉汁.所有这些都可以添加,删除或查询.
有两点需要注意:
Carrots和Giblets彼此有点共同之处,但两者都与Gravys 有很大不同.
我可能需要稍后添加更多项目(因此需要重构)...
是否可以减少上面的代码,以便"动词"只写一次?
我正在使用Haskell和OpenGL.OpenGL有一种使用一组统一函数加载变量的方法:
glUniform1i location intValue
glUniform1f location floatValue
glUniform2i location intValue1 intValue2
... etc.
Run Code Online (Sandbox Code Playgroud)
我试图将其翻译成更惯用的Haskell.我想写一个函数:
uniform :: String -> a -> IO ()
uniform location value = ...
Run Code Online (Sandbox Code Playgroud)
我的问题是我调用的函数依赖于的值a.有没有办法做到这一点,而无需编写ň不同的功能?
我正在编写 C++ 代码,并且我有一个包含许多成员方法的类。例如,print()和act();前者不会修改类的内部结构(即变量i),而后者会。
class Example {
int i;
void act() { i++; };
void print() { };
Run Code Online (Sandbox Code Playgroud)
在这种情况下这是显而易见的,但是对于较长的方法,或者定义和声明已经分开的方法,哪些方法可以改变对象并不明显。有没有一种方法可以明确表示方法是不可修改的?
(作为另一种语言的例子,在 CI 中可能会传递一个指向对象的指针。如果一个函数是非修改的,我会传递一个常量指针。)
我正在使用以下形式的元组填充的列表:
tups = [(1, 2, 4.56), (2, 1, 1.23), (1, 3, 2.776), ...]
Run Code Online (Sandbox Code Playgroud)
我想执行两个操作.
第一个是查找以数字n开头的所有元组,例如:
def starting_with(n, tups):
'''Find all tuples with tups that are of the form (n, _, _).'''
# ...
Run Code Online (Sandbox Code Playgroud)
第二个是相反的,找到第二个值为n的所有元组:
def middle_with(n, tups):
'''Find all tuples with tups that are of the form (_, n, _).'''
# ...
Run Code Online (Sandbox Code Playgroud)
从某种意义上说,在元组列表上进行模式匹配.我如何在Python中执行此操作?
java ×4
c++ ×2
block ×1
class ×1
flowlayout ×1
haskell ×1
interface ×1
list ×1
opengl ×1
performance ×1
public ×1
python ×1
refactoring ×1
swing ×1
tuples ×1