2017-06-05 79 views
-1

在我的代码中,我将JPanel(Bestellpanel)从frame转换为frame1。之后,每次我使用frame1滚动条重新绘制frame1和我的JPanel(Bestellpanel)不见了。这意味着我需要一种方法来阻止我的JPanel被覆盖。我读了一些关于super.paint();和其他方法,但我有重大问题了解他们。滚动时JPanels正在消失

这里是我的问题的代码示例:

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(); 
    } 
    }); 
    } 

    } 
+0

也许你想[修正你的缩进](https://stackoverflow.com/posts/44369357/edit),让你的代码更易于阅读。 – khelwood

回答

2
  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 s,试试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)演示您的问题,有应当正确地缩进没有外部的依赖或自定义诸如背景颜色/图像等如上面评论所述。

+0

感谢您的帮助Frakcool。所以这意味着当我使用滚动条时,由于JDialog和Panel1 Size配置,我的面板会消失? – Jennifer96

+0

我没有这么说,我说他们是*的一部分。我们无法看到代码中的错误,因为它没有完成。请看看[mcve]页面并发布一个。 'Bestellpanel'我们不知道这堂课的外观,创建一个全新的程序,如果不需要的话,创建一个简单到足以显示你的问题而不需要自定义类的新程序,但这个程序足够短,可以在这里发布。请注意,我们不希望您的整个程序或代码片段,我们想要一个可运行的示例,显示您的问题 – Frakcool

+0

Nevermind我已经看到'Bestellpanel'是'JPanel',请参阅?我很困惑,这就是为什么你需要正确缩进你的代码*和*遵循命名约定 – Frakcool