我正在嘗試在 Java Swing 中制作出勤程式,并嘗試將第一個表的每一列中的布林值的數量計算為除 0 列索引之外的出勤天數(因為它包含名稱的字串值) 在按鈕中單擊。我嘗試使用嵌套回圈獲取布林值的計數。現在,當我嘗試使用setValueAt另一個表格的單元格來顯示缺席和存在的天數時,它似乎不起作用。我在這里錯過了什么嗎?
int mainTRow = mainTable.getRowCount();
int mainTCol = mainTable.getColumnCount();
JButton totBTN = new JButton("Total");
totBTN.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DefaultTableModel mdl1 = (DefaultTableModel)showTable.getModel();
int numT=0, numF=0;
for (int i1=0; i1<mainTRow; i1 ) {
for (int y=0; y<mainTCol; y ) {
Boolean obj = GetData(mainTable, i1, y 1);
if (obj == true) {
numT ;
}
else {
numF ;
}
mdl1.setValueAt(numT, i1, 1);
mdl1.setValueAt(numF, i1, 2);
}
}
}
});
另外,這里是使用的方法GetData。
public Boolean GetData(JTable mainTable, int row_index, int col_index){
return (Boolean) mainTable.getModel().getValueAt(row_index, col_index);
}
uj5u.com熱心網友回復:
這基本上是您上一個問題中提供的示例的重新散列,
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new MainPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class MainPane extends JPanel {
private DefaultTableModel leftModel = new DefaultTableModel(new String[] {"Name", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"}, 0) {
@Override
public Class<?> getColumnClass(int columnIndex) {
switch (columnIndex) {
case 0: return String.class;
case 1:
case 2:
case 3:
case 4:
case 5:
case 6: return Boolean.class;
}
return Object.class;
}
};
private DefaultTableModel rightModel = new DefaultTableModel(new String[]{"Name", "Attended", "Absent"}, 0) {
@Override
public Class<?> getColumnClass(int columnIndex) {
switch (columnIndex) {
case 0: return String.class;
case 1:
case 2: return Integer.class;
}
return Object.class;
}
};
public MainPane() {
setLayout(new GridBagLayout());
JTable leftTable = new JTable(leftModel);
JTable rightTable = new JTable(rightModel);
reload();
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
gbc.gridy = 0;
JButton calculateButton = new JButton(">");
calculateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int rowCount = leftModel.getRowCount();
int colCount = leftModel.getColumnCount();
rightModel.setRowCount(rowCount);
for (int row = 0; row < rowCount; row ) {
int attendedCount = 0;
int absentCount = 0;
for (int col = 1; col < colCount; col ) {
Object value = leftModel.getValueAt(row, col);
if (value instanceof Boolean) {
boolean bValue = (boolean) value;
if (bValue) {
attendedCount ;
} else {
absentCount ;
}
}
}
// Technically, you could calculate the absentCount by
// using the column count - 1 and subtracting the attendedCount
// from it
rightModel.setValueAt(leftModel.getValueAt(row, 0), row, 0);
rightModel.setValueAt(attendedCount, row, 1);
rightModel.setValueAt(absentCount, row, 2);
}
}
});
JButton reloadButton = new JButton("Reload");
reloadButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
reload();
}
});
add(new JScrollPane(leftTable), gbc);
gbc.gridx = 2;
add(new JScrollPane(rightTable), gbc);
gbc.gridx = 1;
gbc.weightx = 0;
gbc.fill = GridBagConstraints.VERTICAL;
JPanel actions = new JPanel(new GridBagLayout());
add(actions, gbc);
gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
actions.add(calculateButton, gbc);
actions.add(reloadButton, gbc);
}
protected void reload() {
leftModel.setRowCount(0);
rightModel.setRowCount(0);
Random rnd = new Random();
for (String[] name : NAMES) {
Object[] rowData = new Object[6];
rowData[0] = name[0] " " name[1];
for (int index = 1; index < 6; index ) {
rowData[index] = rnd.nextBoolean();
}
leftModel.addRow(rowData);
}
}
}
public static final String[][] NAMES = new String[][]{
new String[]{"Laura", "Williams"},
new String[]{"Andrea", "Ellis"},
new String[]{"John", "King"},
new String[]{"Kenneth", "Garcia"},
new String[]{"William", "Miller"},
new String[]{"William", "Benitez"},
new String[]{"Michelle", "Hansen"},
new String[]{"Nicholas", "Jones"},
new String[]{"Joshua", "Sanchez"},
new String[]{"Stephen", "Herring"},
new String[]{"Kyle", "Wallace"},
new String[]{"Oscar", "Wong"},
new String[]{"Shane", "Santana"},
new String[]{"Jeffrey", "Weber"},
new String[]{"Laura", "Hale"},
new String[]{"Dr.", "Henry"},
new String[]{"Tammy", "Mathis"},
new String[]{"Jennifer", "Rodriguez"},
new String[]{"Joshua", "Tucker"},
new String[]{"Alejandra", "Wong"},
new String[]{"Barbara", "Flores"},
new String[]{"Kristin", "Sims"},
new String[]{"Stephanie", "Green"},
new String[]{"Travis", "Parks"},
new String[]{"Brian", "Meyers"},
new String[]{"Haley", "Casey"},
new String[]{"Laura", "Wilson"},
new String[]{"Sharon", "Berg"},
new String[]{"Joshua", "Warren"},
new String[]{"William", "Martin"},
new String[]{"David", "Ramos"},
new String[]{"Jessica", "Dennis"},
new String[]{"Joel", "Ferrell"},
new String[]{"Michael", "Johnson"},
new String[]{"Kim", "Watkins"},
new String[]{"Loretta", "Reed"},
new String[]{"Jeffrey", "Williams"},
new String[]{"Jennifer", "Hale"},
new String[]{"Alicia", "Padilla"},
new String[]{"Ian", "Wagner"},
new String[]{"Jasmine", "Wheeler"},
new String[]{"Cynthia", "Aguilar"},
new String[]{"Justin", "Flores"},
new String[]{"Mitchell", "Stephens"},
new String[]{"Kristi", "Rodriguez"},
new String[]{"Renee", "Young"},
new String[]{"Shane", "Simmons"},
new String[]{"Beverly", "Werner"},
new String[]{"Jordan", "Townsend"},
new String[]{"Carrie", "Solomon"},
new String[]{"Jessica", "Martin"},
new String[]{"John", "Pearson"},
new String[]{"Steven", "Miranda"},
new String[]{"Jennifer", "Knight"},
new String[]{"Lindsay", "Martinez"},
new String[]{"Joshua", "Roy"},
new String[]{"Jerry", "Bailey"},
new String[]{"Lauren", "Barr"},
new String[]{"Frank", "Castaneda"},
new String[]{"Gary", "Franklin"},
new String[]{"Robert", "Lewis"},
new String[]{"Peter", "Vasquez"},
new String[]{"Brittany", "Rich"},
new String[]{"Jacob", "White"},
new String[]{"Anna", "Smith"},
new String[]{"Michelle", "Davis"},
new String[]{"Cesar", "Frank"},
new String[]{"Chad", "Walsh"},
new String[]{"Thomas", "Johnson"},
new String[]{"Susan", "Wilkerson"},
new String[]{"Hunter", "Garrett"},
new String[]{"Molly", "Hernandez"},
new String[]{"Gary", "Richmond"},
new String[]{"Megan", "Price"},
new String[]{"Daniel", "Mack"},
new String[]{"Margaret", "Andrade"},
new String[]{"Erika", "White"},
new String[]{"Laura", "Carr"},
new String[]{"Robin", "Schultz"},
new String[]{"Valerie", "King"},
new String[]{"Jacob", "Sherman"},
new String[]{"Monique", "King"},
new String[]{"Laura", "Strickland"},
new String[]{"Jonathan", "Zuniga"},
new String[]{"Danny", "Taylor"},
new String[]{"Darrell", "Reese"},
new String[]{"Juan", "Watkins"},
new String[]{"Valerie", "Cohen"},
new String[]{"David", "Ortiz"},
new String[]{"Catherine", "Hawkins"},
new String[]{"William", "Parker"},
new String[]{"Christine", "Freeman"},
new String[]{"Corey", "Keller"},
new String[]{"James", "Hicks"},
new String[]{"Nicole", "Petty"},
new String[]{"Alexandria", "Aguirre"},
new String[]{"Heather", "Kim"},
new String[]{"Nichole", "Palmer"},
new String[]{"Jonathan", "Moore"},
new String[]{"Cynthia", "Gibbs"}
};
}
正如我在上一個答案中所說,如果兩個表的結構不同,則需要在它們之間進行資料轉換。
如果你仍然不能讓它作業,你需要提供一個Minimal, Reproducible Example,就像我現在做的那樣,兩次。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/491723.html
