简体   繁体   中英

Java application not responding to code changes

I am trying to implement a simple interface in java but It has become unresponsive to code changes,for example setting a new location to for a combo box does not change the location of it. My code is:

public class Interfata extends JFrame
{
    static JFrame window= new JFrame("Aplicatie bancara");
    static JButton persoana_button= new JButton("PERSOANE");
    static JButton cont_button= new JButton("CONTURI");
    public static void main( String[] args )
    {

        window.setBounds(10, 10, 300, 200);
        JPanel panel_main= new JPanel();
        window.add(panel_main);

        cont_button.setBounds(90, 150, 100, 70);
        panel_main.add(cont_button);
        cont_button.addActionListener(new ActionListener(){ 
            public void actionPerformed(ActionEvent arg0) { 
                 JFrame view = new JFrame("Accounts");
                 JPanel pane= new JPanel();
            String[] account_type=
                {"savings", "spending"};
            JComboBox petList = new JComboBox(account_type);
            petList.setSelectedIndex(1);
            petList.addActionListener(new ActionListener()
                    {
                public void actionPerformed(ActionEvent arg0) {
                    // TODO Auto-generated method stub

                }
                    });
            petList.setSize(50, 50);
            petList.setLocation(400,320);   
                pane.add(petList);
                view.setDefaultCloseOperation(view.DISPOSE_ON_CLOSE);
                view.setBounds(10,10,400,400);
                view.add(pane);
                view.setVisible(true);

            }});


        window.setVisible(true);

As shown in code my window is set to the size, aproximately, 400x400 and I have set the location to the combo box at 400, 320 which should place it outside the frame, but it stays stuck in the middle. 在此处输入图片说明

I am using eclipse neon oxygen., and coding in a Maven project. just after changing my code I got this message:

some code changes cannot be hot swapped into a running virtual machine

I have tried restarting eclipse, changing the workspace location and completely starting a new with the project. However it still won't move.

You have a running copy of your program, that was launched before you made your changes. This running copy is not the same process as your Eclipse IDE.

You need to stop this running copy, and then relaunch the new copy which is compiled on disk, but for one reason or another, could not be patched into the copy that you had already launched.

To find your running copy, you may need to use your operating system's process list and find the correct program (typically containing the word 'java' in the program command line) to terminate.

Once you kill that program, you should be able to relaunch it, and then you should see the changes saved to disk in your last successful compile.

This is mainly related to LayoutManager:

  1. You have added combo box to panel
  2. JPanel have default layout as FlowLayout
  3. FlowLayout adds controls by default with center alignment of container.
  4. FlowLayout do not adhere to setLocation(x,y) hence you don't see any effect

With your existing code, if you set the pane's layout to null ie pane.setLayout(null) , you will surely get the desired result, however null layout is not recommended . Perhaps you may consider using other layouts SpringLayout or GroupLayout but this would require an IDE as manual coding would be a pain.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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