0

我在桌面窗格中有一个 JInternalframe (tab_items)。每当用户单击 tab_items 中的弹出菜单时,我想在同一桌面窗格中打开另一个 JInternalframe (frm_add_items)。我怎样才能做到这一点?

//class tab_items 

public class tab_items extends JInternalFrame implements MouseListener,DocumentListener,ActionListener{

    DS co= new DS ();
    JPanel panel1;
    JScrollPane pane;
    DefaultTableModel model= new DefaultTableModel();
    JTable tbl= new JTable(model);
    JLabel lbl_search;
    JTextField txt_search;
    JPopupMenu pop;
    JMenuItem mi_add,mi_edit, mi_del;  

    public tab_items(){


        panel1= new JPanel(); 
        panel1.setLayout(null);      

        //popupmenu
        pop=new JPopupMenu();
        mi_add=new JMenuItem("Add new record");
        mi_edit=new JMenuItem("Edit record");
        mi_del=new JMenuItem("Delete record");
        add(pop);

        pop.add(mi_add);        
        mi_add.addActionListener(this);
        mi_add.addMouseListener(this);
        pop.add(mi_edit);
        mi_edit.addActionListener(this);

        pop.add(mi_del);
        mi_del.addActionListener(this);

      //     try{
       //         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
       //     }catch(Exception ex){}

           initcomponents();        
           setVisible(true);
           setSize(700,500);
           setLocation(100,150);
           setTitle("List Of Items");           
           setDefaultCloseOperation(EXIT_ON_CLOSE);


    }

     public void initcomponents(){

     try{        
         model.addColumn("ID");
         model.addColumn("Item Name");
         model.addColumn("Cost Price");
         model.addColumn("Selling Price");
         model.addColumn("Qty");

         tbl.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
         PreparedStatement pstat=co.con.prepareStatement("Select * from tbl_items ORDER BY itemid ASC");
         ResultSet rs=pstat.executeQuery();       
            while(rs.next()){
                model.addRow(new Object[]{rs.getInt(1), rs.getString(2).trim(), rs.getString(3).trim(), rs.getString(4).trim(),rs.getInt(5)});
            }                
        }catch(Exception ex){}

     // SETTING COLUMN WIDTH
           TableColumnModel model=tbl.getColumnModel();           
            model.getColumn(0).setPreferredWidth(50);
            model.getColumn(1).setPreferredWidth(400);
            model.getColumn(2).setPreferredWidth(100);
            model.getColumn(3).setPreferredWidth(100);
            model.getColumn(4).setPreferredWidth(50);

        // adding panel

         add(panel1); 
         panel1.setBounds(0,0,800,600);   

         //scrollpane initialize       
        int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
        int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;
        pane=new JScrollPane(tbl, v, h); //table included in scrollpane
        panel1.add(pane);
        pane.setBounds(0,50,700,400);
        tbl.addMouseListener(this);

        lbl_search= new JLabel();
        lbl_search.setIcon(new ImageIcon(getClass().getResource("/img/btn_search_up.png")));
        panel1.add(lbl_search);
        lbl_search.setBounds(5,10,25,20);

        txt_search=new JTextField();
        panel1.add(txt_search);
        txt_search.setBounds(35,10,200,20);
        txt_search.getDocument().addDocumentListener(this);
        /* 
        btn_add= new JButton();
        btn_add.setIcon(new ImageIcon(getClass().getResource("/img/add-button-md.png")));
       //   btn_add.setHorizontalAlignment(SwingConstants.CENTER);
       //  btn_add.setText("Add");

        add(btn_add);
        btn_add.setBounds(665, 125, 65, 65);

        btn_edit= new JButton();        
        btn_edit.setText("Update");
        add(btn_edit);
        btn_edit.setBounds(665, 200, 65, 65);

        btn_delete=new JButton();
        btn_delete.setText("Delete");
        add(btn_delete);
        btn_delete.setBounds(665, 275, 65, 65);
      */          

    }  

    public void mouseClicked(MouseEvent e) {}
    public void mousePressed(MouseEvent me){maybeShowPopup(me);}
    public void mouseReleased(MouseEvent me){maybeShowPopup(me);}
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
    private void maybeShowPopup(MouseEvent e){
        if (e.isPopupTrigger()){
            pop.show(e.getComponent(),e.getX(), e.getY());               
        }
}

