当前位置: 代码迷 >> J2SE >> 请教关于JAVA窗口间的数值传递
  详细解决方案

请教关于JAVA窗口间的数值传递

热度:98   发布时间:2016-04-24 12:14:27.0
请问关于JAVA窗口间的数值传递
请问各位,如何实现在Main窗口中点击按钮弹出用户另一个窗口RequestValueDialog,让用户在两个textfield中输入数据,点击OK后将输入数据回传给原来窗口显示在文本框中?
因为类中有其他方法,可以不使用静态变量吗?
貌似不能传图片,那就传简单的示例代码。
Main.java
Java code
public class Main extends JFrame (){        private JPanel contentPane;    private JButton btnClick;    private JTextField textField;        public static void main(String[] args) {                new Main().setVisible(true);        }        public Main() {        initGUI();    }    private void initGUI() {        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        setBounds(100, 100, 450, 300);        contentPane = new JPanel();        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));        setContentPane(contentPane);        contentPane.setLayout(new BorderLayout(0, 0));                btnClick = new JButton("click");        contentPane.add(btnClick, BorderLayout.NORTH);                textField = new JTextField();        contentPane.add(textField, BorderLayout.CENTER);    }}

RequestValueDialog.java
Java code
public class  RequestValueDialog extends JDialog(){        public RequestValueDialog() {        setBounds(100, 100, 450, 300);        getContentPane().setLayout(new BorderLayout());        contentPanel.setLayout(new FlowLayout());        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));        getContentPane().add(contentPanel, BorderLayout.CENTER);        {            textField = new JTextField();            contentPanel.add(textField);            textField.setColumns(10);        }        {            textField_1 = new JTextField();            textField_1.setColumns(10);            contentPanel.add(textField_1);        }        {            textField_2 = new JTextField();            textField_2.setColumns(10);            contentPanel.add(textField_2);        }        {            JPanel buttonPane = new JPanel();            buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));            getContentPane().add(buttonPane, BorderLayout.SOUTH);            {                JButton okButton = new JButton("OK");                okButton.setActionCommand("OK");                buttonPane.add(okButton);                getRootPane().setDefaultButton(okButton);            }            {                JButton cancelButton = new JButton("Cancel");                cancelButton.setActionCommand("Cancel");                buttonPane.add(cancelButton);            }        }    }}


------解决方案--------------------
JDialog本身有构造方法new JDialog(Frame owner, String title, boolean modal),你直接写

public RequestValueDialog(Frame owner, String title, boolean modal) {
super(owner,title, modal);
………………
}
就行了
------解决方案--------------------
第一个JTextField的内容可以回显到主Frame上面,你参考看看吧

Java code
import javax.swing.*;import javax.swing.border.EmptyBorder;import java.awt.*;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;public class Main extends JFrame implements ActionListener {    private JPanel contentPane;    private JButton btnClick;    public JTextField getTextField() {        return textField;    }    public void setTextField(JTextField textField) {        this.textField = textField;    }    private JTextField textField;        public static void main(String[] args) {        new Main().setVisible(true);    }    public Main() {        initGUI();    }    private void initGUI() {        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        setBounds(100, 100, 450, 300);        contentPane = new JPanel();        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));        setContentPane(contentPane);        contentPane.setLayout(new BorderLayout(0, 0));        btnClick = new JButton("click");        btnClick.addActionListener(this);        contentPane.add(btnClick, BorderLayout.NORTH);        textField = new JTextField();        contentPane.add(textField, BorderLayout.CENTER);    }    @Override    public void actionPerformed(ActionEvent e) {        //To change body of implemented methods use File | Settings | File Templates.        if(e.getSource()==btnClick){            RequestValueDialog dialog=new RequestValueDialog(this);            dialog.setVisible(true);                   }    }}
  相关解决方案