繁体   English   中英

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

[英]pass value from one jframe to other jframe?

我创建了两个类,一个类就像表单,另一个类是主类,并且具有jmenu和jinternal框架,我想在jinternal框架上打印来自表单类的输入,但是我不明白如何在表单类中调用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 ){  

        }

    }
}

我猜您想在主窗体上单击按钮时将一些文本传递给InternalFrame类。 修改您的createFrame()方法以接受字符串值,例如-

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

并在调用InternalFrame类时,将此值传递给其构造函数。 例如-

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

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

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

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

ClassName(parameter)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM