gpt4 book ai didi

java - 计算 JTable 中的列和行中的值的总和

转载 作者:行者123 更新时间:2023-12-01 22:10:55 24 4
gpt4 key购买 nike

enter image description here

我正在用 Java 编写一个程序,需要你们的帮助。问题是:

  1. 如果我按下按钮(Tüm 0),我希望程序将第一行中的值相加,并将其显示在我用 ?????? 标记的单元格中。

    如果我按下按钮 (Tüm 2),我希望程序将第二列中的值相加,并将其显示在我用 ???? 标记的单元格中。

  2. 如果我在该表中选择窗帘单元格,JTable 是否具有像 Excel 一样对值求和并显示它们的功能?这是代码:

    package mgm;
    public class thirdFrame extends JFrame{

    private JPanel contentPane;
    private JFrame thirdFrame;
    private JTextField tF;
    private JTextField tF1;
    private JTextField tF2;
    private JTable table;
    private JTextField tF3;
    private JTextField tF4;
    private JTextField tF5;

    public thirdFrame() {
    thirdFrame = new JFrame();
    thirdFrame.setFont(new Font("David",Font.BOLD,17));
    thirdFrame.setTitle("TOSCOW");
    thirdFrame.setIconImage(Toolkit.getDefaultToolkit().getImage("C:\\Users\\EXP\\Desktop\\mgm\\src\\MGM-LOGO.png"));
    thirdFrame.setBounds(100, 100, 1600, 620);
    thirdFrame.setVisible(true);
    thirdFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    thirdFrame.getContentPane().setLayout(null);
    JDesktopPane desktop = new JDesktopPane();
    desktop.setOpaque(true);
    thirdFrame.getContentPane().add(desktop);

    JInternalFrame internalFrame = new JInternalFrame ("MGMSportif ",false,true,false,true);
    internalFrame.setFrameIcon(new ImageIcon("C:\\Users\\EXP\\Desktop\\mgm\\src\\reized logo.png"));
    internalFrame.setBounds(209, 126, 720, 400);

    JToolBar toolBar = new JToolBar();
    toolBar.setBounds(0, 0, 1362, 32);
    thirdFrame.getContentPane().add(toolBar);
    JButton bt = new JButton("");
    bt.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    bt.setToolTipText("Dosya a\u00E7mak i\u00E7in t\u0131klay\u0131n");
    bt.setIcon(new ImageIcon("C:\\Users\\EXP\\Desktop\\mgm\\src\\open-file-icon.png"));
    toolBar.add(bt);
    JButton bt1 = new JButton("");
    bt1.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
    Save sF =new Save();
    sF.setVisible(false);
    dispose();
    }
    });
    bt1.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    bt1.setToolTipText("Kay\u0131d etmek i\u00E7in t\u0131klay\u0131n");
    bt1.setIcon(new ImageIcon("C:\\Users\\EXP\\Desktop\\mgm\\src\\save.png"));
    toolBar.add(bt1);
    JButton bt2 = new JButton("");
    bt2.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    bt2.setToolTipText("Kesmek i\u00E7in t\u0131klay\u0131n");
    bt2.setIcon(new ImageIcon
    ("C:\\Users\\EXP\\Desktop\\mgm\\src\\Cut-icon.png"));
    toolBar.add(bt2);
    JButton bt3 = new JButton("");
    bt3.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    bt3.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    internalFrame.setVisible(true);
    }
    });

    thirdFrame.getContentPane().add(internalFrame);
    internalFrame.getContentPane().setLayout(null);
    JLabel lb = new JLabel("Tarihe g\u00F6re ara");
    lb.setFont(new Font("Tahoma", Font.PLAIN, 13));
    lb.setBounds(10, 11, 110, 26);
    internalFrame.getContentPane().add(lb);
    tF = new JTextField();
    tF.setToolTipText("Aramak isteyen tarihi giriniz");
    tF.setBounds(144, 16, 445, 20);
    internalFrame.getContentPane().add(tF);
    tF.setColumns(10);
    JProgressBar pBar = new JProgressBar(0);
    pBar.setValue(0);
    pBar.setString("Aranıyor....");
    pBar.setStringPainted(true);
    pBar.setBounds(10, 67, 684, 21);
    internalFrame.getContentPane().add( pBar);
    JButton sesarchButton = new JButton("");
    sesarchButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    sesarchButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
    sesarchButton.setEnabled(false);
    setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    }
    });
    sesarchButton.setIcon(new ImageIcon("C:\\Users\\EXP\\Desktop\\mgm\\src\\search48.png"));
    sesarchButton.setBounds(599, 7, 57, 56);
    internalFrame.getContentPane().add(sesarchButton);
    JLabel lb1 = new JLabel("Makineye g\u00F6re ara");
    lb1.setFont(new Font("Tahoma", Font.PLAIN, 13));
    lb1.setBounds(10, 43, 124, 20);
    internalFrame.getContentPane().add( lb1);
    tF1 = new JTextField();
    tF1 .setBounds(144, 42, 445, 20);
    internalFrame.getContentPane().add(tF1 );
    tF1 .setColumns(10);
    tF1 .setBounds(144, 42, 445, 20);
    JList list = new JList();
    list.setBounds(10, 99, 684, 360);
    internalFrame.getContentPane().add(list); bt3.setToolTipText("Dosya aramak i\u00E7in t\u0131klay\u0131n");
    bt3.setIcon(new ImageIcon("C:\\Users\\EXP\\Desktop\\mgm\\src\\Zoom-icon.png"));
    toolBar.add( bt3);
    JButton bt4 = new JButton("");
    bt4.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    bt4.setToolTipText("\u00C7\u0131kt\u0131 almak i\u00E7in t\u0131klay\u0131n");
    bt4.setIcon(new ImageIcon("C:\\Users\\EXP\\Desktop\\mgm\\src\\print-icon16.png"));
    toolBar.add(bt4);
    JButton bt5 = new JButton("");
    bt5.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    bt5.setToolTipText("Pencereyi kapatmak i\u00E7in t\u0131klay\u0131n");
    bt5.setIcon(new ImageIcon("C:\\Users\\EXP\\Desktop\\mgm\\src\\Close.png"));
    toolBar.add(bt5);
    JCheckBox checkBox = new JCheckBox("Enjeksyon Makine No:");
    checkBox.addItemListener(new ItemListener() {
    @Override
    public void itemStateChanged(ItemEvent e) {
    if(e.getStateChange() == ItemEvent.SELECTED){
    tF2.setEnabled(true);
    }
    else if(e.getStateChange() == ItemEvent.DESELECTED){
    tF2.setEnabled(false);
    }
    }
    });
    checkBox.setFont(new Font("Tahoma", Font.PLAIN, 11));
    checkBox.setBounds(25, 39, 131, 23);
    thirdFrame.getContentPane().add(checkBox);

    tF2 = new JTextField();
    tF2.setBounds(162, 40, 31, 19);
    thirdFrame.getContentPane().add( tF2);
    tF2.setColumns(10);

    JCheckBox checkBox1 = new JCheckBox("Kaynak Makine No:");
    checkBox1.addItemListener(new ItemListener() {
    @Override
    public void itemStateChanged(ItemEvent e) {
    if(e.getStateChange() == ItemEvent.SELECTED){
    tF3.setEnabled(true);
    tF3.setText("");
    }
    else if(e.getStateChange() == ItemEvent.DESELECTED){
    tF3.setEnabled(false);
    tF3.setText("");
    }


    }
    });
    checkBox1.setFont(new Font("Tahoma", Font.PLAIN, 11));
    checkBox1.setBounds(337, 39, 117, 23);
    thirdFrame.getContentPane().add(checkBox1);

    tF3 = new JTextField();

    tF3.setBounds(460, 43, 31, 19);
    thirdFrame.getContentPane().add(tF3);
    tF3.setColumns(10);

    JCheckBox checkBox2 = new JCheckBox("Otomatsyon Makine No:");
    checkBox2.addItemListener(new ItemListener() {
    @Override
    public void itemStateChanged(ItemEvent e) {
    if(e.getStateChange() == ItemEvent.SELECTED){
    tF4.setEnabled(true);
    tF4.setText("");
    }
    else if(e.getStateChange() == ItemEvent.DESELECTED){
    tF4.setEnabled(false);
    tF4.setText("");
    }


    }
    });
    checkBox2.setFont(new Font("Tahoma", Font.PLAIN, 11));
    checkBox2.setBounds(651, 39, 141, 23);
    thirdFrame.getContentPane().add(checkBox2);

    tF4 = new JTextField();
    tF4.setBounds(795, 43, 31, 19);
    thirdFrame.getContentPane().add(tF4);
    tF4.setColumns(10);

    JCheckBox checkBox3 = new JCheckBox("Pakitleme:");
    checkBox3.setFont(new Font("Tahoma", Font.PLAIN, 11));
    checkBox3.addItemListener(new ItemListener() {

    public void itemStateChanged(ItemEvent e) {
    if(e.getStateChange() == ItemEvent.SELECTED){
    tF5.setEnabled(true);
    tF5.setText("");
    }
    else if(e.getStateChange() == ItemEvent.DESELECTED){
    tF5.setEnabled(false);
    tF5.setText("");
    }
    }
    });



    checkBox3.setBounds(923, 39, 85, 23);
    thirdFrame.getContentPane().add(checkBox3);

    tF5 = new JTextField();
    tF5.setBounds(1010, 43, 31, 19);
    thirdFrame.getContentPane().add(tF5);
    tF5.setColumns(10);

    JLabel lb2 = new JLabel(" Vardiya 1 Vardiya 2");
    lb2.setForeground(SystemColor.desktop);
    lb2.setFont(new Font("Tahoma", Font.PLAIN, 13));
    lb2.setBounds(0, 73, 1341,21);
    thirdFrame.getContentPane().add(lb2);

    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setBounds(20, 94, 1324, 455);
    thirdFrame.getContentPane().add(scrollPane);

    table = new JTable();
    JTable table = new JTable();
    table.setColumnSelectionAllowed(true);
    table.setAutoCreateRowSorter(true);


    table.setForeground(Color.BLACK);
    table.setModel(new DefaultTableModel(
    new Object[][] {
    {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
    {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
    {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
    {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
    {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
    {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
    {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
    {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
    {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
    {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
    {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
    {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
    {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
    {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
    {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
    {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
    {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
    {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
    {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
    {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
    {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
    {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
    {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
    {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
    {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
    {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
    {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
    {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
    {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
    {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
    {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
    {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
    },
    new String[] {
    "TAR\u0130H", "BEYAZ", "KIRMIZI", "MAV\u0130", "SARI", "S\u0130YAH", "ADET", "PERS.", "\u00DCR.NEDEN\u0130", "Toplam", "TAR\u0130H", "BEYAZ", "KIRMIZI", "MAV\u0130", "SARI", "S\u0130YAH", "ADET", "PERS.", "\u00DCR.NEDEN\u0130", "Toplam", "2VDT"
    }
    ) {
    Class[] columnTypes = new Class[] {
    Integer.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class
    };
    public Class getColumnClass(int columnIndex) {
    return columnTypes[columnIndex];
    }
    });
    scrollPane.setViewportView(table);


    JSeparator separator = new JSeparator();
    separator.setBounds(0, 69, 1362, 2);
    thirdFrame.getContentPane().add(separator);

    JButton bt6 = new JButton("T\u00FCm 0");
    bt6.setFont(new Font("Tahoma", Font.PLAIN, 8));
    bt6.setBounds(579, 69, 61, 23);
    thirdFrame.getContentPane().add(bt6);

    JButton bt7 = new JButton("T\u00FCm 1");
    bt7.setFont(new Font("Tahoma", Font.PLAIN, 8));
    bt7.addActionListener(new ActionListener() {


    public void actionPerformed(ActionEvent e) {


    }
    });

    bt7.setBounds(1201, 69, 61, 23);
    thirdFrame.getContentPane().add(bt7);

    JButton bt8 = new JButton("T\u00FCm 2");
    bt8.setFont(new Font("Tahoma", Font.PLAIN, 8));
    bt8.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent e) {


    }
    });

    bt8.setBounds(82, 551, 61, 23);
    thirdFrame.getContentPane().add(bt8);

    JButton bt9 = new JButton("T\u00FCm 3");
    bt9.setFont(new Font("Tahoma", Font.PLAIN, 8));
    bt9.setBounds(145, 551, 61, 23);
    thirdFrame.getContentPane().add(bt9);

    JButton bt10 = new JButton("T\u00FCm 4");
    bt10.setFont(new Font("Tahoma", Font.PLAIN, 8));
    bt10.setBounds(207, 551, 61, 23);
    thirdFrame.getContentPane().add(bt10);

    JButton bt11 = new JButton("T\u00FCm 5");
    bt11.setFont(new Font("Tahoma", Font.PLAIN, 8));
    bt11.setBounds(270, 551, 61, 23);
    thirdFrame.getContentPane().add(bt11);

    JButton bt12 = new JButton("T\u00FCm 6");
    bt12.setFont(new Font("Tahoma", Font.PLAIN, 8));
    bt12.setBounds(332, 551, 61, 23);
    thirdFrame.getContentPane().add( bt12);

    JButton bt13 = new JButton("T\u00FCm 7");
    bt13.setFont(new Font("Tahoma", Font.PLAIN, 8));
    bt13.setBounds(395, 551, 61, 23);
    thirdFrame.getContentPane().add(bt13);

    JButton b14 = new JButton("T\u00FCm 8");
    b14.setFont(new Font("Tahoma", Font.PLAIN, 8));
    b14.setBounds(703, 551, 61, 23);
    thirdFrame.getContentPane().add(b14);

    JButton bt15 = new JButton("T\u00FCm 9");
    bt15.setFont(new Font("Tahoma", Font.PLAIN, 8));
    bt15.setBounds(764, 551, 61, 23);
    thirdFrame.getContentPane().add(bt15);

    JButton bt16 = new JButton("T\u00FCm 10");
    bt16.setFont(new Font("Tahoma", Font.PLAIN, 8));
    bt16.setBounds(826, 551, 61, 23);
    thirdFrame.getContentPane().add(bt16);

    JButton bt17 = new JButton("T\u00FCm 11");
    bt17.setFont(new Font("Tahoma", Font.PLAIN, 8));
    bt17.setBounds(889, 551, 61, 23);
    thirdFrame.getContentPane().add(bt17);

    JButton bt18 = new JButton("T\u00FCm 12");
    bt18.setFont(new Font("Tahoma", Font.PLAIN, 8));
    bt18.setBounds(952, 551, 61, 23);
    thirdFrame.getContentPane().add( bt18);

    JButton bt19 = new JButton("T\u00FCm 13");
    bt19.setFont(new Font("Tahoma", Font.PLAIN, 8));
    bt19.setBounds(1015, 551, 61, 23);
    thirdFrame.getContentPane().add( bt19);



    }
    }

最佳答案

没有内置的求和函数,但您可以编写自己的求和函数。重写(匿名)TableModel 中的 getValueAt() 方法,如下所示 here 。在下面的片段中,sum(col) 应调用 super.getValueAt() 来添加在特定 col 中找到的值。我不确定是否需要一排按钮。

@Override
public Object getValueAt(int row, int col) {
if (row == DEPENDENT_ROW) {
return sum(col);
} else {
return super.getValueAt(row, col);
}
}

关于java - 计算 JTable 中的列和行中的值的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31853287/

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