想要让身材在面板下方

时间:2019-06-12 08:01:20

标签: java swing layout

我必须制作一个绘图工具,这是我的问题

enter image description here

Screenshot of another case the issue happens

因此,当图形进入另一个面板时,具有按钮的面板和具有参数的面板,我希望图形位于这些面板下方

JPanel panel_contenu = new JPanel();
panel_contenu.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(panel_contenu);
panel_contenu.setLayout(null);

JPanel panel_dessin = new JPanel();
panel_dessin.setBounds(0, 139, 1257, 831);
panel_contenu.add(panel_dessin);
panel_dessin.setOpaque(true);

JPanel panel_parametre = new JPanel();
panel_parametre.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
panel_parametre.setBounds(1264, 0, 247, 977);
panel_contenu.add(panel_parametre);
panel_parametre.setLayout(null);

JPanel panel_boutons = new JPanel();
panel_boutons.setBounds(0, 0, 1264, 101);
panel_contenu.add(panel_boutons);
panel_boutons.setLayout(null);

if (cbo_listeobj.getSelectedItem() instanceof Triangle) {
    Triangle t = new Triangle();

    t = (Triangle) cbo_listeobj.getSelectedItem();

    t.deplacer(val_vect_x, val_vect_y);

    repaint();
} // this is when i move a figure

public void paint(Graphics g) {
    super.paintComponents(g);
    Point2D p1 = new Point2D(val_p1_x, val_p1_y);// Commun à toutes les figures

    // TRIANGLE
    if (Btriangle2) {
        Point2D p2 = new Point2D(val_p2_x, val_p2_y);
        Point2D p3 = new Point2D(val_p3_x, val_p3_y);

        Triangle t = new Triangle(p1, p2, p3);
        colltri.add(t);

        listeobj.add(t, cbo_listeobj);

        Btriangle2 = false;
    }
    colltri.afficher(g); //this is when it paint the contentPane

    btnOk.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
        edt_vect_x.setEditable(true);
        edt_vect_y.setEditable(true);
        btnDepl.setEnabled(true);

        if (Btriangle) {
            Btriangle2 = true;
            Btriangle = false;

            val_p1_x = Integer.parseInt(edt_p1_x.getText());
            val_p1_y = Integer.parseInt(edt_p1_y.getText());

            val_p2_x = Integer.parseInt(edt_p2_x.getText());
            val_p2_y = Integer.parseInt(edt_p2_y.getText());

            val_p3_x = Integer.parseInt(edt_p3_x.getText());
            val_p3_y = Integer.parseInt(edt_p3_y.getText());

            repaint();
        } // the ok Button display the figures 

所以我的主要问题是,当您显示或移动图形时,图形通过其他面板,而不是在其他面板下方,您会看到图形通过面板,如屏幕截图所示。

我希望图形在显示时以及在使用参数移动图形时位于其他面板下方。我有4个面板 panel_contenu是contentPane,其中包括其他面板 panel_dessinpanel_parametrepanel_boutons

1 个答案:

答案 0 :(得分:0)

  1. 请勿使用空布局(Swing设计用于布局管理器)
  2. 不要使用setBounds()(这是布局负责人设置组件的大小/位置的工作。
  3. 不要覆盖paint()
  4. 不要直接调用paintComponents()(Swing会这样做)。

通过覆盖JPanel上的paintComponent()来完成自定义绘制。阅读关于Custom Painting

的Swing教程中的部分

然后将不同的面板添加到框架:

  1. 一个用于工具栏
  2. 一个用于定制绘画

所以基本逻辑是:

JPanel toolBarPanel = new CustomToolBarPanel();
JPanel paintingPanel = new CustomPaintingPanel();
frame.add(toolBarPanel, BorderLayout.PAGE_START)
frame.add(paintingPanel, BurderLayout.CENTER);

上面的教程链接还包含How to Use BorderLayout的一部分。

相关问题