gpt4 book ai didi

java - 按下按钮时更新 JTextField

转载 作者:太空宇宙 更新时间:2023-11-04 12:43:18 26 4
gpt4 key购买 nike

我在按钮上有一个 if 语句,单击该语句会将 JTextField 更改为 £35.00。我还希望能够使用 JRadioButton,单击该按钮会将 JTextField 更改为 £70.00。

代码:

sesh1.addActionListener(e2->{
if (sesh1.isSelected())
tSesh1.setText("35.00");
});

tSesh1 是文本字段。

另外:

//ActionListener for saving session info to text file
JButton submitInvoice = new JButton ("Submit");
sPanel.add(submitInvoice);
submitInvoice.addActionListener(e9->{
try{
BufferedWriter bw = new BufferedWriter(new FileWriter("RegInvoice_0to2.txt",true));
bw.write("---------------Booking Invoice---------------");
bw.write("\r\n");
bw.write("---------------0 To 2 Year Olds--------------");
bw.write("\r\n");
bw.write("[Sessions Selected]");
bw.write("\r\n");
bw.write("All Day: £"); bw.write(tSesh1.getText());
bw.write(" On");
//
//Displays the days selected for All Day Sessions
if(mon1.isSelected()){
bw.write(" Monday"); bw.write(" ");
tSesh1.setText("70.00"); // This wont change the text field
}

最佳答案

我制作了这个小演示,获取您的句子,与选择 JRadioButtons 并影响 JTextField 相关。

它满足您的要求。它们是你自己的句子。

I have an if statement on a button that when clicked will change a JTextField to £35.00. I also want to be able to use a JRadioButton that when clicked will change the JTextField to £70.00.

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class JRadioTest extends JFrame {

public JRadioTest() {

setSize(500,500);
setTitle("JRadioTest");
setResizable(true);

initComponents();

setVisible(true);

}

private void initComponents() {

JPanel panel = new JPanel();
JLabel label = new JLabel("My text field");

panel.add(label);

JTextField tSesh1 = new JTextField(20);
panel.add(tSesh1);

JRadioButton sesh1 = new JRadioButton("35.00");

sesh1.addActionListener(e -> {
if (sesh1.isSelected()) {
tSesh1.setText("35.00");
}
});

panel.add(sesh1);

JRadioButton mon1 = new JRadioButton("70.00");
panel.add(mon1);

JButton submitInvoice = new JButton("Submit");

submitInvoice.addActionListener(e -> {
if (mon1.isSelected()) {
tSesh1.setText("70.00");
}
});

panel.add(mon1);
panel.add(submitInvoice);

this.add(panel);

}


public static void main(String args[]){

SwingUtilities.invokeLater(new Runnable() {
public void run() {
new JRadioTest();

}
});

}

}

测试一下。

如果当您选择 mon1 并按 submitInvoice 时,您的 JTextField 未显示“70.00”,可能是因为您尝试写入文件时引发了异常。

关于java - 按下按钮时更新 JTextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36603907/

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