开发者

Understanding layout managers -- I'm not, please enlighten me

开发者 https://www.devze.com 2023-02-21 14:49 出处:网络
I\'m just not understanding why things are being resized when I call the validate() and repaint() methods.I\'m struggling to understand this.Essentially, my program is meant to display like this.I hav

I'm just not understanding why things are being resized when I call the validate() and repaint() methods. I'm struggling to understand this. Essentially, my program is meant to display like this. I have a main frame into which I plug the various JPanels that I'm extending for the various functions of my photo album. The class below is the NewAlbum class that is supposed to allow the user to select files and make a new album out of them.

The code for choosing files works nicely. Once the files are selected, the change to the NewAlbum panel should be the select files button is replaced by a done button. Under the done button is a JSplitPane with the horizontal splitter just off center with the right side being larger than the left. The left side will eventually have a thumbnail of each photo as metadata about the photo is entered into the right side.

The right side pane is to be a JScrollPane with a single JPanel which has, in a grid form, the 4 entries that the user is asked for data about. After adding everything, the dimensions are where I want them to be, but when I call the validate/repaint combination the dimensions become "messed up." I'm pretty sure it's because I'm not understanding how the default layout managers for the various classes I'm using, or extending. Please help me understand. Also, tell me if the GridBagLayout is what I want, or if a different one is what I'm looking for.

The NewAlbum code is below.

I apologize for the uncompilable code. I figured that you'd be able to just look at the class and tell me, "Oh, yeah, this is the problem." Below is compilable and does demonstrate the problem. Once the files are selected, the split pane window is too thin and too long. I want it to fit inside the frame. Actually, it should fit inside the JPanel which is inside the JFrame.

Thanks, Andy

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

class Main extends JFrame {
   static JPanel transientPanel = null;

   public Main() {
      super();
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setSize(640, 480);
      JMenuBar menuBar = new JMenuBar();
      JMenu menu = new JMenu("Example");
      JMenuItem albumMenu = new JMenuItem("New Album");
      albumMenu.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent ae) {
            transientPanel = new NewAlbum();
            add(transientPanel);
            validate();
            repaint();
         }
      });
      menu.add(albumMenu);
      menuBar.add(menu);

      setJMenuBar(menuBar);
      validate()开发者_JAVA百科;
   }

   public static void main(String[] args) {
      final Main m = new Main();
      m.setVisible(true);
   }
}
/**
 * @description NewAlbum is the window that is presented to the user
 * to select new photographs for the album.  Once selected, the user
 * will be presented a form, of sorts, to complete the metadata for this
 * album.
 * @author Andy
 */
class NewAlbum extends JPanel {
    JButton selectFiles;
    JButton done;
    JButton nextButton = new JButton("Next Image");

    ArrayList<File> filesArray;

