2015-09-07 53 views
0

我想在選擇特定選項卡時使JFrame顯示不同的JPanel。我已經嘗試添加代碼,使其基於選擇哪個選項卡索引添加新面板。當選擇JTabbedPane選項卡時更改JFrame顯示

我在哪裏出錯了?我需要添加什麼才能使其工作?謝謝。

編輯

這裏是我的解決SSCCE:

import javax.swing.*; 
import java.awt.*; 
import java.awt.geom.Ellipse2D; 
import java.awt.geom.Rectangle2D; 
import java.util.ArrayList; 

public class MainPanel { 

    private static JTabbedPane tabbedPane = new JTabbedPane(); 
    private static JFrame frame = new JFrame("Testing"); 

    public static void main(String[] args) { 

     EventQueue.invokeLater(MainPanel::createAndShowGUI); 
    } 

    protected static void createAndShowGUI() 
    { 
     DrawGraphics drawGraphics = new DrawGraphics(); 
     DrawDifferentGraphics drawDifferentGraphics = new DrawDifferentGraphics(); 
     frame.setLayout(new BorderLayout()); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.add(tabbedPane, BorderLayout.WEST); 

     tabbedPane.addTab("CFG", null); 
     tabbedPane.addTab("CNX", null); 

     frame.add(drawGraphics); 

     tabbedPane.addChangeListener(e -> { 

      if (tabbedPane.getSelectedIndex() == 0) { 

       frame.remove(drawDifferentGraphics); 
       frame.add(drawGraphics); 
       frame.validate(); 
       frame.repaint(); 

      } 

      if (tabbedPane.getSelectedIndex() == 1) { 

       frame.remove(drawGraphics); 
       frame.add(drawDifferentGraphics); 
       frame.validate(); 
       frame.repaint(); 

      }}); 


     frame.setLocationByPlatform(true); 
     frame.setSize(400, 400); 
     frame.setVisible(true); 
    } 
} 

class DrawGraphics extends JPanel { 

    private ArrayList<Shape> shapes = new ArrayList<Shape>(); 

    public DrawGraphics() { 

     setLayout(new BorderLayout()); 

     shapes.add(new Ellipse2D.Double(10, 10, 20, 20)); 
     shapes.add(new Ellipse2D.Double(10, 30, 20, 20)); 
     shapes.add(new Ellipse2D.Double(10, 50, 20, 20)); 
     shapes.add(new Ellipse2D.Double(10, 70, 20, 20)); 
    } 

    @Override 
    protected void paintComponent(Graphics g) 
    { 
     super.paintComponent(g); 

     Graphics2D g2d = (Graphics2D)g.create(); 
     g2d.setColor(Color.BLUE); 

     shapes.forEach(g2d::fill); 

     g2d.dispose(); 
    } 
} 

class DrawDifferentGraphics extends JPanel { 

    private ArrayList<Shape> shapes = new ArrayList<Shape>(); 

    public DrawDifferentGraphics() { 

     setLayout(new BorderLayout()); 

     shapes.add(new Rectangle2D.Double(10, 10, 10, 10)); 
     shapes.add(new Rectangle2D.Double(10, 30, 10, 10)); 
     shapes.add(new Rectangle2D.Double(10, 50, 10, 10)); 
     shapes.add(new Rectangle2D.Double(10, 70, 10, 10)); 
    } 

    @Override 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 

     Graphics2D g2d = (Graphics2D)g.create(); 
     g2d.setColor(Color.RED); 

     shapes.forEach(g2d::fill); 

     g2d.dispose(); 
    } 
} 

回答

2

我想在面板上顯示旁邊的選項卡窗格的圖形。

請參閱How to Write a ChangeListener的Swing教程中的部分。

當標籤被點擊時,您會收到通知。然後獲取選定的選項卡並將面板添加到框架。

所以基本上你的if (tabbedPane.getSelectedIndex() == 0)邏輯將被轉移到ChangeListener。或者不是有一堆「if語句」,你可以有一個Integer/JPanel值的Map。然後,您只需獲取索引並從地圖獲取面板。

將面板添加到框架後,您需要重新驗證()並重新繪製框架。

編輯:

其實上述建議並不完整。你不能只是在面板上添加面板。 BorderLayout的CENTER區域應該只包含一個組件,否則可能會出現繪畫問題。

這可以通過單擊未選定的選項卡,然後調整框架來演示。原始面板將顯示。

你需要做以下之一:

  1. 使用CardLayout(閱讀教程,如果你以前沒有使用過佈局)關於BordreLayout的中心penel。所以在這種情況下,使用CardLayout的面板是CENTER中唯一的組件,然後它管理CardLayout中顯示的面板。所以你的ChangeListener需要識別要顯示的卡。您可以將卡標識符設置爲選定選項卡的文本。所以

  2. 在添加新面板之前刪除當前面板。在這種情況下,CENTER中只有一個面板,所以繪畫和預期一樣。

+0

謝謝。我會試試這個。 – feltersnach

+0

這很好。請參閱編輯。 – feltersnach

+0

@feltersnach,其實我不確定代碼是如何工作的。你錯過了我關於revalidate()和repaint()的觀點。查看我的關於代碼問題的編輯。 – camickr

相關問題