2017-06-05 74 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