我正在制作一个Java程序,需要你们的帮助。问题是:
如果按下按钮(Tüm0),我希望程序将这些值加到rist行中,并将其显示在我用?????标记的单元格中。
如果按下按钮(Tüm2),我希望程序将第二列中的值相加,并将其显示在我用????标记的单元格中。
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);
}
}
答案 0 :(得分:3)
没有内置的sum函数,但你可以编写自己的函数。如图所示here覆盖您(匿名)getValueAt()
中的TableModel
方法。在下面的片段中,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);
}
}
答案 1 :(得分:0)
要回答第二个问题,没有JTable没有像excel这样的求和函数。您需要自己实现这些。 要对行中的值求和,您可以使用类似的内容。
public int sumRow(TableModel mdl, int row) {
int total = 0;
// iterate over all columns
for (int i = 0 ; i < mdl.getColumnCount() ; i++) {
// null or not Integer will throw exception
total += (Integer) mdl.getValueAt(row, i);
}
return total;
}
对于列类似,您只需更改行索引并保持列索引不变。此函数假定您的表列的类型为Integer,并且该值不为null。如果不是这样的话,你将不得不在那里进行一些空检查和解析。
要查找具有特定值的单元格,您必须迭代行和列。与此相似
public int[] findValue(TableModel mdl, Object value) {
int[] returnVal = {-1, -1}; // default returnVal if value is not found
// iterate over rows
for(int row = 0 ; row < mdl.getRowCount() ; row++){
// iterate over columns
for(int col = 0 ; col < mdl.getColumnCount() ; col++) {
// if table cell is null it is ignored
if (mdl.getValueAt(row, col) != null && mdl.getValueAt(row, col).equals(value)) {
returnVal[0] = row;
returnVal[1] = col;
return returnVal;
}
}
}
return returnVal;
}