gpt4 book ai didi

java - 在jtable中计算%,总计

转载 作者:行者123 更新时间:2023-11-29 20:41:59 27 4
gpt4 key购买 nike

我的项目即将结束,但在解决它时遇到了一些问题。我是一名厨师,想要自己的应用程序与他自己的业务相关

我的表单称为“菜谱”,并包含数量、单价、使用百分比。

假设您有 1 公斤苹果,价格为 5 欧元。清洁结束后,您只剩下 800 克苹果。所以我多付了 20%。

示例

  • 项目始终为 100%,
  • 数量 5,
  • 单价 5,
  • 已使用 % 100% 总计 = 25

数量 5,单价 5,已使用百分比 70%,总计 = 32.5(必须增加 30%)

最后我必须计算食品成本。

所以“总成本/售价 * 100”

我被这段代码困住了。请有人进一步帮助我完成我的项目。我一周前才认识java。所以我没有时间好好学习它。每天工作12小时。

这是在 eclipse 的标题中

public class Recipe extends javax.swing.JFrame {
double Qty;
double UnitPrice;
double Used;
double total;
double subTotal;
private static Connection connection = null;
private JPanel contentPane;
Connection conn=null;

JButton btnNewButtonAdd = new JButton("Add");
// btnNewButtonAdd.setIcon(new // ??
btnNewButtonAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
int i=100;
Qty=Double.parseDouble(textFieldQty.getText());
UnitPrice=Double.parseDouble(txtUnitPrice.getText());
Used=Double.parseDouble(textFieldUsed.getText());
//Used=subTotal/100;
subTotal=Qty*UnitPrice;
//total=subTotal*Used;
//Used=subTotal/100;
total=subTotal*(Used/100);//(100+Used);

String TotalRow= String.format("%.2f", total);
txtTotal.setText(TotalRow);

DefaultTableModel model=(DefaultTableModel) table.getModel();
model.addRow(new Object[]{textFieldQty.getText(), BoxCategory.getSelectedItem(),
BoxDescription.getSelectedItem(), textFieldItemID.getText(),
txtUnitPrice.getText(), BoxUnit.getSelectedItem(), textFieldUsed.getText(),
txtTotal.getText()});
}
}

enter image description here

最佳答案

如图here , herehere ,您可以在 getValueAt() 的实现中计算派生值。在下面的示例中,

  • COLUMN_PERCENT是每行工资与表中工资sum()的比率。

  • 百分比列还获取一个 PercentFormatter 实例,以将派生值格式化为百分比。

image

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultRowSorter;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableModel;

