Frage

I realize this question has been asked before (several times) and I have tried all the tips I found online but to no avail! Perhaps the JTabbedPane has something to do with it? Anyway, here is the code I have written (minimal working example):

public class Main extends JPanel {

public Main() {
  super(new GridLayout(1, 1));

JTabbedPane tabbedPane = new JTabbedPane();
ImageIcon icon = createImageIcon("");



for(int i=0; i< 5; i++)
{  
   JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    panel.setLayout(new FlowLayout());

    tabbedPane.addTab("Irrelevant " +(i+1), icon, panel,
    "Hi " + (i+1) + " String");
    tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);

    JLabel label0 = new JLabel("Title of Pane " + (i+1));
    panel.add(label0);

   String[] irrelevantArray = {"hi","sup", "bye"};
    for(String s: irrelevantArray)
    {

         JLabel label = new JLabel(s.toString());
         panel.add(label);
         label.setLocation(0,100);
    }

    add(tabbedPane);        
    tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
   }
 }


protected JComponent makeTextPanel(String text) {
    JPanel panel = new JPanel(false);
    JLabel filler = new JLabel(text);
    filler.setHorizontalAlignment(JLabel.CENTER);
    panel.setLayout(new GridLayout(1, 1));
    panel.add(filler);
    return panel;
}

protected static ImageIcon createImageIcon(String path) {
    java.net.URL imgURL = Main.class.getResource(path);
    if (imgURL != null) {
        return new ImageIcon(imgURL);
    } else {
        System.err.println("Couldn't find file: " + path);
        return null;
    }
}

private static void createAndShowGUI() {
    JFrame frame = new JFrame("Chamber Assignments");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.add(new Main(), BorderLayout.CENTER);

    frame.pack();
    frame.setVisible(true);
}

public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable()
    {
       public void run() {
           UIManager.put("swing.boldMetal", Boolean.FALSE);
           createAndShowGUI();
        }
    }
    );
   }
  }

Of course, the problem with this is that even though I am setting the label at some (arbitrary) location, it does not move! It simple puts the labels centered at the top and going across left to right. I have tried everything I come across to no avail. How can I correct this?

Thank you!

War es hilfreich?

Lösung

Your code makes no sense:

  1. Why do you have a loop to create 5 identical JFrames?
  2. Why are you trying to add multiple labels at the same location?

You should NOT be attempting to set the location of your components. Swing was designed to be used with layout managers. Read the section from the Swing tutorial on Using Layout Managers for working examples. You can also nest panels with different layouts to get your desired effects. The examples will also give you a better idea of how to structure your code.

For example, maybe you can use a BoxLayout. It allows you to add strut and glue components which will help you align components in a line.

when I take the layout manager out, the labels don't even show

That is because layout manager to more than you think. They manage the size and location of a component. Than manage the preferred size of the parent container. They do lots of work behind the scenes to make sure the GUI works properly.

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