我正在制作一个项目来制作一个小费计算器,它有两个15%和20%的复选框和一个文本框,允许你输入自己的%,我只是无法弄清楚如何链接复选框和文本框会影响答案。我只是希望有人能指出将这些事情联系在一起所需要的东西。
INCLUDE Irvine32.inc
.data
count = 5
scores DWORD count DUP(0)
prompt BYTE "Please type an integer score: ", 0
.code
GetScores PROTO, dwArray:PTR DWORD, arraySize : DWORD
main PROC
exit
main ENDP
GetScores PROC, dwArray:PTR DWORD, arraySize : DWORD
ret
GetScores ENDP
END main
答案 0 :(得分:2)
基本答案是,检查每个JCheckBox
以查看它是否已被选中,并根据选择的那个,将它所代表的金额分配给某个变量。这需要您了解有关JCheckBox
的问题。
你可以:
putClientProperty
到getClientProperty
功能来针对每个字段中的键存储对象,个人而言,这有点脏,因为你需要进行对象转换,容易出错Action
API 此示例使用简单的“模型”和Action
API来设置基本功能。
“模型”保持当前选择的百分比。选择其中一个可用选项后,模型会自动更新。当用户按下“计算”按钮时,从“模型”获得尖端百分比并进行计算
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
import javax.swing.AbstractAction;
import static javax.swing.Action.NAME;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JSpinner subTotal;
private JSpinner custom;
private JButton btnCalculate;
private TipModel model;
private JLabel tipAmountLabel;
private JLabel billTotalLabel;
public TestPane() {
setBorder(new EmptyBorder(10, 10, 10, 10));
subTotal = new JSpinner(new SpinnerNumberModel());
subTotal.setValue(0d);
subTotal.setEditor(new JSpinner.NumberEditor(subTotal, "$#0.0#"));
model = new TipModel();
custom = new JSpinner(new SpinnerNumberModel(0, 0, 1, 0.05));
custom.setEditor(new JSpinner.NumberEditor(custom, "##%"));
JCheckBox cb15 = new JCheckBox(new TipAction(model, 0.15d));
JCheckBox cb20 = new JCheckBox(new TipAction(model, 0.20d));
JCheckBox cbCustom = new JCheckBox(new CustomTipAction(model, custom));
ButtonGroup bg = new ButtonGroup();
bg.add(cb15);
bg.add(cb20);
bg.add(cbCustom);
btnCalculate = new JButton("Calculate Tip");
btnCalculate.setMargin(new Insets(10, 10, 10, 10));
tipAmountLabel = new JLabel("...");
billTotalLabel = new JLabel("...");
billTotalLabel.setHorizontalAlignment(JLabel.CENTER);
tipAmountLabel.setHorizontalAlignment(JLabel.CENTER);
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.insets = new Insets(2, 2, 2, 2);
add(subTotal, gbc);
gbc.anchor = GridBagConstraints.WEST;
gbc.gridy++;
gbc.fill = GridBagConstraints.NONE;
add(cb15, gbc);
gbc.gridy++;
add(cb20, gbc);
gbc.gridwidth = 1;
gbc.gridy++;
add(cbCustom, gbc);
gbc.gridx++;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(custom, gbc);
gbc.gridx = 0;
gbc.gridy++;
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(btnCalculate, gbc);
gbc.anchor = GridBagConstraints.CENTER;
gbc.gridy++;
add(tipAmountLabel, gbc);
gbc.gridy++;
add(billTotalLabel, gbc);
btnCalculate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
double tipPercentage = model.getTipPercentage();
if (tipPercentage > 0.0) {
double subTotalAmount = (double) subTotal.getValue();
double tipAmount = subTotalAmount * tipPercentage;
double billTotal = subTotalAmount + tipAmount;
tipAmountLabel.setText("Tip amount: " + NumberFormat.getCurrencyInstance().format(tipAmount));
billTotalLabel.setText("Bill total: " + NumberFormat.getCurrencyInstance().format(billTotal));
} else {
JOptionPane.showMessageDialog(TestPane.this, "Please provide a valid tip percentage");
}
}
});
}
public class TipModel {
private double tipPercentage;
public double getTipPercentage() {
return tipPercentage;
}
public void setTipPercentage(double tipPercentage) {
this.tipPercentage = tipPercentage;
}
}
public class TipAction extends AbstractAction {
private TipModel model;
private double tipPercentage;
public TipAction(TipModel model, double tipPercentage) {
this.model = model;
this.tipPercentage = tipPercentage;
putValue(NAME, NumberFormat.getPercentInstance().format(tipPercentage));
}
@Override
public void actionPerformed(ActionEvent e) {
model.setTipPercentage(tipPercentage);
}
}
public class CustomTipAction extends AbstractAction {
private TipModel model;
private JSpinner spinner;
public CustomTipAction(TipModel model, JSpinner spinner) {
this.model = model;
this.spinner = spinner;
spinner.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
model.setTipPercentage((double) spinner.getValue());
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
model.setTipPercentage((double) spinner.getValue());
}
}
}
}
避免使用null
布局,像素完美布局是现代ui设计中的一种幻觉。影响组件个体大小的因素太多,您无法控制。 Swing旨在与布局管理器一起工作,放弃这些将导致问题和问题的终结,您将花费越来越多的时间来纠正
看看:
了解更多详情