gpt4 book ai didi

java - 如何在按 Enter 键时阻止 JDialog 关闭?

转载 作者:行者123 更新时间:2023-12-02 01:43:57 24 4
gpt4 key购买 nike

我试图阻止我的 JDialog 在按下 Enter 键时关闭。我已经尝试过使用 getRootPane().setDefaultButton(null); ,但它仍然无法正常工作。我从主 JFrame 为我的 JDialog 调用此构造函数。这是我的代码:

public class CustomSaleDialog extends JDialog {

private final JPanel contentPanel = new JPanel();
private JTextField txtScanBarcode;
private JTextField txtNumSold;
private String barcode;
private int numTickets;

public void setNumTickets(int num) {
numTickets = num;
}

public void setBarCode(String code) {
barcode = code;
}

public int getNumTickets() {
return numTickets;
}

public String getBarCode() {
return barcode;
}

public CustomSaleDialog(JFrame f) {
getRootPane().setDefaultButton(null); //This is what I tried
setTitle("Custom Sale");
setBounds(100, 100, 450, 300);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setModalityType(ModalityType.APPLICATION_MODAL);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);


{
JLabel lblMakeACustom = new JLabel("Make a Custom Sale");
lblMakeACustom.setFont(new Font("Tahoma", Font.PLAIN, 25));
lblMakeACustom.setHorizontalAlignment(SwingConstants.CENTER);
lblMakeACustom.setBounds(10, 11, 414, 44);
contentPanel.add(lblMakeACustom);
}
{
JLabel lblScanTicket = new JLabel("Scan Ticket");
lblScanTicket.setFont(new Font("Tahoma", Font.PLAIN, 20));
lblScanTicket.setBounds(20, 73, 102, 20);
contentPanel.add(lblScanTicket);
}
{
JLabel lblNumberOfTickets = new JLabel("Number of Tickets Being Sold");
lblNumberOfTickets.setFont(new Font("Tahoma", Font.PLAIN, 20));
lblNumberOfTickets.setBounds(20, 136, 262, 25);
contentPanel.add(lblNumberOfTickets);
}
{
txtScanBarcode = new JTextField();
txtScanBarcode.setBounds(132, 73, 284, 20);
contentPanel.add(txtScanBarcode);
txtScanBarcode.setColumns(10);
}
{
txtNumSold = new JTextField();
txtNumSold.setBounds(292, 141, 124, 20);
contentPanel.add(txtNumSold);
txtNumSold.setColumns(10);
}
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JButton okButton = new JButton("OK");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
barcode = txtScanBarcode.getText();
String numTickets = txtNumSold.getText();
int numTicketsInt = 0;
if (barcode.length() > 0
&& numTickets.matches("[0-9]+")
&& numTickets.length() >= 1) {
numTicketsInt = Integer.parseInt(numTickets);
setBarCode(barcode);
setNumTickets(numTicketsInt);
}
setVisible(false);
dispose();
}
});
okButton.setActionCommand("OK");
buttonPane.add(okButton);
getRootPane().setDefaultButton(okButton);
}
{
JButton cancelButton = new JButton("Cancel");
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
dispose();
}
});
cancelButton.setActionCommand("Cancel");
buttonPane.add(cancelButton);
}
}


setLocationRelativeTo(f);
setVisible(true);
}

}

最佳答案

你忘记删除...

getRootPane().setDefaultButton(okButton);

这个...

Bad layout

这就是为什么你不使用空布局

关于java - 如何在按 Enter 键时阻止 JDialog 关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53984410/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com