将值从一个jframe传递到另一个jframe?

时间:2012-10-03 18:43:24

标签: java user-interface

我创建了两个类,一个类就像形式,另一个类是主类,并且有jmenu和jinternal帧我想打印来自jinternal帧上的表单类的输入但是我无法理解我是如何回想起jinternalframe中的jinternalframe表格类,请指导我这方面或任何提示或一些代码或教程,可以帮助我这里是两个类的代码。此外,这两个班级都运作良好。

JTextArea text;
     static int openFrameCount = 0;
    public form(){


           super("Insert Form");
        Container panel=getContentPane();
        JPanel  cc    = new JPanel();
        cc.setLayout(new  FlowLayout());


        JButton b=new JButton("print");
     b.setPreferredSize(new Dimension(140,50));
     b.setBounds(1000,500,350,50);
       cc.add(b);
       .......................................................


        JLabel label1=new JLabel(" Question"+(++openFrameCount));

        cc.add(label1);
        text=new JTextArea();
                text.setLineWrap(true);
        text.setWrapStyleWord(true);
        text.setPreferredSize(new Dimension(750,50));
        text.setBounds(80, 60,750,50);
        cc.add(text);
         JLabel symbol=new JLabel("Selection for Option?");
         symbol.setBounds(200, 120,1000,100);
 cc.add(symbol);

  ..................................................



          JLabel op4=new JLabel("4th Option?");
           JTextArea otext4=new  JTextArea();
          otext4.setLineWrap(true);
        otext4.setWrapStyleWord(true);
        otext4.setPreferredSize(new Dimension(750,50));
        otext4.setBounds(10, 40,700,30);
            cc.add( op4 ) ;
          cc.add( otext4 ) ;






          cc.revalidate();
 validate();

  ............................................................  

        }

    @Override
    public void actionPerformed(ActionEvent ae) {
     if ( e.getSource() == b1 ){  

    }

}

} 而第二类jinternalframe是

public class Desktop1 extends JFrame
                               implements ActionListener {
    Desktop p=new Desktop();
    JDesktopPane desktop;

static int openFrameCount = 0;
    public Desktop1() {
        super("InternalFrameDemo");

        //Make the big window be indented 50 pixels from each edge
        //of the screen.
        int inset = 50;
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds(inset, inset,
                  screenSize.width  - inset*2,
                  screenSize.height - inset*2);

        //Set up the GUI.
        desktop = new JDesktopPane(); //a specialized layered pane

        createFrame(); //create first "window"
        setContentPane(desktop);
        setJMenuBar(createMenuBar());

        //Make dragging a little faster but perhaps uglier.
        desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
    }

    protected JMenuBar createMenuBar() {
        JMenuBar menuBar = new JMenuBar();

        //Set up the lone menu.


       .................................................



        return menuBar;
    }

    //React to menu selections.
    public void actionPerformed(ActionEvent e) {
        if ("new".equals(e.getActionCommand())) { //new
            createFrame();
        } 

        ............................................
        }
    }


    class MyInternalFrame extends JInternalFrame {

        static final int xPosition = 30, yPosition = 30;
        public MyInternalFrame() {
            super("IFrame #" + (++openFrameCount), true, // resizable
                    true, // closable
                    true, // maximizable
                    true);// iconifiable
            setSize(700, 700);
            // Set the window's location.
            setLocation(xPosition * openFrameCount, yPosition
                    * openFrameCount);
        }
    }
    //Create a new internal frame.
    protected void createFrame() {
        Desktop1.MyInternalFrame frame = new Desktop1.MyInternalFrame();


        JPanel panel=new JPanel();//to add scrollbar in jinternalpane insert jpanel
        panel.setBackground(Color.white);//set background color of jinternal frame
        JScrollPane scrollBar=new JScrollPane(panel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        frame.add(scrollBar);

        frame.setVisible(true); 


        desktop.add(frame);
        try {
            frame.setSelected(true);
           frame.setMaximum(true);
        } catch (java.beans.PropertyVetoException e) {}
    }
    public static void main(String[] args) {
        Desktop1 d=new Desktop1();
         d.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        d.setVisible(true);

}
}

我想知道关于这部分代码中的工作的提示,以便在我点击打印按钮时将表单的值传递给内部框架

public void actionPerformed(ActionEvent ae) {
         if ( e.getSource() == b1 ){  

        }

    }
}

2 个答案:

答案 0 :(得分:0)

我想你想在主窗体上点击按钮时将一些文本传递给你的InternalFrame类。 修改createFrame()方法以接受String值 e.g -

protected void createFrame(String value){
 //..your code    
}

当您调用InternalFrame类时,将此值传递给其构造函数。 e.g -

 Desktop1.MyInternalFrame frame = new Desktop1.MyInternalFrame(value); 

参数化构造函数将解决您的问题。修改您的InternalFrame构造函数 例如 -

public MyInternalFrame(String value){
 //..use this value    
}

答案 1 :(得分:0)

只需调用构造函数并传递值

ClassName(parameter)