java checkbox数组_Java中的复选框数组

我想有6个复选框,按下按钮后做一些事情,您有例子吗?

还可以使用一系列复选框吗?

我想避免:

Checkbox cb1 = new Checkbox("A");

Checkbox cb2 = new Checkbox("B");

我正在做类似的事情:

JPanel panel = new JPanel();

JFrame frame = new JFrame("the title");

final JTextArea txt = new JTextArea(20, 30);

Button boton = new Button( "DO");

panel.add(txt);

panel.add(boton);

frame.add(panel);

frame.add(panel);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(500, 500);

frame.setVisible(true);

boton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

txt.setText("");

/*

How would be the logic of array of checkboxes

if checbox[0] is active do action 0

if checbox[1] is active do action 1

if checbox[2] is active do action 2

if checbox[3] is active do action 3

if checbox[4] is active do action 4

if checbox[5] is active do action 5

if checbox[0] and checbox[1] is active do action 6

if checbox[0] and checbox[2] is active do action 7

if checbox[0] and checbox[3] is active do action 8

etc...

*/

}

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,在前端页面,需要为复选框设置一个相同的 name 值,然后使用 JavaScript 获取选复选框的值,将其转换为数组并提交给后端 servlet。 HTML代码示例: ``` <form> <label><input type="checkbox" name="fruit" value="apple"> Apple</label> <label><input type="checkbox" name="fruit" value="banana"> Banana</label> <label><input type="checkbox" name="fruit" value="orange"> Orange</label> <button type="button" onclick="submitForm()">Submit</button> </form> ``` JavaScript代码示例: ``` function submitForm() { // 获取选复选框的值,转换为数组 var selectedFruits = Array.from(document.querySelectorAll('input[name="fruit"]:checked')).map(e => e.value); // 创建XMLHttpRequest对象 var xhr = new XMLHttpRequest(); // 设置请求方法和URL xhr.open('POST', '/servletName'); // 设置请求头 xhr.setRequestHeader('Content-Type', 'application/json'); // 发送请求 xhr.send(JSON.stringify(selectedFruits)); // 监听响应 xhr.onreadystatechange = function() { if (xhr.readyState === 4 && xhr.status === 200) { // 获取响应数据 var responseData = xhr.responseText; // 将响应数据转换为数组并显示在页面上 var responseArray = JSON.parse(responseData); var output = responseArray.join(', '); document.querySelector('#output').textContent = output; } }; } ``` 在后端 servlet ,使用 request.getParameterValues() 方法获取前端传递的数组,并进行相应的操作,然后将处理后的数组转换为字符串并返回给前端页面。 Java代码示例: ``` protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 获取前端传递的数组 String[] selectedFruits = request.getParameterValues("fruit"); // 进行相应的操作 // ... // 将处理后的数组转换为字符串并返回给前端页面 String responseString = Arrays.toString(selectedFruits); response.setContentType("application/json"); PrintWriter out = response.getWriter(); out.print(responseString); out.flush(); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值