0

I am doing a demo project, where this time I have created different packages and put some different JFrames in those packages.

I want it to open when I call the JFrame of a different package from the JFrame of another package.

But, I have three packages, one of which is package com.mkw.adminpanel; which contain a JFrame named Admin And by clicking the button here I want package com.mkw.loginpanel; To open the JFrame named Start inside. But it is not opening.And package com.mkw.studentpanel; The frame named Start inside this package is opening.

the package named package com.mkw.adminpanel; code of JButton actionperformed is given below:

package com.mkw.adminpanel;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import com.mkw.studentpanel.Start;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Admin extends JFrame {
private JPanel contentPane;
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Admin frame = new Admin();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}
public Admin() {
    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(null);
    
    JButton btnNewButton = new JButton("New button");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            dispose();
            Start s = new Start();
            s.setVisible(true);
        }
    });
    btnNewButton.setBounds(175, 182, 89, 23);
    contentPane.add(btnNewButton);
   }
}
MK.Whridoy
  • 68
  • 1
  • 8
  • 2
    Relevant information: [The Use of Multiple JFrames: Good or Bad Practice?](https://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-or-bad-practice) – maloomeister May 07 '21 at 09:08
  • 3
    In general is not a good idea to have classes with same name in the same project, even in different packages, nevertheless you can distinguish them by using the fully qualified name, so replace `new Start(...);`, with `com.mkw.loginpanel.Start(...);` – Rocco May 07 '21 at 09:10
  • @maloomeister I know about card layout. I always use this layout. But this time I want to do with different packages.Because in Eclipse the codes are so long if i use card layout and hard to find specific line. – MK.Whridoy May 07 '21 at 09:12
  • 2
    1) What's a `Start`? For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). Hard code data to replace the DB. 2) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556).3) Also, agree with @maloomeister. `JDialog`s! – Andrew Thompson May 07 '21 at 09:13
  • 2
    But using a `CardLayout` does not stop you from structuring your code? You can e.g. instead of subclassing `JFrame`, subclass `JPanel` and add these to your `CardLayout`? – maloomeister May 07 '21 at 09:15
  • @Rocco Thanks It works! Actually I have never used different packages in the same project before. So, really I have no Idea about it. I Always use card layout for multiple frames. that's why for the first time I find the problem :( – MK.Whridoy May 07 '21 at 09:15
  • 2
    *"in Eclipse the codes are so long if i use card layout"* They don't **have** to be long! You might code each panel or view as a single class, then reference them from the code that uses the card layout. – Andrew Thompson May 07 '21 at 09:15
  • @maloomeister card layout is really useful for me.But, I am trying to do something different this time.That's why I avoid card layout this time. otherwise in my many projects i use card layout to make my projects. – MK.Whridoy May 07 '21 at 09:18
  • @AndrewThompson I mean that also. sorry for my typing or grammatical mistake – MK.Whridoy May 07 '21 at 09:19

0 Answers0