/**
* @see https://stackoverflow.com/posts/38192710/edit
* @see https://stackoverflow.com/a/37913520/230513
* @see https://stackoverflow.com/a/37892395/230513
*/
public class JTablePercentTotalExample {

public static void main(String[] args) {
EventQueue.invokeLater(JTablePercentTotalExample::display);
}

public static void display() {
JFrame f = new JFrame("JTable Sorting Example");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
List<Employee> listEmployees = createListEmployees();
TableModel model = new EmployeeTableModel(listEmployees);
JTable table = new JTable(model) {
@Override
public Dimension getPreferredScrollableViewportSize() {
return new Dimension(500, getRowCount() * getRowHeight());
}
};
table.getColumnModel().getColumn(3).setCellRenderer(new CurrencyFormatter());
table.getColumnModel().getColumn(4).setCellRenderer(new PercentFormatter());
table.setAutoCreateRowSorter(true);
DefaultRowSorter sorter = (DefaultRowSorter) table.getRowSorter();
sorter.setSortable(4, false);
f.add(new JScrollPane(table), BorderLayout.CENTER);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}

private static List<Employee> createListEmployees() {
List<Employee> listEmployees = new ArrayList<>();
listEmployees.add(new Employee("Peter", "Manager", 40000));
listEmployees.add(new Employee("Paul", "Programmer", 25000));
listEmployees.add(new Employee("Mary", "Designer", 25000));
listEmployees.add(new Employee("Donald", "Leader", 30000));
listEmployees.add(new Employee("Tom", "Designer", 28000));
listEmployees.add(new Employee("Samantha", "Analyst", 50000));
listEmployees.add(new Employee("Jerome", "Programmer", 32000));
listEmployees.add(new Employee("Jonathon", "Developer", 29000));
listEmployees.add(new Employee("Kevin", "Programmer", 23000));
listEmployees.add(new Employee("Anthony", "Programmer", 23000));
listEmployees.add(new Employee("John", "Designer", 33000));
listEmployees.add(new Employee("David", "Developer", 28000));
listEmployees.add(new Employee("Harry", "Designer", 31000));
listEmployees.add(new Employee("Charles", "Programmer", 26000));
listEmployees.add(new Employee("Joseph", "Manager", 40000));
return listEmployees;
}

private static class EmployeeTableModel extends AbstractTableModel {

private static final int COLUMN_NUM = 0;
private static final int COLUMN_NAME = 1;
private static final int COLUMN_JOB = 2;
private static final int COLUMN_SALARY = 3;
private static final int COLUMN_PERCENT = 4;
private final String[] columnNames = {"No", "Name", "Job", "Salary", "Percent"};
private final List<Employee> listEmployees;

public EmployeeTableModel(List<Employee> listEmployees) {
this.listEmployees = listEmployees;
int indexCount = 1;
for (Employee employee : listEmployees) {
employee.setIndex(indexCount++);
}
}

@Override
public int getColumnCount() {
return columnNames.length;
}

@Override
public int getRowCount() {
return listEmployees.size();
}

@Override
public String getColumnName(int columnIndex) {
return columnNames[columnIndex];
}

@Override
public Class<?> getColumnClass(int columnIndex) {
return getValueAt(0, columnIndex).getClass();
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Employee employee = listEmployees.get(rowIndex);
switch (columnIndex) {
case COLUMN_NUM:
return employee.getIndex();
case COLUMN_NAME:
return employee.getName();
case COLUMN_JOB:
return employee.getJob();
case COLUMN_SALARY:
return employee.getSalary();
case COLUMN_PERCENT:
int salary = (int) getValueAt(rowIndex, COLUMN_SALARY);
return Double.valueOf(salary / (double) sum());
default:
throw new IllegalArgumentException("Invalid column index");
}
}

private int sum() {
int sum = 0;
for (int r = 0; r < getRowCount(); r++) {
sum += (int) getValueAt(r, COLUMN_SALARY);
}
return sum;
}
}

private static class Employee {

private int index;
private String name;
private String job;
private int salary;

public Employee(String name, String job, int salary) {
this.name = name;
this.job = job;
this.salary = salary;
}

public int getIndex() {
return index;
}

public void setIndex(int index) {
this.index = index;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getJob() {
return job;
}

public void setJob(String job) {
this.job = job;
}

public int getSalary() {
return salary;
}

public void setSalary(int age) {
this.salary = age;
}
}

private static class CurrencyFormatter extends DefaultTableCellRenderer {

private final NumberFormat nf = NumberFormat.getCurrencyInstance();

@Override
public Component getTableCellRendererComponent(JTable jTable, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
Component c = super.getTableCellRendererComponent(jTable, value,
isSelected, hasFocus, row, column);
if (c instanceof JLabel && value instanceof Number) {
JLabel label = (JLabel) c;
label.setHorizontalAlignment(JLabel.RIGHT);
Number num = (Number) value;
String text = nf.format(num);
label.setText(text);
}
return c;
}
}

private static class PercentFormatter extends DefaultTableCellRenderer {

private NumberFormat nf = NumberFormat.getPercentInstance();

@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
Component c = super.getTableCellRendererComponent(table, value,
isSelected, hasFocus, row, column);
if (c instanceof JLabel && value instanceof Number) {
JLabel label = (JLabel) c;
label.setHorizontalAlignment(JLabel.RIGHT);
Number num = (Number) value;
String text = nf.format(num);
label.setText(text);
}
return c;
}
}
}

关于java - 在jtable中计算%,总计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38591592/

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