📄 invoicepopup.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.event.*;
import javax.swing.*;
public class InvoicePopup
extends MouseAdapter
implements ActionListener {
private JPopupMenu menu = new JPopupMenu("Table Options");
private JMenuItem add = new JMenuItem("Add Item");
private JMenuItem del = new JMenuItem("Delete Item");
private JMenuItem mod = new JMenuItem("Modify Item");
private Invoicer parent = null;
public InvoicePopup(Invoicer _parent) {
parent = _parent;
menu.add(add);
add.addActionListener(this);
menu.add(mod);
mod.addActionListener(this);
menu.add(new JPopupMenu.Separator());
menu.add(del);
del.addActionListener(this);
}
public void mousePressed(MouseEvent me) {
if (SwingUtilities.isRightMouseButton(me)) {
menu.show(parent.table, me.getX(), me.getY());
}
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == add) {
add();
}
else if (ae.getSource() == del) {
delete();
}
else if (ae.getSource() == mod) {
modify();
}
}
private void delete() {
/*从表格中删除当前选择的行*/
int row = parent.table.getSelectedRow();
if (row >= 0) {
String msg = "Delete '" + parent.data.getValueAt(row, 1) + "'?";
if (JOptionPane.showConfirmDialog(parent, msg, "Confirm Deletion",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.WARNING_MESSAGE)
== JOptionPane.YES_OPTION) {
parent.data.removeRow(row);
parent.price.setText("Total Price: ¥" + parent.total());
}
}
}
private void add() {
/*弹出对话框,以便在表格中增加支票项*/
InvoicePanel invoice = new InvoicePanel();
if (JOptionPane.showOptionDialog(parent, invoice, "New Invoice Item",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE,
null, null, null) ==
JOptionPane.YES_OPTION) {
try {
parent.data.addRow(invoice.getData());
parent.price.setText("Total Price: ¥" + parent.total());
}
catch (NumberFormatException nfe) {
JOptionPane.showMessageDialog(parent, "Invalid numeric format",
"Error Message",
JOptionPane.ERROR_MESSAGE);
}
}
}
private void modify() {
/*弹出包含一个发票项的对话框,进行编辑。*/
InvoicePanel invoice = new InvoicePanel();
int row = parent.table.getSelectedRow();
if (row >= 0) {
String data[] = new String[4];
for (int i = 0; i < data.length; i++) {
data[i] = parent.data.getValueAt(row, i).toString();
}
invoice.setData(data);
if (JOptionPane.showOptionDialog(parent, invoice, "Change Item",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.WARNING_MESSAGE,
null, null, null) ==
JOptionPane.YES_OPTION) {
try {
for (int i = 0; i < invoice.getData().length; i++) {
parent.data.setValueAt(invoice.getData()[i], row, i);
}
parent.price.setText("Total Price: ¥" + parent.total());
}
catch (NumberFormatException nfe) {
JOptionPane.showMessageDialog(parent, "Invalid number",
"Error Message",
JOptionPane.ERROR_MESSAGE);
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -