0

I'm new to eclipse and java, I'm trying to switch between two panels with the use of a button. i want to test my project but when i try to run it it launches, gives no errors and then nothing happens.

Have i missed something that i need to do before to make this run?

This is the version i have and i downloaded it earlier today.

Eclipse IDE for Java Developers

Version: 2019-09 R (4.13.0) Build id: 20190917-1200

And below is my code if there is anything stopping it from working in there.

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Music_Test {

    private JFrame frame;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Music_Test window = new Music_Test();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Music_Test() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JPanel Menu = new JPanel();
        Menu.setBounds(6, 6, 438, 266);
        frame.getContentPane().add(Menu);
        Menu.setLayout(null);
        Menu.setVisible(true);

        JPanel Select_Level = new JPanel();
        Select_Level.setBounds(6, 6, 438, 266);
        frame.getContentPane().add(Select_Level);
        Select_Level.setVisible(false);

        JButton btnSelectLevel = new JButton("Select Level");
        btnSelectLevel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Menu.setVisible(false);
                Select_Level.setVisible(true);
            }
        });
        btnSelectLevel.setBounds(158, 45, 117, 29);
        Menu.add(btnSelectLevel);

        JLabel lblMenu = new JLabel("Menu");
        lblMenu.setFont(new Font("Comic Sans MS", Font.BOLD, 22));
        lblMenu.setBounds(187, 17, 61, 29);
        Menu.add(lblMenu);

        JLabel lblSelectLevel = new JLabel("Select Level");
        lblSelectLevel.setFont(new Font("Comic Sans MS", Font.BOLD, 22));
        lblSelectLevel.setBounds(187, 17, 61, 29);
        Select_Level.add(lblSelectLevel);


    }
}
A Stephens
  • 21
  • 5
  • How are you running it? Do you have multiple monitors (check if it opens there) because your code ran for me. – sleepToken Nov 27 '19 at 15:01
  • I'm just clicking the run button at the top of the screen and with the run>run in the menu at the top of the screen – A Stephens Nov 27 '19 at 15:03
  • [Not reproducible here either.](https://i.imgur.com/uNirakn.png) – Marv Nov 27 '19 at 15:03
  • what does that mean @Marv? – A Stephens Nov 27 '19 at 15:04
  • He has linked the image that I am also seeing. Your program, running, with the code you provided. – sleepToken Nov 27 '19 at 15:05
  • I don't experience the same issue you're having. The program runs fine for me, and the linked window opens when running it. – Marv Nov 27 '19 at 15:05
  • do i need to set something up in eclipse to launch the window then? – A Stephens Nov 27 '19 at 15:07
  • I've just tested it in blueJ and it works from there. Anyone know what the issue might be in eclispe for it not to run? – A Stephens Nov 27 '19 at 15:16
  • 1
    I confirm that it works as expected on Eclipse IDE. Just right-click on your file > Run As > Java Application. By the way, you can check that an application has been launched by checking the _Console_ view: it should display ` Music_Test [Java Application]` at the top. – Emmanuel Chebbi Nov 27 '19 at 15:20
  • I've tried that also and nothing happens, it looks like it still running but i cannot see any window appear. – A Stephens Nov 27 '19 at 15:25
  • @AStephens how does the _Console_ view looks like? Is ` ...` shown? You should also see a square on the right of the "Console" title; if it's red then the application is running, otherwise it is over/not started. Could you try to add a breakpoint (double-clicking on a line) then launching the application in Debug mode (right-click > Debug As > Java Application) and see what happens? Eclipse should open the Debug perspective that shows the application's stacktrace on the left – Emmanuel Chebbi Nov 27 '19 at 15:39
  • Yeah the red square is there but no window opens...I waited 5/10 mins to see if it was just my computer being slow. – A Stephens Nov 27 '19 at 15:57
  • OK, so either the application is stuck or the window is hidden somewhere. Can you make sure it isn't just out of your sight? Otherwise, could you run the application in debug mode as I explained before? The stacktrace should allow you to determine where exactly the application is stuck. – Emmanuel Chebbi Nov 27 '19 at 16:02
  • When i go to debug as the option of a java application doesn't appear. – A Stephens Nov 27 '19 at 16:06
  • I think i need to add a jdk but not sure how to do this – A Stephens Nov 27 '19 at 16:20
  • I think that the _Debug As > Java Application_ should be available for each Java class with a `main` method. Alternatively you can create a debug configuration manually by going to `Run > Debug Configuration > double-click on Java Application` (see also resources on internet such as [this article](https://www.eclipse.org/community/eclipse_newsletter/2017/june/article1.php)). – Emmanuel Chebbi Nov 27 '19 at 16:22
  • Well, you need at least a JRE, see for instance [here](https://www.zkoss.org/wiki/Setting_Default_JRE_In_Eclipse) for the steps. I think Eclipse IDE would have tell you if you didn't have any, though. Are you able to run a simple HelloWorld? – Emmanuel Chebbi Nov 27 '19 at 16:24
  • @EmmanuelChebbi Yes i can run a simple hello world program that appears in the console. – A Stephens Nov 27 '19 at 16:28
  • Great! Did you manage to debug the execution of the program? – Emmanuel Chebbi Nov 27 '19 at 16:55

1 Answers1

1

Start by using a different layout manager, FlowLayout or GridBagLayout might work better

JPanel centerPanel = new JPanel(new FlowLayout());
centerPanel.add(openReportSelection);     
centerPanel.add(closeButton); 

These layouts will honour the preferred sizes of your buttons

As for opening another window, well, you've already create one, so the process is pretty much the same. Having said that, you might consider having a look at The Use of Multiple JFrames: Good or Bad Practice? before you commit yourself to far.

A better approach might be to use a JMenuBar and JMenuItems to act as the "open" and "exit" actions. Take a look at How to Use Menus then you could use a CardLayout to switch between views instead, for example

From a pure design perspective (I know it's only practice, but perfect practice makes perfect), I wouldn't extend anything from JFrame and instead would rely on building your main GUIs around something like JPanel instead.

This affords you the flexibility to decide how to use these components, as you could add them to frames, applets or other components...

AND

Although you have implemented the actionPerformed method as per the ActionListener interface, you class is not of that that type as you haven't implemented the interface. Once you implement that interface and register it with the JButton btnAdd,

btnAdd.addActionListener(this);

the method will be called.

A more compact alternative might be to use an anonymous interface:

btnAdd.addActionListener(new ActionListener() {
   @Override
   public void actionPerformed(ActionEvent e) {
      // handle button ActionEvent & display dialog...    
   }
});

Side notes:

Using more than one JFrame in an application creates a lot of overhead for managing updates that may need to exist between frames. The preferred approach is to use a modal JDialog if another window is required. This is discussed more Here

Kiran Mistry
  • 2,614
  • 3
  • 12
  • 28
  • Thanks for your feedback, I'll take note. But at the moment i cant test it because it wont run which is my issue – A Stephens Nov 27 '19 at 15:10