    JSplitPane splitWindow = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true);
    JScrollPane scrollWindow;
    JPanel rightSidePanel = new JPanel();
    JPanel leftSidePanel = new JPanel();

    JLabel subjectLabel = new JLabel("Image subject:");
    JLabel locationLabel = new JLabel("Image location:");
    JLabel commentLabel = new JLabel("Comments:");
    JLabel dateLabel = new JLabel("Date (mm/dd/yyyy):");

    JTextField subjectText = new JTextField(25);
    JTextField locationText = new JTextField(25);
    JTextArea commentText = new JTextArea(4, 25);
    JTextField dateText = new JTextField(10);

    public NewAlbum() {
        super();

        selectFiles = new JButton("Select Photos");

        selectFiles.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                selectFilesForAlbum();
            }
        });
        add(selectFiles);
    }

    private void configureRightPanel() {
        int jPanelX = getParent().getWidth();
        int jPanelY = getParent().getHeight() - 30; // this should account for buttons

        // now, resize this panel so that it will be the right size for the split pane
        jPanelX = jPanelX - (int)(jPanelX * .31);
        rightSidePanel.setSize(jPanelX, jPanelY);

        rightSidePanel.add(subjectLabel);
        rightSidePanel.add(subjectText);
        rightSidePanel.add(locationLabel);
        rightSidePanel.add(locationText);
        rightSidePanel.add(commentLabel);
        rightSidePanel.add(commentText);
        rightSidePanel.add(dateLabel);
        rightSidePanel.add(dateText);
        rightSidePanel.add(nextButton);

        // iterate over the photos selected, make bogus info for now
    }

    private ArrayList<File> makeFileIntoArrayList(File[] f) {
        ArrayList<File> a = new ArrayList<File>();
        a.addAll(Arrays.asList(f));

        return filesArray = a;
    }

    /**
     * selectFilesForAlbum
     * This method is private to the NewAlbum class.  It is the handler for
     * when the user clicks on the "select photos" button.  When the function
     * executes, it displays the JFileChooser so that the user may select
     * the desired photos.  The files selected are assigned to a class variable
     * of type File[] which is used by the enterPhotoInfo method.
     *
     * @return void
     */
    private void selectFilesForAlbum() {
        JFileChooser jfc = new JFileChooser();
        jfc.setMultiSelectionEnabled(true);
        jfc.showOpenDialog(this);
        makeFileIntoArrayList(jfc.getSelectedFiles());

        changeButtonToDone();
        enterPhotoInfo();

        // TODO write the photo album to the disk
    }

    private void changeButtonToDone() {
        remove(selectFiles);
        done = new JButton("Done");
        add(done);

        // by the time this gets called, we'll have a parent container
        getParent().validate();
        getParent().repaint();
    }

    private void enterPhotoInfo() {
        splitWindow.setSize(this.getWidth(), this.getHeight() - 30);

        // remove when the left side panel actually has something
        Dimension iewDims = splitWindow.getSize();
        int leftX = iewDims.width - (int)(iewDims.width * .69);
        int leftY = iewDims.height;

        leftSidePanel.setSize(leftX, leftY);

        configureRightPanel();

        scrollWindow = new JScrollPane(rightSidePanel);
        scrollWindow.setSize(rightSidePanel.getSize());

        splitWindow.setRightComponent(scrollWindow);
        splitWindow.setLeftComponent(leftSidePanel);
        splitWindow.setDividerLocation(.31);

        System.out.println("Printing dimensions of before validate/repaint: this, splitWindow, scrollWindow, LSP, RSP");
        debugPrintDimensions(this);
        debugPrintDimensions(splitWindow);
        debugPrintDimensions(scrollWindow);
        debugPrintDimensions(leftSidePanel);
        debugPrintDimensions(rightSidePanel);

        //infoEntryWindow.add(infoScroller);
        this.add(splitWindow);
        this.validate();
        this.repaint();

        System.out.println("Printing dimensions of: this, splitWindow, scrollWindow, LSP, RSP");
        debugPrintDimensions(this);
        debugPrintDimensions(splitWindow);
        debugPrintDimensions(scrollWindow);
        debugPrintDimensions(leftSidePanel);
        debugPrintDimensions(rightSidePanel);
    }

    private void debugPrintDimensions(Container c) {
        System.out.println("DEBUG: Containers (x,y): (" +
                String.valueOf(c.getWidth()) +
                "," +
                String.valueOf(c.getHeight()) +
                ")");
    }
}


Also, tell me if the GridBagLayout is what I want, or if a different one is what I'm looking for.

You use the appropriate layout manager for the job. This can also mean using different layout managers on different panels.

splitWindow.setSize(this.getWidth(), this.getHeight() - 30); 

You should NEVER use setSize(). That is the job of the layout manager, to determine the size of the component based on the rules of the layout manager.

All components have a preferred size which is used by the layout manager. At times you can use the setPreferredSize() to change the default.


By selecting a LayoutManager, you are handing over control of the layout to that layout manager. You can give the LayoutManager hints via layout constraints and restrictions by setting the preferred dimensions on the components you are arranging, but essentially, the layout manager will call the shots.

With GridBagLayoutManager you can achieve almost anything with constraints and component dimension settings, but it can still be tricky to get right. Try setting the preferred size on your components.

I used to use GridBagLayoutManager for everything, but then I came across MigLayout which really is a huge step forward in terms of easy configuration and layout consistency. I recommend you give it a look.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号