Frage

I am having trouble formatting the GUI for a program I am developing. I need to have four buttons with a JList above it. My JList keeps appearing besides all of my buttons instead of above them. Could anyone point me in the correct direction to solve my problem?

import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;


public class Test extends JPanel {

    private static final long serialVersionUID = 1L;

    private JList jlist; 

    public static void main(String[] args) {
        JFrame boxOptions = new JFrame("Calculator");
        boxOptions.setSize(0,0);
        boxOptions.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        boxOptions.setResizable(false);
        boxOptions.setLayout(new BorderLayout());
        boxOptions.add(new Test(), BorderLayout.CENTER);
        boxOptions.pack();
        boxOptions.setLocationRelativeTo(null);
        boxOptions.setVisible(true);

    }

    public Test(){  
        setLayout(new GridLayout(1, 4, 5, 5));

        add(new JButton("add"));
        add(new JButton("Check In"));
        add(new JButton("Check Out"));
        add(new JButton("Delete"));


        String[] Titles = {"one", "two", "three"};
        jlist = new JList(Titles);
        jlist.setVisibleRowCount(3);
        add(new JScrollPane(jlist), BorderLayout.NORTH);

    }

}
War es hilfreich?

Lösung 2

First of all , the BorderLayout Parameters use only when the layout is BorderLayout. You can use GridBagLayout Instead. Sample code is below

    setLayout(new GridBagLayout());

    add(new JButton("add"), new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
    add(new JButton("Check In"), new GridBagConstraints(1, 1, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
    add(new JButton("Check Out"), new GridBagConstraints(2, 1, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
    add(new JButton("Delete"), new GridBagConstraints(3, 1, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));


    String[] Titles = {"one", "two", "three"};
    jlist = new JList(Titles);
    jlist.setVisibleRowCount(3);
    add(new JScrollPane(jlist), new GridBagConstraints(0, 0, 4, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));

Is this you expected ?

Andere Tipps

You're setting the layout of your Test JPanel class to GridLayout, and then trying to add a component to it using BorderLayout.NORTH specifier -- which is not kosher as that specifier only works for a BorderLayout and is meaningless for your GridLayout-using Test JPanel.

Solution: nest JPanels. Have an outer JPanel use a BorderLayout, and add the list's JScrollPane to this outer JPanel using the BorderLayout.NORTH specifier, and then create an inner JPanel that uses GridLayout, that holds your JButtons and add it to the main outer JPanel in the BorderLayout.CENTER position.

Most important though: read the Swing tutorials which you can find here, in particular the section on using the layout managers, since there is no need to guess at this stuff.

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