gpt4 book ai didi

java - 如何对该表进行加法运算?

转载 作者:行者123 更新时间:2023-12-01 18:35:41 26 4
gpt4 key购买 nike

enter image description here

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComponent;
import javax.swing.SwingUtilities;
import net.java.dev.designgridlayout.DesignGridLayout;
import java.io.*;
import net.java.dev.designgridlayout.Tag;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.*;
import java.sql.*;
import javax.swing.table.TableColumn;

class table1 {
JFrame JF;
Container C;
JPanel JP;
JLabel creditLabel;
JComboBox credit;
String[] Credit = { "Vasan Phalke", "Pansare", "Anil Kg", "Suresh",
"Total Credit", "" };
String[] Names = { "Name", "Qty", "Rate/ Kg", "Total Amt." };
JTable table;
DefaultTableModel model;
JScrollPane scrollPane;

public table1() {
JF = new JFrame();
JP = new JPanel();
C = JF.getContentPane();
C.setLayout(new BorderLayout());
JF.pack();
JF.setLocationRelativeTo(null);
JF.setVisible(true);

DesignGridLayout layout = new DesignGridLayout(JP);

creditLabel = new JLabel("Credit");
credit = new JComboBox<String>(Credit);
model = new DefaultTableModel(Names, 6) {
@Override
public Class<?> getColumnClass(int columnIndex) {
switch (columnIndex) {
case 0:
return String.class;
case 1:
return Integer.class;
case 2:
case 3:
return Double.class;
}
return super.getColumnClass(columnIndex);
}

public Object getValueAt(int row, int column) {

if (column == 3) {
Integer i = (Integer) getValueAt(row, 1);
Double d = (Double) getValueAt(row, 2);
if (i != null && d != null) {
return i * d;
} else {
return 0 * 0;
}
}
return super.getValueAt(row, column);
}

public boolean isCellEditable(int arg0, int arg1) {
return true;
}

public void setValueAt(Object aValue, int row, int column) {
super.setValueAt(aValue, row, column);
fireTableCellUpdated(row, 3);
}
};
table = new JTable(model);
{
TableColumn nameColumn = table.getColumnModel().getColumn(0);
nameColumn.setCellEditor(new DefaultCellEditor(credit));
}

scrollPane = new JScrollPane(table);

layout.row().grid(creditLabel);
layout.emptyRow();
layout.row().grid().add(scrollPane);

C.add(JP);

}

public static void main(String args[]) {
new table1();
}
}

我想要总金额。列出现在单元格 (5,4) 上。我尝试了一些代码,但失败了,请在下面添加显示所附图像的代码。我在过去的 5 个小时里一直被困在这张 table 上。提前致谢。

最佳答案

您的示例的简化变体,其中包含此 approach如下所示。请注意几个要点:

  • 虽然JTable不是电子表格,但它可以动态更新单元格以帮助理解。使用TableModelListener更新相关 View 组件。

  • TableModel 应为尽可能少的单元格触发事件; fireTableDataChanged() 用于在这个小型、固定大小的模型上提供便利。

  • 始终pack()封闭的Window并使setVisible()最后

  • Swing GUI 对象应该event dispatch thread 上构建和操作。 .

  • 使用通用编码约定。

代码,经测试:

import java.awt.EventQueue;
import javax.swing.*;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

public class Table1 {

JFrame frame;
JComboBox credit;
String[] rowNames = {
"Vasan Phalke", "Pansare", "Anil Kg", "Suresh", "Total Credit", ""};
String[] colNames = {"Name", "Qty", "Rate/ Kg", "Total Amt."};
JTable table;
DefaultTableModel model;
JScrollPane scrollPane;

public Table1() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
credit = new JComboBox<>(rowNames);
model = new DefaultTableModel(colNames, 6) {
@Override
public Class<?> getColumnClass(int columnIndex) {
switch (columnIndex) {
case 0:
return String.class;
case 1:
return Integer.class;
case 2:
return Double.class;
case 3:
return Double.class;
}
return super.getColumnClass(columnIndex);
}

@Override
public Object getValueAt(int row, int col) {
if (col == 3 & row == 5) {
double sum = 0;
for (int i = 0; i < 5; i++) {
sum += ((Double) getValueAt(i, 3)).doubleValue();
}
return sum;
}
if (col == 3 & row != 5) {
Integer i = (Integer) getValueAt(row, 1);
Double d = (Double) getValueAt(row, 2);
if (i != null && d != null) {
return i * d;
} else {
return 0d;
}
}
return super.getValueAt(row, col);
}

@Override
public void setValueAt(Object aValue, int row, int col) {
super.setValueAt(aValue, row, col);
fireTableDataChanged();
}

@Override
public boolean isCellEditable(int row, int col) {
return col != 3;
}
};
table = new JTable(model);
TableColumn nameColumn = table.getColumnModel().getColumn(0);
nameColumn.setCellEditor(new DefaultCellEditor(credit));
scrollPane = new JScrollPane(table);
frame.add(new JScrollPane(table));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
new Table1();
}
});
}
}

关于java - 如何对该表进行加法运算?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22141248/

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