   public void update(DocumentEvent de){

                       Document doc=(Document)de.getDocument();
            int length=doc.getLength();
            String str=null;
            try
            {
                str=doc.getText(0,length);
            }
            catch(Exception ex)
            {
                            JOptionPane.showMessageDialog(null, "Error in retreiving Search Length\n"+ex);
            }

                        try
                {
                                        Class.forName("com.mysql.jdbc.Driver");
                                        Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/dstore","root","");
                    PreparedStatement pstat=con.prepareStatement("select * from tbl_items where itemid LIKe '"+str+"%' OR itemname LIKe '"+str+"%' ORDER BY itemid ASC");
                                        ResultSet rs=pstat.executeQuery();   
                                        model.setRowCount(0);
                                        while(rs.next()){
                                            model.addRow(new Object[]{rs.getInt(1), rs.getString(2).trim(), rs.getString(3).trim(), rs.getString(4).trim(),rs.getInt(5)});
                                        }

                }
                catch(Exception ex)
                {
                    JOptionPane.showMessageDialog(null,"ERROR IN DOCUMENT LISTENER.\nCannot Fetch Records"+ex);
                }

    }
    public void insertUpdate(DocumentEvent de) {
    update(de);
    }
    public void removeUpdate(DocumentEvent de) {
    update(de);
    }
    public void changedUpdate(DocumentEvent de) {
    update(de);
    } 

    public void actionPerformed(ActionEvent ae) {


       if(ae.getSource()==mi_add){

    //   frm_add_item ob= new frm_add_item();
    //   ob.show();
      // dp.add(ob);

       }else if(ae.getSource()==mi_edit) {


       }else {


       }

    } 

}

#//class frm_main ( it contains desktop pane)#
4

2 回答 2

2

您的片段提出的几个问题需要仔细审查:

  • 用于Action封装功能。

  • 不要JInternalFrame无谓地扩展;使用工厂方法。

  • 不要使用setBounds(); 使用布局管理器

图片

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.beans.PropertyVetoException;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;

//** @see http://stackoverflow.com/a/18556224/230513 */
public class Test {

    private JDesktopPane jdp = new JDesktopPane() {
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(600, 400);
        }
    };
    private NewAction newAction = new NewAction("New");

    public Test() {
        createAndShowGUI();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Test();
            }
        });
    }

    private void createAndShowGUI() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        newAction.actionPerformed(null);
        frame.add(jdp);
        frame.pack();
        frame.setVisible(true);
    }

    private void createInternalFrame(int x, int y) {
        final JInternalFrame jif =
            new JInternalFrame("Test" + x, true, true, true, true);
        jif.setLocation(x, y);
        JPanel jp = new JPanel() {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(256, 128);
            }
        };
        JPopupMenu popup = new JPopupMenu();
        jp.setComponentPopupMenu(popup);
        popup.add(new JMenuItem(newAction));
        jp.add(new JButton(newAction));
        jif.add(jp);
        jif.pack();
        jdp.add(jif);
        jif.setVisible(true);
        try {
            jif.setSelected(true);
        } catch (PropertyVetoException e) {
            e.printStackTrace(System.err);
        }
    }

    private class NewAction extends AbstractAction {

        private int i;

        public NewAction(String name) {
            super(name);
        }

        @Override
        public void actionPerformed(ActionEvent ae) {
            i++;
            createInternalFrame(50 * i, 50 * i);

        }
    }
}
于 2013-09-01T07:26:56.427 回答
0

您必须在第一个 JInternalFrame 的父级中添加稍后的 JInternalFrame。例如,您在 JDesktopPane 中添加第一个 JInternalFrame 作为

    JdesktopPane.add(JInternalFrame_obj1);
    JInternalFrame_obj1.toFront();

然后单击一个按钮以添加另一个 JInternalFrame。如果按钮放置在未显示的 JInternalFrame 上

    JdesktopPane.add(JInternalFrame_obj2);
    JInternalFrame_obj2.toFront();

如果按钮放在 JInternalFrame_obj1 添加

    this.getParent().add(JInternalFrame_obj12);
    JInternalFrame_obj2.toFront();
于 2016-08-18T12:24:18.687 回答