有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java想让图形显示在面板下面

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

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) 个答案

  1. # 1 楼答案

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

    自定义绘制是通过重写JPanel上的paintComponent()来完成的。阅读Swing教程中关于Custom Painting的部分

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

    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的部分