分离JLayeredPane

时间:2012-04-01 19:01:20

标签: java swing layout separator jlayeredpane

我在分层窗格中有一组面板。我需要一个分隔符来将sideBar与topBar和tabbedPanel分开。我留下了一个10像素的缓冲区供它放置。不幸的是,可能由于它是一个JLayeredPane,我可以让它查看。

有没有办法定义分隔符的X位置?因为这应该解决它。无论哪种方式,这里都是代码的示例,我从中删除了大部分信息。

或者,完全提供不同的解决方案,只要我可以从sideBar和其他两个面板获得定义的分割。我已经尝试将BorderLayout.WEST应用到sideBar,但由于它是一个JLayeredPane,它给了我错误。

lPane = new JLayeredPane();
lPane.setBounds(0, 0, 1024, 768);

calendarFrame = new JFrame ("Calendar Frame");
calendarFrame.setPreferredSize(new Dimension(1024, 768));
calendarFrame.setLayout(null);

//Prepare side bar
sideBar = new JPanel ();
sideBar.setLayout(null);
sideBar.setBounds(0, 0, 210, 768);

//Prepare top bar
topBar = new JPanel ();
topBar.setLayout(null);
topBar.setBounds(220, 0, 774, 50);

//Create tabbed pane
tabbedPane = new JTabbedPane();
tabbedPane.setBounds(220, 50, 774, 700);
//Tab code here, but not needed for questuion

calendarFrame.add(lPane, BorderLayout.CENTER);
lPane.add(sideBar, new Integer(0), 0);
lPane.add(Box.createHorizontalStrut(5));
lPane.add(new JSeparator(SwingConstants.VERTICAL));
lPane.add(Box.createHorizontalStrut(5));
lPane.add(topBar, new Integer(1), 0);
lPane.add(tabbedPane, new Integer(2), 0);

1 个答案:

答案 0 :(得分:2)

编辑:

如果你想创建固定的10pixels差距,

你可以使用

我的好奇心是你......

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;

public class EditableListExample extends JFrame {

    private static final long serialVersionUID = 1L;

    public EditableListExample() {
        super("Editable List Example");
        String[] data = {"a", "b", "c", "d", "e", "f", "g"};
        JList list = new JList(data);
        JScrollPane scrollList = new JScrollPane(list);
        scrollList.setMinimumSize(new Dimension(100, 80));
        Box listBox = new Box(BoxLayout.Y_AXIS);
        listBox.add(scrollList);
        listBox.add(new JLabel("JList"));
        DefaultTableModel dm = new DefaultTableModel();
        Vector<String> dummyHeader = new Vector<String>();
        dummyHeader.addElement("");
        dm.setDataVector(strArray2Vector(data), dummyHeader);
        JTable table = new JTable(dm);
        table.setShowGrid(false);
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        JScrollPane scrollTable = new JScrollPane(table);
        scrollTable.setColumnHeader(null);
        scrollTable.setMinimumSize(new Dimension(100, 80));
        Box tableBox = new Box(BoxLayout.Y_AXIS);
        tableBox.add(scrollTable);
        tableBox.add(new JLabel("JTable"));
        Container c = getContentPane();
        c.setLayout(new BoxLayout(c, BoxLayout.X_AXIS));
        c.add(listBox);
        c.add(new JSeparator(SwingConstants.VERTICAL));
        //c.add(new JLabel("test"));
        //c.add(new JSeparator(SwingConstants.HORIZONTAL));
        c.add(tableBox);
        pack();
        setVisible(true);
    }

    private Vector<Object> strArray2Vector(String[] str) {
        Vector<Object> vector = new Vector<Object>();
        for (int i = 0; i < str.length; i++) {
            Vector<Object> v = new Vector<Object>();
            v.addElement(str[i]);
            vector.addElement(v);
        }
        return vector;
    }

    public static void main(String[] args) {
        final EditableListExample frame = new EditableListExample();
        frame.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}