JFrames互相绘画

时间:2014-11-13 02:58:28

标签: java swing user-interface

我正在使用java中的JFrame来制作GUI。我遇到了一个问题,即我的2个JFrame正在相互绘画。

public VidbergGUI() throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
    super("Automatic Output Verifier");
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    setBounds(100, 100, 600, 400);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container con = this.getContentPane();
    con.add(titlePane);

    titlePane.setLayout(new BoxLayout(titlePane, BoxLayout.PAGE_AXIS));
    componentPane.setLayout(new BoxLayout(componentPane, BoxLayout.LINE_AXIS));

    programsLoaded = new JTable(data, columnNames) {
        @Override
        public boolean isCellEditable(int row, int col) {
            if (col == 3) return false;
            return true;
        }
    };
    programsLoaded.getColumnModel().getColumn(2).setCellEditor(new FileChooserEditor());

    tableHolder = new JScrollPane(programsLoaded);

    titleLabel.setFont(new Font("Ariel", Font.BOLD, 28));

    addButton.setSize(40, 40);
    removeButton.setSize(40, 40);

    titlePane.add(titleLabel, BorderLayout.PAGE_START);
    con.add(componentPane);
    componentPane.add(tableHolder, BorderLayout.LINE_END);
    componentPane.add(addButton, BorderLayout.EAST);
    componentPane.add(removeButton, BorderLayout.EAST);
    setVisible(true); // make frame visible
}

使用此设置,只有componentPane可见。如果我注释掉con.add(componentPane),则只显示titlePane。有没有办法可以分配某种布局,以便2帧垂直叠加?

1 个答案:

答案 0 :(得分:3)

您确实理解BorderLayout只能在其中的每个可用插槽中布置单个组件吗?可能不是......

创建第三个JPanel并向其添加titlePanecomponentPane,然后将其添加到CENTER

BorderLayout位置

您可以在此面板中使用GridLayoutGridBagLayout ...

JPanel centerPane = new JPanel(new GridLayout(2, 1));
centerPane.add(titlePane);
centerPane.add(componentPane);
con.add(centerPane);

或者只需将titlePane添加到NORTH位置...

con.add(titlePane, BorderLayout.NORTH);