简体   繁体   English

JDialog和JFrame以及新实例

[英]JDialog and JFrame and new instance

I have 2 classes - a JFrame with a button, and a JDialog (a pop up) with few textfields on it. 我有2个类-带按钮的JFrame和上面没有几个文本字段的JDialog(弹出窗口)。 Now, when I click the button in JFrame a JDialog shows up: 现在,当我单击JFrame中的按钮时,将显示一个JDialog:

/*** someClass class ****/  
JButton btnNewButton = new JButton("");
btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            jd = new jDialog();
            jd.setModal(true);
            jd.setVisible(true);    
        }
    });
public void doStuff(String one....String five){
   ... ..
  }

Now a jDialog pops up and i need to fill up some textfields inside it and click another button to confirm. 现在,将弹出一个jDialog,我需要填充其中的一些文本字段,然后单击另一个按钮进行确认。

/*** jDialog class ***/   
JButton btnConfirm = new JButton("");
btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            one = 1_tf.getText();
            two = 2_tf.getText();
            three = 3_tf.getText();
            four = 4_tf.getText();
            five = 5_tf.getText();

            doAnything(one,two,three,four,five);

            1_tf.setText("");
            2_tf.setText("");
            3_tf.setText("");
            4_tf.setText("");
            5_tf.setText("");                           
        }
    });

public void doAnything(String one,String two,String three,String four,String five){

 someClass sc = new someClass();
 sc.doStuff(one,two,three,four,five);        
}

The textfield's values will be passed to doAnything() method, inside doAnything() is an instance of someClass class to access sc.doStuff() method and pass the values. 文本字段的值将传递给doAnything()方法,doAnything()内部是someClass类的实例,以访问sc.doStuff()方法并传递值。

The problem now is that, Whenever I click confirm on jDialog class a new JFrame appears so there are 2 JFrames which is not what I want.. It's like whenever I do something on jdialog it creates a new JFrame instead of over lapping the original JFrame. 现在的问题是,每当我在jDialog类上单击“确认”时,就会出现一个新的JFrame,所以有2个JFrame不是我想要的。就像每当我在jdialog上执行某些操作时,它都会创建一个新的JFrame而不是重叠原始的JFrame 。

In the doAnything() don't recreate the someClass (it's better to read java naming convention -classes should start from capital letter) instances. 在doAnything()中不要重新创建someClass(最好阅读java命名约定-classes应该从大写字母开始)实例。

Define class field for the someClass instance? 定义someClass实例的类字段? create it just once (if it's null) and reuse it. 只需创建一次(如果为null),然后重复使用。

That's probably because in the someClass constructor you create an show a JFrame. 这可能是因为在someClass构造函数中创建了一个JFrame展示。 To avoid this problem, make a static reference to the doStuff() method. 为避免此问题,请static引用doStuff()方法。

Declare it as: 声明为:

public static doStuff(one, two, three, four, five) {
    ...
}

and use it as: 并将其用作:

someClass.doStuff(one, two, three, four, five);

Now, when you use the method, you don't have to create an object of someClass class, and then you don't have to call the constructor method. 现在,使用该方法时,不必创建someClass类的对象,也不必调用构造函数方法。

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

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