是否可以使用组件(如 jButton)作为函数中的参数?
Is it possible to use component (like jButton) as an argument in a function?
如果我对多个按钮使用完全相同的代码行,我可以使用组件(在本例中为按钮)作为函数的参数而不是使用变量吗?这将使我的工作变得容易得多。如果我有这样的东西:
a1.setText("something");
a1.setBackground(Color.BLACK);
a1.setForeground(Color.WHITE);
a2.setText("something");
a2.setBackground(Color.BLACK);
a2.setForeground(Color.WHITE);
a3.setText("something");
a3.setBackground(Color.BLACK);
a3.setForeground(Color.WHITE);
我可以把它们变成一个函数吗:
public void buttonFunction(Button something){
something.setText("something");
something.setBackground(Color.BLACK);
something.setForeground(Color.WHITE);
}
如果可以,我怎么可以?
是的。
你所做的尝试就是做到这一点的方法。
public void buttonFunction(JButton something){
something.setText("something");
something.setBackground(Color.BLACK);
something.setForeground(Color.WHITE);
}
您需要做的就是在创建 JButton 对象后调用此函数。
当然可以。
您可以将任何东西作为参数传递,甚至是方法、类 和类型。
1.这里有一个简单的方法可以完成你的要求:
public static Component setUpComponent(Component component){
if(component instanceof JButton){
JButton button = (JButton)component;
button.setText("something");
button.setBackground(Color.BLACK);
button.setForeground(Color.WHITE);
return button;
} else if(component instanceof <"Some other class, like JPanel">){
//do something else for that class
}
throw new Exception("Invalid Object");
return null;
}
以下是使用方法:
for(int index = 0; index <= 2; index++){
JButton button = new JButton();
setUpComponent(button);
//do whatever you want to do
}
2. 你也可以创建一个简单的方法来给你一个准备好的按钮:
public static JButton getButton(){
JButton button = new JButton("something");
button.setBackground(Color.BLACK);
button.setForeground(Color.WHITE);
return button;
}
以下是使用方法:
for(int index = 0; index <= 2; index++){
JButton button = setUpComponent(button);
//do whatever you want to do
}
3. 但我认为最好的方法是创建一个新的 Class 并在那里设置按钮。像这样:
public class CustomButton extends JButton{
public CustomButton(){
this("something",Color.BLACK,Color.WHITE);
}
public CustomButton(String text, Color backColor, Color frontColor){
super(something);
setBackground(backColor);
setForeground(frontColor);
}
}
以下是使用方法:
for(int index = 0; index <= 2; index++){
JButton button = new CustomButton();
//same as:
//CustomButton button = new CustomButton();
//or
//JButton button = new CustomButton("something",Color.BLACK,Color.WHITE);
}
如果我对多个按钮使用完全相同的代码行,我可以使用组件(在本例中为按钮)作为函数的参数而不是使用变量吗?这将使我的工作变得容易得多。如果我有这样的东西:
a1.setText("something");
a1.setBackground(Color.BLACK);
a1.setForeground(Color.WHITE);
a2.setText("something");
a2.setBackground(Color.BLACK);
a2.setForeground(Color.WHITE);
a3.setText("something");
a3.setBackground(Color.BLACK);
a3.setForeground(Color.WHITE);
我可以把它们变成一个函数吗:
public void buttonFunction(Button something){
something.setText("something");
something.setBackground(Color.BLACK);
something.setForeground(Color.WHITE);
}
如果可以,我怎么可以?
是的。 你所做的尝试就是做到这一点的方法。
public void buttonFunction(JButton something){
something.setText("something");
something.setBackground(Color.BLACK);
something.setForeground(Color.WHITE);
}
您需要做的就是在创建 JButton 对象后调用此函数。
当然可以。 您可以将任何东西作为参数传递,甚至是方法、类 和类型。
1.这里有一个简单的方法可以完成你的要求:
public static Component setUpComponent(Component component){
if(component instanceof JButton){
JButton button = (JButton)component;
button.setText("something");
button.setBackground(Color.BLACK);
button.setForeground(Color.WHITE);
return button;
} else if(component instanceof <"Some other class, like JPanel">){
//do something else for that class
}
throw new Exception("Invalid Object");
return null;
}
以下是使用方法:
for(int index = 0; index <= 2; index++){
JButton button = new JButton();
setUpComponent(button);
//do whatever you want to do
}
2. 你也可以创建一个简单的方法来给你一个准备好的按钮:
public static JButton getButton(){
JButton button = new JButton("something");
button.setBackground(Color.BLACK);
button.setForeground(Color.WHITE);
return button;
}
以下是使用方法:
for(int index = 0; index <= 2; index++){
JButton button = setUpComponent(button);
//do whatever you want to do
}
3. 但我认为最好的方法是创建一个新的 Class 并在那里设置按钮。像这样:
public class CustomButton extends JButton{
public CustomButton(){
this("something",Color.BLACK,Color.WHITE);
}
public CustomButton(String text, Color backColor, Color frontColor){
super(something);
setBackground(backColor);
setForeground(frontColor);
}
}
以下是使用方法:
for(int index = 0; index <= 2; index++){
JButton button = new CustomButton();
//same as:
//CustomButton button = new CustomButton();
//or
//JButton button = new CustomButton("something",Color.BLACK,Color.WHITE);
}