2013-10-15 59 views
2

我在一個項目中工作我有一個主類(mdiform),它管理我所有的類。我使用JInternalFrame創建了一個類PurchaseMaster,我想通過一些選項卡顯示它。JTabbedPane和java中的JInternalFrame

我已經做了這樣的代碼:

import javax.swing.*; 
import javax.swing.event.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.io.*; 

public class PurchaseMaster extends JInternalFrame implements ActionListener 
{ 
    JTabbedPane tabbedPane; 
    JPanel panel1;//,panel2; 
    JOptionPane op; 
    mdiform m; 
    PurchaseMaster p; 
    JLabel lblrtmt,lblsale,lblform,lblto,lblmaterial,lblload,lblpaid,lbldeposit,lblbalance,lbldlname,lblpur,lblinvoice,lbldate,lblparty,lbltrucktype,lbltruckno,lblitem,lblqty,lblrate; 
     JButton btnreset,btnsave,btncancel,btnprint; 
     JTextField txtfrom,txtto,txtload,txtdeposit,txtpaid,txtbal,txtdl,txtcalu,txtinvoice,txtslip,txtparty,txttruckno,txtrate; 
     JComboBox cmaterial,ctrucktype,citem,cqty; 
     String type[]={"SLC","Self"}; 
     String qty[]={"Trip","Weight"}; 
     String tno[]={"Coal","Steel","Wood","Rakhad","Iron"}; 
     String[] material={"1","2","3"}; 

    public PurchaseMaster(mdiform m) 
    { 
     super("Puchase",false,true,false,true); 
     setBounds(m.getWidth()/2-300,m.getHeight()/2-200,600,400); 
     //setBounds(400,600); 
     setLayout(null); 

     this.m=m; 

     JPanel topPanel = new JPanel(); 
     topPanel.setLayout(new BorderLayout()); 


     // Create the tab pages 
     createPage1(); 
     //createPage2(); 

     // Create a tabbed pane 
     tabbedPane = new JTabbedPane(); 
     tabbedPane.addTab("ADD", panel1); 
     //tabbedPane.addTab("View", panel2); 
     topPanel.add(tabbedPane, BorderLayout.CENTER); 
     add(topPanel); 
     this.pack(); 
     op=new JOptionPane(); 

      setVisible(true); 
     setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE); 

     try{ 
      m.ConnectDB(); 
     m.msgbox("Work"); 
     m.rs=m.st.executeQuery("Select max(invno)+1 from bricks"); 
     if(m.rs.next()) 
     { 
      txtinvoice.setText(""+m.rs.getInt(1)); 
     } 
      m.msgbox("First User ......"); 
      txtinvoice.setText("101"); 

     } 
     catch(Exception e){}  
    } 



     public void createPage1() 
     { 
     panel1 = new JPanel(); 
     panel1.setLayout(null); 

     lblpur=new JLabel("Purchase"); 
     lblpur.setFont(new Font("Times new roman",Font.BOLD,24)); 
     lblpur.setForeground(new Color(100,149,23)); 
     lblpur.setBounds(250,20,200,50); 
     panel1.add(lblpur); 

     lblinvoice =new JLabel("Invoice No. : "); 
     lbldate =new JLabel("Date : "); 
     lblparty =new JLabel("Party Name : "); 
     lbltruckno =new JLabel("Truck No. : "); 
     lbltrucktype =new JLabel("Truck type : "); 
     lblitem =new JLabel("Invoice No. : "); 
     lblqty =new JLabel("Qty in : "); 
     lblrate =new JLabel("Rate : "); 
     txtcalu=new JTextField(); 
     txtinvoice=new JTextField(); 
     txtparty=new JTextField(); 
     txtrate=new JTextField(); 
     txttruckno=new JTextField(); 
     citem=new JComboBox(type); 
     cqty=new JComboBox(qty); 
     ctrucktype=new JComboBox(tno); 
     btncancel=new JButton("Cancel"); 
     btnprint=new JButton("Print"); 
     btnreset=new JButton("Reset"); 
     btnsave=new JButton("Save"); 

     lblinvoice.setBounds(50,100,80,25); 
     txtinvoice.setBounds(140,100,50,25); 
     lbldate.setBounds(440,100,80,25); 
     lblparty.setBounds(50,135,80,25); 
     txtparty.setBounds(140,135,400,25); 
     lbltrucktype.setBounds(50,170,80,25); 
     ctrucktype.setBounds(140,170,100,25); 
     lbltruckno.setBounds(350,170,80,25); 
     txttruckno.setBounds(450,170,100,25); 
     lblitem.setBounds(50,205,80,25); 
     citem.setBounds(140,205,100,25); 
     lblqty.setBounds(260,240,80,25); 
     cqty.setBounds(350,240,80,25); 
     txtcalu.setBounds(440,240,80,25); 
     lblrate.setBounds(260,275,80,25); 
     txtrate.setBounds(350,275,80,25); 
     btnreset.setBounds(50,310,100,25); 
     btnsave.setBounds(160,310,100,25); 
     btnprint.setBounds(270,310,100,25); 
     btncancel.setBounds(380,310,100,25); 


     panel1.add(lblinvoice);  panel1.add(lbldate); 
     panel1.add(lblparty);panel1.add(lbltruckno);panel1.add(lbltrucktype);panel1.add(lblitem); panel1.add(lblqty);panel1.add(lblrate); panel1.add(btncancel);panel1.add(btnprint); panel1.add(btnreset); panel1.add(citem); panel1.add(cqty); panel1.add(ctrucktype);panel1.add(btnsave); panel1.add(txtcalu);panel1.add(txtinvoice);panel1.add(txtparty);panel1.add(txtrate); panel1.add(txtslip); panel1.add(txttruckno); 
     panel1.add(lblpur); 

     } 


    public void actionPerformed(ActionEvent ae) 
    { 

    } 

} 

