📄 invoicepopup.java
字号:
import java.awt.event.*;
import javax.swing.*;
public class InvoicePopup extends MouseAdapter implements ActionListener
{
private JPopupMenu menu = new JPopupMenu("Table option");
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);
add.addActionListener(this);
menu.add(mod);
add.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("$" + 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("$" + 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, 1).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("$" + 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 + -