2013-08-03 21 views
0
package com.spiralfive.inventory; 

import java.awt.Font; 
import java.awt.Panel; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 
import java.awt.BorderLayout; 

import javax.swing.JApplet; 
import javax.swing.JButton; 

public class MainFile extends JApplet implements MouseListener { 

// Declare the initial components 
private JButton btnNewInventory; 
private JButton btnMarkDown; 
private JButton btnProcessSales; 
private JButton btnActive; 
private JButton btnStats; 
private Panel mainPanel; 
private Panel menuPanel; 
private Panel inventoryPanel; 
private Panel setactivePanel; 

// Call the Initializer... 
public void init() { 
    initComponents(); 
} 

// Initialize the Main Components - Mostly Menu Items 
private void initComponents() { 

    // Main Form Components 
    mainPanel = new Panel(); 
    menuPanel = new Panel(); 
    inventoryPanel = new Panel(); 
    setactivePanel = new Panel(); 
    btnNewInventory = new JButton("New Inventory"); 
    btnActive = new JButton("Set Active"); 
    btnMarkDown = new JButton("Mark Down"); 
    btnProcessSales = new JButton("Process Sale"); 
    btnStats = new JButton("Statistics"); 

    // Enable Mouse Interactivity 
    btnNewInventory.addMouseListener(this); 
    btnMarkDown.addMouseListener(this); 
    btnProcessSales.addMouseListener(this); 
    btnActive.addMouseListener(this); 
    btnStats.addMouseListener(this); 

    // Set the Fonts 
    Font buttonFont = new Font("Arial", Font.PLAIN, 20); 

    // Apply Fonts To Menu 
    btnNewInventory.setFont(buttonFont); 
    btnMarkDown.setFont(buttonFont); 
    btnProcessSales.setFont(buttonFont); 
    btnActive.setFont(buttonFont); 
    btnStats.setFont(buttonFont); 

    // Set Panel Layout For Menu Items 
    menuPanel.setLayout(new java.awt.GridLayout(1,6)); 

    // Add the Components 
    menuPanel.add(btnNewInventory); 
    menuPanel.add(btnActive); 
    menuPanel.add(btnMarkDown); 
    menuPanel.add(btnProcessSales); 
    menuPanel.add(btnStats); 

    // Add The Menu To the Main Panel 
    mainPanel.add(BorderLayout.NORTH, menuPanel); 

    // Make Inventory Load by Default 
    createPanels(); 
    makeInvVisible(); 

    // Set It All Visible 
    add(mainPanel); 

} 

// Create the Panels Used in this applet 
public void createPanels() { 
    NewInventory inventoryPanel = new NewInventory(); 
    inventoryPanel.setLayout(new java.awt.GridLayout(6,2)); 
    mainPanel.add(BorderLayout.CENTER, inventoryPanel); 

    SetActive setactivePanel = new SetActive(); 
    setactivePanel.setLayout(new java.awt.GridLayout(6,2)); 
    mainPanel.add(BorderLayout.CENTER, setactivePanel); 

} 

public void makeInvVisible() { 
    inventoryPanel.setVisible(true); 
    mainPanel.repaint(); 
} 

public void makeInvDisappear() { 
    inventoryPanel.setVisible(false); 
    mainPanel.repaint(); 
} 

public void makeActiveVisible() { 
    setactivePanel.setVisible(true); 
    mainPanel.repaint(); 
} 

public void makeActiveDisppear() { 
    setactivePanel.setVisible(false); 
    mainPanel.repaint(); 
} 

public void mouseClicked(MouseEvent e) { 

    // Determine Which Button Has Been Clicked 
    JButton currentButton = (JButton)e.getComponent(); 

    // New Inventory Button 
    if(currentButton == btnNewInventory) { 
     makeInvVisible(); 
     makeActiveDisppear(); 
    } 

    // Set Active Button 
    else if(currentButton == btnActive) { 
     makeActiveVisible(); 
     makeInvDisappear(); 
    } 
} 

public void mousePressed(MouseEvent e) {} 

public void mouseReleased(MouseEvent e) {} 

public void mouseEntered(MouseEvent e) {} 

public void mouseExited(MouseEvent e) {} 

} 

我使用上述試圖使一個新的面板()的代碼出現,當按鈕被通過調用setVisible點擊現有的面板中消失。但是,我無法讓面板()更改。單擊按鈕時不會發生任何事情,這些按鈕會將當前面板上的setVisible更改爲false,並在請求的面板上將其設置爲true。如何更改面板調用setVisible()在不同的Java類

我相當肯定這是因爲面板被設置爲不同類的私人(initComponents)。如何訪問面板上的setVisible屬性?或者,這是什麼問題?

回答

1

在makeActiveVisible()方法添加

this.validate(); 

Swing組件的默認狀態是無效的,除非驗證(通過在組件本身或其中一個父容器上調用.validate()方法),否則不會將其繪製到屏幕上。

參見

repaint() in Java

+2

我想重新驗證將是一個更好的選擇,除此之外,我認爲CardLayout將是一個更好的解決辦法;) – MadProgrammer