其中m是主類(的MDIForm)的對象。

編譯它,但是當我點擊子菜單購買,除了在交易菜單,然後它給我的錯誤。

enter image description here

我的堆棧跟蹤是:

H:\Workspace\Mining Inventory\Project\src>java mdiform 
java.lang.NullPointerException 
     at java.awt.Container.addImpl(Container.java:1090) 
     at java.awt.Container.add(Container.java:410) 
     at PurchaseMaster.createPage1(PurchaseMaster.java:127) 
     at PurchaseMaster.<init>(PurchaseMaster.java:40) 
     at mdiform.actionPerformed(mdiform.java:134) 
     at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:20 
18) 
     at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.jav 
a:2341) 
     at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel 
.java:402) 
     at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259 
) 
     at javax.swing.AbstractButton.doClick(AbstractButton.java:376) 
     at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:8 
33) 
     at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMen 
uItemUI.java:877) 
     at java.awt.Component.processMouseEvent(Component.java:6504) 
     at javax.swing.JComponent.processMouseEvent(JComponent.java:3321) 
     at java.awt.Component.processEvent(Component.java:6269) 
     at java.awt.Container.processEvent(Container.java:2229) 
     at java.awt.Component.dispatchEventImpl(Component.java:4860) 
     at java.awt.Container.dispatchEventImpl(Container.java:2287) 
     at java.awt.Component.dispatchEvent(Component.java:4686) 
     at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832 
) 
     at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492) 

     at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422) 
     at java.awt.Container.dispatchEventImpl(Container.java:2273) 
     at java.awt.Window.dispatchEventImpl(Window.java:2713) 
     at java.awt.Component.dispatchEvent(Component.java:4686) 
     at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707) 
     at java.awt.EventQueue.access$000(EventQueue.java:101) 
     at java.awt.EventQueue$3.run(EventQueue.java:666) 
     at java.awt.EventQueue$3.run(EventQueue.java:664) 
     at java.security.AccessController.doPrivileged(Native Method) 
     at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDo 
main.java:76) 
     at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDo 
main.java:87) 
     at java.awt.EventQueue$4.run(EventQueue.java:680) 
     at java.awt.EventQueue$4.run(EventQueue.java:678) 
     at java.security.AccessController.doPrivileged(Native Method) 
     at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDo 
main.java:76) 
     at java.awt.EventQueue.dispatchEvent(EventQueue.java:677) 
     at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre 
ad.java:211) 
     at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread. 
java:128) 
     at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre 
ad.java:117) 
     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113) 

     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105) 

     at java.awt.EventDispatchThread.run(EventDispatchThread.java:90) 

其工作,但它沒有最大化。 enter image description here

+0

發佈完整堆棧跟蹤。 – UDPLover

+0

某處您正嘗試訪問非實例化變量/ null變量。在您的日誌/控制檯中,您應該看到代碼中的哪一行會引發異常。另外不要大寫你的類名是一個不好的做法。 –

+0

我沒有得到堆棧跟蹤。如果你需要檢查它,我可以給你發送文件。 –

回答

3

每條物理線路上的一行代碼對幫助解決這類問題將有很大的幫助。

如此說來,你的錯誤是在啓動非常長的線:

panel1.add(lblparty);panel1.add(... 

要添加到面板這是從來沒有初始化的組件。在這種情況下,這是您的txtslip組件。可能有其他人。

+0

謝謝,我解決了它。它打開但沒有顯示框架中的任何選項卡。 –

+0

你的代碼有很多錯誤。最重要的是,你的佈局設置爲「null」,這幾乎總是一個壞主意。在這種情況下,您將tabbedPane添加到'topPanel'似乎沒問題,但是隨後'topPanel'被添加到一個空佈局容器中,而沒有爲此面板指定邊界,這很可能是您沒有看到它的原因。 – splungebob

+1

現在感謝它正常工作。 –