有 Java 编程相关的问题?

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

滚动时java JPanel正在消失

在我的代码中,我将JPanel(Bestellpanel)从frame转移到frame1。之后,每次我使用frame1滚动条时,它都会重新绘制frame1,而我的JPanel(Bestellpanel)就不见了。这意味着我需要一种方法来阻止我的JPanel被超额支付。我读了一些关于super.paint();和其他方法的文章,但我在理解它们时遇到了一些重大问题

以下是我的问题的代码示例:

 import java.awt.Color;
 import javax.swing.JPanel;
 import java.awt.Container;
 import java.awt.Dimension;
 import java.awt.Insets;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import javax.swing.JButton;
 import javax.swing.JFrame;
 import javax.swing.JScrollPane;
 import javax.swing.ScrollPaneConstants;


public class weqe {


private static JFrame frame = new JFrame("First Frame");
private static JFrame frame1 = new JFrame("Second Frame");
private static JPanel Bestellpanel = new JPanel();

private static int kunde = 1;


public static void addComponentsToPane(final Container pane) {

    pane.setLayout(null);
    final Insets insets1 = pane.getInsets();    
    // Mitn Button

    JButton MitnIcon = new JButton("Mitnehmen");
            MitnIcon.setFocusPainted(false);
            MitnIcon.setVisible(true); 

    Dimension size2 = MitnIcon.getPreferredSize();
            MitnIcon.setBounds(1010 + insets1.left, 700 + insets1.top,
                    size2.width + 27, size2.height + 50);

    pane.add(MitnIcon);

    MitnIcon.addActionListener(new ActionListener(){

    public void actionPerformed(ActionEvent e) {

            if (kunde == 1) {

                frame.getContentPane().remove(Bestellpanel);
                Bestellpanel.setLocation(0, 0);
                frame1.getContentPane().add(Bestellpanel);  
                Bestellpanel.repaint();
                frame.repaint();


                    }

        }});

    // ScrollPane           

    JPanel panel1 = new JPanel();
    panel1.setPreferredSize(new Dimension(2000,800));
    panel1.setVisible(false);    


    JScrollPane scrollPane = new JScrollPane (panel1,
                ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
     frame1.add(scrollPane);

    Bestellpanel.setBounds(930 + insets1.left, 50 + insets1.top,size2.width 
    + 30, size2.height + 400);    

    Bestellpanel.setVisible(true);pane.add(Bestellpanel);
    Bestellpanel.setBackground(Color.green);     

        }

   private static void createAndShowGUI() {

        //Create and set up the window.

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     

        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     
        addComponentsToPane(frame.getContentPane());



        //Size and display the window.
        Insets insets = frame.getInsets();
        Insets insets1 = frame1.getInsets();
        frame.setSize(1200 + insets.left + insets.right,
                      900 + insets.top + insets.bottom);
        frame.setVisible(true);
        frame1.setSize(800 + insets1.left + insets1.right,
                600 + insets1.top + insets1.bottom);
        frame1.setVisible(true);

        frame.add(Bestellpanel);


    }


    public static void main(String[] args) {

    javax.swing.SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    createAndShowGUI();
    }
    });
    }

    }

共 (1) 个答案

  1. # 1 楼答案

    1. meinJDialog.setSize(800,800);panel1.setPreferredSize(new Dimension(2000,800));很可能是你的问题的一部分,请参见Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?(普遍的共识是肯定的,并替代getPreferred|Maximum|MinimumSize()方法)

    2. 与其自己删除/添加JComponent,不如尝试一下Card Layout

    3. 您不需要手动更改组件的可见性,再次检查第2点中的链接,对于这一行:Bestellpanel2.setVisible(true);

    4. 请遵循Java naming conventionsFirstWordUpperCaseClassfirstWordLowerCaseVariablefirstWordLowerCaseMethod()ALL_WORDS_UPPER_CASE_CONSTANT),这样,您的代码对您和我们来说都更容易阅读和理解

    如果上面所有的点不起作用,那么考虑发布一个有效的Minimal, Complete and Verifiable Example (MCVE)Short, Self Contained, Correct Example (SSCCE)来演示您的问题,没有外部依赖性或自定义,如背景颜色/图像等。它应该正确地缩进,如上面的评论所说的。