Frage

I'm working on a pathfinder program which uses GUI. I tried to run the program before I added some stuff and it showed everything fine. But after working on it some more it stopped showing the buttons and the menu bar.

Here is the code. Some variable names are in Swedish as well, but I hope that it won't be an issue. (Please take in mind that the program is far from finished.) Thank you in advance!

import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;

public class Pathfinder extends JFrame {

    JButton hittaVäg, visaFörbindelse, nyPlats, nyFörbindelse, ändraFörbindelse;
    JMenuBar menyBar;
    JMenuItem ny, avsluta, hittaVägMeny, visaFörbindelseMeny, nyPlatsMeny, nyFörbindelseMeny, ändraFörbindelseMeny;
    String str = System.getProperty("user.dir");
    JFileChooser jfc;
    BildPanel Bild = null;

    Pathfinder(){

        super("PathFinder");
        setLayout(new BorderLayout());
        setSize(590, 400);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
        jfc = new JFileChooser(".");

        JPanel norra = new JPanel();
        add(norra, "norra");


        JButton hittaVäg = new JButton("Hitta väg");
        JButton visaFörbindelse = new JButton("Visa förbindelse");
        JButton nyPlats = new JButton("Ny plats");
        JButton nyFörbindelse = new JButton("Ny förbindelse");
        JButton ändraFörbindelse = new JButton("Ändra förbindelse");

        norra.add(hittaVäg);
        norra.add(visaFörbindelse);
        norra.add(nyPlats);
        norra.add(nyFörbindelse);
        norra.add(ändraFörbindelse);

        hittaVäg.addActionListener(new HittaLyss());
        visaFörbindelse.addActionListener(new VisaLyss());
        nyPlats.addActionListener(new NyPlatsLyss());
        nyFörbindelse.addActionListener(new NyFörbindelseLyss());
        ändraFörbindelse.addActionListener(new NyFörbindelseLyss());


        JMenuBar menyBar = new JMenuBar();
        setJMenuBar(menyBar);

        JMenu arkivMeny = new JMenu("Arkiv");
        JMenu operationerMeny = new JMenu("Operationer");

        menyBar.add(arkivMeny);
        menyBar.add(operationerMeny);


        JMenuItem ny = new JMenuItem("Ny");
        JMenuItem avsluta = new JMenuItem("Avsluta");

        arkivMeny.add(ny);
        arkivMeny.add(avsluta);

        ny.addActionListener(new NyLyss());
        avsluta.addActionListener(new AvslutaLyss());


        JMenuItem hittaVägMeny = new JMenuItem("Hitta väg");        
    JMenuItem visaFörbindelseMeny = new JMenuItem("Visa förbindelse");  


        JMenuItem nyPlatsMeny = new JMenuItem("Ny plats");
        JMenuItem nyFörbindelseMeny = new JMenuItem("Ny förbindelse");
        JMenuItem ändraFörbindelseMeny = new JMenuItem("Ändra förbindelse");

        operationerMeny.add(hittaVägMeny);
        operationerMeny.add(visaFörbindelseMeny);
        operationerMeny.add(nyPlatsMeny);
        operationerMeny.add(nyFörbindelseMeny);
        operationerMeny.add(ändraFörbindelseMeny);

        hittaVäg.addActionListener(new HittaLyss());
        visaFörbindelse.addActionListener(new VisaLyss());
        nyPlats.addActionListener(new NyPlatsLyss());
        nyFörbindelse.addActionListener(new NyFörbindelseLyss());
        ändraFörbindelse.addActionListener(new ÄndraFörbindelseLyss());




    }

    class HittaLyss implements ActionListener{
        public void actionPerformed(ActionEvent ave){ 

        }

    }
    class VisaLyss implements ActionListener{
        public void actionPerformed(ActionEvent ave){ 

        }

    }
    class NyPlatsLyss implements ActionListener{
        public void actionPerformed(ActionEvent ave){ 

        }

    }
    class NyFörbindelseLyss implements ActionListener{
        public void actionPerformed(ActionEvent ave){ 

        }

    }
    class ÄndraFörbindelseLyss implements ActionListener{
        public void actionPerformed(ActionEvent ave){ 

        }

    }
    class NyLyss implements ActionListener{
        public void actionPerformed(ActionEvent ave){ 
            int svar = jfc.showOpenDialog(Pathfinder.this);
            if (svar == JFileChooser.APPROVE_OPTION){
                File f = jfc.getSelectedFile();
                String filnamn = f.getAbsolutePath();
                if (Bild != null)
                    remove(Bild);
                Bild = new BildPanel(filnamn);
                add(Bild, BorderLayout.CENTER);
                validate();
                repaint();
                pack();
            }
        }

    }
    class AvslutaLyss implements ActionListener{
        public void actionPerformed(ActionEvent ave){ 

        }

    }
    public static void main (String[] args){
        new Pathfinder();
    }
}
War es hilfreich?

Lösung

One of the issues is IllegalArgumentException at line

add(norra, "norra");

The layout of the frame's content pane is set to BorderLayout, but this layout does not understand "norra" constraint. See How to Use BorderLayout for more details and examples.

Also, you should call setVisible once all the components are added and initialized.

Andere Tipps

Call setVisible(true) on the JFrame after adding all components, not before. The order should be:

  1. Add components
  2. Call pack() on the JFrame
  3. Then call setVisible(true) on the JFrame.

You need to first add the components and then call setVisible(true).
So the order matters here. Try it, see if it helps.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top