简体   繁体   English

Java:如何通过jcombo列表获取表达式

[英]Java : how to get the expression through the jcombo list

How do i create the one line expression using Java swing, link image picture . 如何使用Java swing,链接图像图片创建单行表达式。 the every minute, every day,every month, every weekday and every hour need to convert it to "*" and also all the combo box contain the list of number list number link and weekday contain the click the picture 每分钟,每天,每个月,每个工作日和每个小时都需要将其转换为“ *”,并且所有组合框都包含数字列表编号链接的列表,而工作日包含单击图片

what i want is, if the user select "Every Minute" , "Every day","month = 2", "Weekday = monday", "hour= 3" 我想要的是,如果用户选择“每分钟”,“每天”,“月= 2”,“工作日=星期一”,“小时= 3”

note of weekday JCombo : sunday = 0 , monday = 1, tuesday = 2 ..... 工作日JCombo的注释:星期日= 0,星期一= 1,星期二= 2 .....

the output will print as : * * 2 1 3 输出将打印为:* * 2 1 3

thanks alot. 非常感谢。

i already tried this , my beginning code but cant do much : 我已经尝试过了,这是我的开始代码,但是不能做很多事情:

String sjcb_EM = jcb_EM.getSelectedItem().toString();
    String sjcb_EH = jcb_EH.getSelectedItem().toString();
    String sjcb_ED = jcb_ED.getSelectedItem().toString();
    String sjcb_EEM = jcb_EEM.getSelectedItem().toString();
    String sjcb_EW = jcb_EW.getSelectedItem().toString();

    String vb_1 = sjcb_EM + " " + sjcb_EH + " " + sjcb_ED + " " + sjcb_EEM + " " + sjcb_EW;

System.out.println(vb_1); System.out.println(vb_1);

now i stuck, how to make the expression that i wanted. 现在我坚持,如何使我想要的表达。

Start by building a class which can hold both the display value and the query value... 首先建立一个可以同时包含显示值和查询值的类。

public class WorkoutUnit {
    private String displayValue;
    private String queryValue;

    public WorkoutUnit(String displayValue, String queryValue) {
        this.displayValue = displayValue;
        this.queryValue = queryValue;
    }

    public String getDisplayValue() {
        return displayValue;
    }

    public String getQueryValue() {
        return queryValue;
    }

    @Override
    public String toString() {
        return displayValue;
    }
}

Build a ComboBoxModel using these values... 使用这些值构建一个ComboBoxModel ...

DefaultComboBoxModel<WorkoutUnit> model = new DefaultComboBoxModel<>();
model.addElement(new WorkoutUnit("Every Minute", "*"));
for (int index = 10; index < 61; index += 10) {
    model.addElement(new WorkoutUnit(Integer.toString(index), Integer.toString(index)));
}
JComboBox<WorkoutUnit> cb = new JComboBox(model);

When needed, get the selected item from the combo box and get its query value... 需要时,从组合框中获取所选项目并获取其查询值...

WorkoutUnit unit = (WorkoutUnit)cb.getSelectedItem();
System.out.println("Query = " + unit.getQueryValue());

In this example, I've used toString to provide the display value to the JComboBox , this is not my preferred solution, I'd prefer to use a ListCellRenderer as demonstrated here 在这个例子中,我使用toString提供的显示值到JComboBox ,这不是我的首选的解决方案,我宁愿使用一个ListCellRenderer作为证明这里

Oh, and because it looks like you're heading down a database query route, you should also have a look at Using Prepared Statements 哦,因为您似乎正在寻找数据库查询路线,所以您还应该看看使用准备好的语句

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM