もちろん、それが動作することができます。 メソッド、クラス、型のいずれでもパラメータとして何かを渡すことができます。ここで
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.ます。また、作成することができます単にあなたに準備ができているメソッドを提供しています。
ここ
は、あなたがこれを使用することができる方法です。
for(int index = 0; index <= 2; index++){
JButton button = setUpComponent(button);
//do whatever you want to do
}
3.しかし、これについて移動する最良の方法私の意見では
は、新しいクラスや設定がボタンを作成することです。そうのような:ここで
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);
}
はい、できます。通常の関数のように呼び出すだけです。スイングはスレッドセーフではないので注意してください。これはEDTで行う必要があります。 – markspace
Re、「私はそれらを1つの機能にすることはできますか?」あなたがそれをしたとき何が起こったのですか? –