请原谅我的问题,如果它看起来很愚蠢,但我很好奇.我正在制作一个Java程序,它将有一个GUI,并对整个属性的想法感到好奇.为什么在我们可以向类添加数据时使用它们?例如:
class myButton extends Button {
private boolean booleanProperty = false;
myButton(args...) {
// Do something with the property
}
public void setProperty(boolean value) {
this.booleanProperty = value;
}
public boolean getProperty() {
return this.booleanProperty;
}
}
Run Code Online (Sandbox Code Playgroud)
似乎工作得很好,可以存储关于按钮的自定义实现的其他信息.但是关于:
class myButton extends Button {
private SimpleBooleanProperty booleanProperty = new SimpleBooleanProperty(false);
myButton(args...) {
// Do something with the property
}
public void setProperty(boolean value) {
this.booleanProperty.set(value);
}
public boolean getProperty() {
return this.booleanProperty.get();
}
}
Run Code Online (Sandbox Code Playgroud)
唯一真正的区别,我看到(纠正我,如果我错了)是你可以将听众附加到属性值,但我觉得好像不仅仅是那个.想法?
就像一个背后的故事,我真的进入java并一直在观看所有这些视频等在线,我决定虽然学习它是好的,如果我不能实际使用它,它毫无意义.所以我带了一个计算器.除了一件事,一切都很顺利.
为了展示我的懒惰,我创建了一个接受参数的方法,并使用它们通过简单的方法调用来构建JButton.该代码是:
public class GUI_Element_Methods{
private JButton button1;
private JButton numpad[];
public void createButton(String buttonText, ActionListener eventMethod, boolean visible, String tooltipText){
button1 = new JButton(buttonText);
button1.addActionListener(eventMethod);
button1.setVisible(visible);
button1.setToolTipText(tooltipText);
}
public JButton getButton1(){
return(button1);
}
Run Code Online (Sandbox Code Playgroud)
被召唤:
guiElement.createButton("+", asHandler, true, "Addition");
add(guiElement.getButton1());
Run Code Online (Sandbox Code Playgroud)
我工作得很好,但是我无法检测一个动作是否是事件处理程序中的某个按钮,因为它们都通过这个"button1"JButton运行.例如,毕达哥拉斯定理,它有两个基于你想要解决的变体,我想只使用一个事件处理程序来检测哪一个被按下.
if(event.getSource().equals()){
}else{
}
Run Code Online (Sandbox Code Playgroud)
那是我假设我会把它,我只是不知道如何引用方法创建按钮.