java 弹窗 双按钮,简单的弹出式Java表单,至少包含两个字段

When the user clicks a button, I want to show a popup form that should have at least two JTextFields and two JLabels, so using JOptionPane.showInputDialog is not a possibility.

解决方案

You should at least consider one of the JOptionPane methods such as showInputDialog() or showMessageDialog().

Addendum: The choice to use JOptionPane hinges more on the suitability of modality, rather than on the number of components shown. See also How to Make Dialogs.

Addendum: As noted in a comment by @camickr, you can set the focus to a particular component using the approach discussed in Dialog Focus, cited here.

Zg3E1.png

package gui;

import java.awt.EventQueue;

import java.awt.GridLayout;

import javax.swing.*;

/** @see https://stackoverflow.com/a/3002830/230513 */

class JOptionPaneTest {

private static void display() {

String[] items = {"One", "Two", "Three", "Four", "Five"};

JComboBox combo = new JComboBox<>(items);

JTextField field1 = new JTextField("1234.56");

JTextField field2 = new JTextField("9876.54");

JPanel panel = new JPanel(new GridLayout(0, 1));

panel.add(combo);

panel.add(new JLabel("Field 1:"));

panel.add(field1);

panel.add(new JLabel("Field 2:"));

panel.add(field2);

int result = JOptionPane.showConfirmDialog(null, panel, "Test",

JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);

if (result == JOptionPane.OK_OPTION) {

System.out.println(combo.getSelectedItem()

+ " " + field1.getText()

+ " " + field2.getText());

} else {

System.out.println("Cancelled");

}

}

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

@Override

public void run() {

display();

}

});

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 Element UI 的 Dialog 对话框组件来实现在表格中点击按钮弹窗的功能。具体实现步骤如下: 1. 在表格中添加一个操作列,该列包含一个按钮,用于触发弹窗。 2. 在按钮中绑定一个点击事件,该事件会调用一个方法。 3. 在该方法中,使用 Element UI 的 Dialog 组件来创建一个弹窗,并设置弹窗的内容。 4. 在弹窗中添加需要的表单元素或其他组件,并进行相关的操作。 下面是一个简单的代码示例,以帮助你更好地理解如何实现这个功能: ``` <template> <el-table :data="tableData"> <el-table-column prop="name" label="姓名"></el-table-column> <el-table-column prop="age" label="年龄"></el-table-column> <<el-table-column label="操作"> <template slot-scope="scope"> <el-button type="primary" @click="handleClick(scope.row)">编辑</el-button> </template> </el-table-column> </el-table> <el-dialog :visible.sync="dialogVisible"> <el-form> <el-form-item label="姓名"> <el-input v-model="editRow.name"></el-input> </el-form-item> <el-form-item label="年龄"> <el-input v-model="editRow.age"></el-input> </el-form-item> </el-form> <div slot="footer" class="dialog-footer"> <el-button @click="dialogVisible = false">取消</el-button> <el-button type="primary" @click="saveEdit">保存</el-button> </div> </el-dialog> </template> <script> export default { data() { return { tableData: [ { name: '张三', age: 18 }, { name: '李四', age: 20 } ], dialogVisible: false, editRow: {} } }, methods: { handleClick(row) { this.editRow = Object.assign({}, row) this.dialogVisible = true }, saveEdit() { // 在这里进行保存操作 this.dialogVisible = false } } } </script> ``` 在这个示例中,我们在表格中添加了一个“操作”列,该列包含一个“编辑”按钮。当用户点击该按钮时,会触发 `handleClick` 方法。该方法会将当前行的数据复制到 `editRow` 对象中,并将 `dialogVisible` 属性设置为 `true`,以显示一个弹窗弹窗包含一个表单,用户可以在其中编辑数据。当用户点击“保存”按钮时,会触发 `saveEdit` 方法,该方法可以在这里进行保存操作。最后,我们将 `dialogVisible` 属性设置为 `false`,以关闭弹窗。 需要注意的是,在这个示例中,我们使用了 `Object.assign` 方法来创建一个新的对象,以确保我们不会直接修改原始数据。这是一个非常重要的步骤,因为直接修改原始数据可能会导致一些意外的结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值