开发者

Calling a separate Java file to open a second gui window

I currently have a java file that has buttons to create new GUI windows. I have a second java file that is in the same project that I would like to have open when the user clicks a specific button. The second file has all the components add and just needs to b开发者_Go百科e initiated in my "Main java file" (file one). How can I do this? (calling and calling and instantiating the second java file not making the method).

Any Comments or suggestions are appreciated.

Thanks!


Since this post isn't marked as solved yet, I suppose you haven't found a solution to the problem. Only 2 things can be wrong here. Either your second class is wrong, or your listener is. All you need to make sure to get your "second frame" to show is to add the setVisible(true), and to create a new object of your second class as stated in previous posts.

This is what your second class needs to create an empty window:

public class MySecondClass extend JFrame{

   public MySecondClass(){
        setSize(200, 200);
        setVisible(true);
        //additional calls
     }
}

and your listener should be as below in the constructor of your mainclass:

theButton.addActionListener(new buttonListener());

and last, add the following as a inner class in your mainclass:

public class buttonListener implements ActionListener{
        public void actionPerformed(ActionEvent e){
                new MySecondClass();
          }
}

if you still can't get your code to work, add a printline in the constructor of your second class, and one in your listener class so you can figure out which part is faulty


Hmm, I don't know if I understand correctly, but you want create windows when clicking on a button?

Try this:

new GUIConstructorNameHere.setVisible(true);

PS: The GUIConstructorName is the constructor method of the class that you want to show.


You should create a method for the same, say you have two files, eg. file1 & file2 You have to navigate from file1 to file2 on click of a button in file1.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
file2 f2 = new file2();
this.dispose(); //closes current file
f1.seVisible(true); //opens the next file
}


You didn't give enough information. Java has more than one GUI library, Swing and SWT being the ones that come immediately to mind. If you're using Swing and your "second class file" that you want to show extends JPanel, then you can create a new YourSecondClass() and add it to your JFrame. In Swing all components must be added to a Container, so your second class must either BE the container (e.g. JPanel), or the components in your second class can be added to an existing container.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