gpt4 book ai didi

java - 使用一个文本字段中的值到另一个文本字段中

转载 作者:行者123 更新时间:2023-12-02 10:22:45 26 4
gpt4 key购买 nike

我正在尝试计算事件中单独文本字段的小计,该事件涉及用户选择房间数和天数(基于入住和退房日期)。

此文本字段(小计)将有效地乘以(天数 * 房间数 * 房价),并将在用户更改持续时间或房间数量时更新。

请注意我的 GUI 是基于拖放的。

private void checkDoubleActionPerformed(java.awt.event.ActionEvent evt) {                                            
// User clicks "Check Availability" button after selecting number of Double Rooms required and duration of stay

String value2 = spinner2.getValue().toString(); //Getting number of room using spinner2

//.set a default current date to Check in. Can be changed by customer
cid_chooser2.getJCalendar().setMinSelectableDate(new Date());
cid_chooser2.setMinSelectableDate(new Date());

Date d1 = null; //initial value of check in date
Date d2 = null; // initial value of check out date

try {
d1 = cid_chooser2.getDate();
d2 = cod_chooser2.getDate();

long duration = d2.getTime() - d1.getTime(); //calculationg duration in days
long days = TimeUnit.MILLISECONDS.toDays(duration);

if (days > 0) {

JOptionPane pane = new JOptionPane("You selected " + value2 + " Double Rooms for: " + days,JOptionPane.PLAIN_MESSAGE, JOptionPane.DEFAULT_OPTION);
JDialog dialog = pane.createDialog(null, "Customer Notification");
dialog.setSize(new Dimension(400, 200));
dialog.show();
}
else {JOptionPane.showMessageDialog(null, "Check out Date needs to be after Check in Date ");
}
}
catch (NullPointerException ex1) {

if (d1 == null && d2 == null) {

JOptionPane.showMessageDialog(null, "Please enter missing check in AND check out dates.");
}

else if (d2 == null) {
JOptionPane.showMessageDialog(null, "Please enter missing check out date."
+ "\nyour check out date should be at least a day after your check in date");
}
else if (d1 == null) {

JOptionPane.showMessageDialog(null, "Please enter missing check in date."
+ "\nyour check in date should be at least today");
}
}

}

////////////// separate JTextField to calculate sub-total////////////

private void subTotal2ActionPerformed(java.awt.event.ActionEvent evt) {
// sub-Total based on number of Double Rooms selected * duration (days) * unit price

}

最佳答案

我希望这个示例程序能够向您展示如何做到这一点。在这里,我使用 JDateChooser 类,可以从以下位置下载:https://toedter.com/jcalendar/

如果您使用其他日期组件,我确信该类也会有类似的 API,例如您可以使用的 addPropertyChangeListener()

import com.toedter.calendar.JDateChooser;

import javax.swing.*;
import javax.swing.event.*;
import java.awt.GridLayout;
import java.awt.event.*;
import java.beans.*;
import java.util.Date;

public class CalculateSubTotal {

private static JDateChooser checkin = new JDateChooser();
private static JDateChooser checkout = new JDateChooser();
private static JSpinner rooms = new JSpinner(new SpinnerNumberModel(1, 0, 10, 1));
private static JTextField subTotal = new JTextField(20);
private static JButton button = new JButton("Check availability");

public static void main(String[] args) {

checkin.getDateEditor().addPropertyChangeListener("date", new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
calculateSubTotal(checkin.getDate(), checkout.getDate(), (Integer) rooms.getValue(), 50);
}
});

checkout.getDateEditor().addPropertyChangeListener("date", new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
calculateSubTotal(checkin.getDate(), checkout.getDate(), (Integer) rooms.getValue(), 50);
}
});

rooms.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
calculateSubTotal(checkin.getDate(), checkout.getDate(), (Integer) rooms.getValue(), 50);
}
});

button.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
calculateSubTotal(checkin.getDate(), checkout.getDate(), (Integer) rooms.getValue(), 50);
}
});

JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().setLayout(new GridLayout(6, 2));
f.getContentPane().add(new JLabel("Price"));
f.getContentPane().add(new JLabel("50"));
f.getContentPane().add(new JLabel("Check in"));
f.getContentPane().add(checkin);
f.getContentPane().add(new JLabel("Check out"));
f.getContentPane().add(checkout);
f.getContentPane().add(new JLabel("Number of rooms"));
f.getContentPane().add(rooms);
f.getContentPane().add(new JLabel("Sub total"));
f.getContentPane().add(subTotal);
f.getContentPane().add(button);
f.setBounds(300, 200, 400, 300);
f.setVisible(true);
}

private static void calculateSubTotal(Date checkin, Date checkout, int rooms, int price) {
if (checkin == null || checkout == null) {
return;
}
int sub = getDays(checkin, checkout) * rooms * price;
subTotal.setText(String.valueOf(sub));
}

private static int getDays(Date checkin, Date checkout) {
return (int) ((checkout.getTime() - checkin.getTime()) / (1000 * 60 * 60 * 24));
}
}

关于java - 使用一个文本字段中的值到另一个文本字段中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54209292/

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