我有一些重复的代码,我希望将其付诸实践。但问题是我使用全局变量,我无法发送功能,任何想法如何缩短我的代码?
public class MainClass extends JFrame {
private static List<JComboBox> sundayCombo;
private static List<JComboBox> mondayCombo;
private static List<JComboBox> tuesdayCombo;
private static List<JComboBox> wednesdayCombo;
private static List<JComboBox> thursdayCombo;
private static List<JComboBox> fridayCombo;
private static List<JComboBox> saturdayCombo;
public static void main(String[] args) {
sundayCombo = new ArrayList<>();
mondayCombo = new ArrayList<>();
tuesdayCombo = new ArrayList<>();
wednesdayCombo = new ArrayList<>();
thursdayCombo = new ArrayList<>();
fridayCombo = new ArrayList<>();
saturdayCombo = new ArrayList<>();
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainClass frame = new MainClass();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public MainClass() {
for (String string : mylist) {
//Create function for the next section
//Some thing like this:
//createPanel(sundayCombo,panel_sunday_therapist);
JComboBox comboBox1 = createComboBox();
sundayCombo.add(comboBox1);
panel_sunday_nurse.add(comboBox1);
panel_sunday_nurse.add(Box.createVerticalStrut(STRUT_SIZE));
JComboBox comboBox2 = createComboBox();
mondayCombo.add(comboBox2);
panel_monday_nurse.add(comboBox2);
panel_monday_nurse.add(Box.createVerticalStrut(STRUT_SIZE));
JComboBox comboBox3 = createComboBox();
tuesdayCombo.add(comboBox3);
panel_tuesday_nurse.add(comboBox3);
panel_tuesday_nurse.add(Box.createVerticalStrut(STRUT_SIZE));
JComboBox comboBox4 = createComboBox();
wednesdayCombo.add(comboBox4);
panel_wednesday_nurse.add(comboBox4);
panel_wednesday_nurse.add(Box.createVerticalStrut(STRUT_SIZE));
JComboBox comboBox5 = createComboBox();
thursdayCombo.add(comboBox5);
panel_thursday_nurse.add(comboBox5);
panel_thursday_nurse.add(Box.createVerticalStrut(STRUT_SIZE));
JComboBox comboBox6 = createComboBox();
fridayCombo.add(comboBox6);
panel_friday_nurse.add(comboBox6);
panel_friday_nurse.add(Box.createVerticalStrut(STRUT_SIZE));
JComboBox comboBox7 = createComboBox();
saturdayCombo.add(comboBox7);
panel_saturday_nurse.add(comboBox7);
panel_saturday_nurse.add(Box.createVerticalStrut(STRUT_SIZE));
}
}
private JComboBox createComboBox() {
//bla bla
return comboBox;
}
}
答案 0 :(得分:1)
您可以创建一个函数,将您希望添加的组合框作为函数的参数,即
using System;
using System.Net.Http;
using System.Threading.Tasks;
namespace TestApi
{
public class MyController : Controller
{
private const string ApiUrlString = "https://apiUrl.com";
private static readonly Uri ApiUri = new Uri(ApiUrlString);
private static readonly HttpClient RestClient;
static MyController()
{
this.RestClient = new HttpClient{
BaseAddress = ApiUri
}
this.RestClient.DefaultRequestHeaders.Accept.Clear();
this.RestClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
RestClient.DefaultRequestHeaders.TryAddWithoutValidation("APIAccessToken", "token1");
RestClient.DefaultRequestHeaders.TryAddWithoutValidation("UserToken", "token2");
}
public async Task<IActionResult> ApiTest()
{
return this.Ok(await this.RestClient.GetStringAsync("somedata/search?text=test"));
}
}
}
您可以将该功能称为private void addComboBox(List<JComboBox> weekdayCombo, JPanel weekdayPanel) {
ComboBox comboBox1 = createComboBox();
weekdayCombo.add(comboBox1);
weekdayPanel.add(comboBox1);
weekdayPanel.add(Box.createVerticalStrut(STRUT_SIZE));
}