以下是我的代码。
它显示一个包含所有每日销售的表格,3个按钮(下一个,上一个,打印)。在按下下一个按钮日期的时候,第二天销售将在另一个窗口显示。现在这里是主要问题 - 我希望当新窗口打开时应该关闭第一个窗口。有人可以帮我解决这个问题吗?
package shop;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.print.PrinterException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
import shop.DBEngine;
public class AllCashTable extends JFrame implements ActionListener{
private static String name;
private Vector<Vector<String>> data;
private Vector<Vector<String>> data1;
private Vector<String> header;
private Vector<String> header1;
JButton next,previous,print;
int a=0,d=0,y=0,m=0;
JTable table;
private JPanel mainPanel = new JPanel();
private String date,month,year;
String gt;
private String newdate;
private static JFrame frame,frame1,frame2;
public AllCashTable(String name) throws Exception{
DBEngine dbengine = new DBEngine();
data = dbengine.getDailySellAdd(name);
header = new Vector<String>();
header.add("Date");
header.add("Cash");
next=new JButton("Next");
previous=new JButton("Previous");
print=new JButton("Print");
table = new JTable(data, header);
table.setRowHeight(40);
table.setSelectionBackground(Color.YELLOW);
JPanel viewPanel = new JPanel();
viewPanel.setLayout(new BorderLayout());
viewPanel.add(table,BorderLayout.CENTER);
JScrollPane scroll = new JScrollPane(viewPanel);
scroll.setColumnHeaderView(table.getTableHeader());
mainPanel.setLayout(new BorderLayout());
mainPanel.setBorder(BorderFactory.createEtchedBorder());
mainPanel.add(scroll,BorderLayout.CENTER);
for(int i=0;i<table.getRowCount();i++){
a=a+Integer.parseInt(data.get(i).get(1));
}
gt=Integer.toString(a);
header1 = new Vector<String>();
header1.add("Total");
header1.add(gt);
data.setSize(table.getRowCount()+1);
data.set(table.getRowCount()-1, header1);
JLabel jlab=new JLabel("Total:");
String gt=Integer.toString(a);
JLabel jlab3=new JLabel(gt);
JLabel jlab4=new JLabel(" ");
JPanel viewPanel2=new JPanel();
viewPanel2.add(jlab);
viewPanel2.add(jlab3);
viewPanel2.add(jlab4);
viewPanel2.add(previous);
viewPanel2.add(next);
viewPanel2.add(print);
previous.addActionListener(this);
print.addActionListener(this);
next.addActionListener(this);
mainPanel.add(viewPanel2,BorderLayout.PAGE_END);
}
public JPanel getMainPanel(){
return mainPanel;
}
private static void createAndShowUI() throws Exception{
frame = new JFrame("Cash Memo");
frame.getContentPane().add(new AllCashTable(name).getMainPanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(330,400);
//frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
//frame.setVisible(false);
}
public static void main(String[] args){
java.awt.EventQueue.invokeLater(new Runnable(){
public void run(){
try {
createAndShowUI();
} catch (Exception ex) {
Logger.getLogger(AllCashTable.class.getName()).log(Level.SEVERE,null,ex);
}
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==next){
date=data.get(0).get(0);
String m[]={"01","02","03","04","05","06","07","08","09","10","11","12"};
Calendar cal=Calendar.getInstance();
date=date.substring(0, 2);
d=Integer.parseInt(date)+1;
newdate=(d+"/"+m[cal.get(Calendar.MONTH)]+"/"+cal.get(Calendar.YEAR));
frame1 = new JFrame("Cash Memo");
try {
frame1.getContentPane().add(new AllCashTable(newdate).getMainPanel());
} catch (Exception ex) {
Logger.getLogger(AllCashTable.class.getName()).log(Level.SEVERE, null, ex);
}
frame1.setSize(330,400);
//frame.pack();
frame1.setLocationRelativeTo(null);
frame1.setVisible(true);
frame.dispose();//here is error nullpointerexception
}if(e.getSource()==previous){
date=data.get(0).get(0);
String m[]={"01","02","03","04","05","06","07","08","09","10","11","12"};
Calendar cal=Calendar.getInstance();
date=date.substring(0, 2);
int ax=Integer.parseInt(date)-1;
System.out.println(ax);
newdate=(ax+"/"+m[cal.get(Calendar.MONTH)]+"/"+cal.get(Calendar.YEAR));
frame2 = new JFrame("Cash Memo");
try {
frame2.getContentPane().add(new AllCashTable(newdate).getMainPanel());
} catch (Exception ex) {
Logger.getLogger(AllCashTable.class.getName()).log(Level.SEVERE, null, ex);
}
//frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.setSize(330,400);
//frame.pack();
frame2.setLocationRelativeTo(null);
frame2.setVisible(true);
//frame1.setVisible(false);
}if(e.getSource()==print){
try {
boolean complete = table.print();
if (complete) {
/* show a success message */
System.out.println("Printing Complete");
} else {
/*show a message indicating that printing was cancelled */
System.out.println("Not Printed");
}
} catch (PrinterException pe) {
/* Printing failed, report to the user */
System.out.println(pe);
}
}
}
}
答案 0 :(得分:2)
这样做:
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
答案 1 :(得分:1)
关闭JFrame
使用actionPerformed()
方法中的下一个代码:
frame.setVisible(false);
frame.dispose();
编辑: 你接下来的错误你也可以在新变量中创建一个新的框架,使用它有用的下一个代码,同时观看@MadProgrammer关于多个框架的建议。
if (e.getSource() == next) {
date = data.get(0).get(0);
String m[] = { "01", "02", "03", "04", "05", "06", "07", "08",
"09", "10", "11", "12" };
Calendar cal = Calendar.getInstance();
date = date.substring(0, 2);
d=Integer.parseInt(date)+1;
newdate = (d + "/" + m[cal.get(Calendar.MONTH)] + "/" + cal
.get(Calendar.YEAR));
frame.dispose();// here is error nullpointerexception
frame.setVisible(false);
frame = new JFrame("Cash Memo");
try {
frame.getContentPane().add(new Form(newdate).getMainPanel());
} catch (Exception ex) {
Logger.getLogger(Form.class.getName()).log(Level.SEVERE, null,
ex);
}
frame.setSize(330, 400);
// frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}