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 单击按钮时出现新的 Panel() 并消失现有的面板。但是,我不能让 Panel() 改变。单击按钮时没有任何反应,这些按钮设置为在当前面板上将 setVisible 更改为 false,在请求的面板上更改为 true。

我相当肯定这是因为面板在不同的类(initComponents)中设置为私有。如何访问面板上的 setVisible 属性?或者,这到底是怎么回事?

4

1 回答 1

1

添加

this.validate();

在 makeActiveVisible() 方法中。

Swing 组件的默认状态为无效,除非经过验证(通过在组件本身或其中一个父容器上调用 .validate() 方法),否则不会将其绘制到屏幕上。

也可以看看

Java中的重绘()

于 2013-08-03T22:50:11.120 回答