如何在按下按钮时从不同的类添加Panel

时间:2016-07-21 04:24:18

标签: java swing jpanel

我想通过按下“menuPan”类中的“drop”按钮来添加“newWork”类的面板。 我不能添加面板。 简单地说如何在按下按钮时从不同的类添加Panel。 这是三个不同的类。 MainClass: -

public class userFrame  extends JFrame{

 public void Frame() 
    {
    setTitle("TEST CASE");
    setSize(900,670);

    add(new MenuPan(),BorderLayout.NORTH);
    add(new WorkPan(),BorderLayout.CENTER);


    setLocationRelativeTo(this);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}
public static void main(String [] args){
    userFrame u =new userFrame();
    u.Frame();
}
}

MenuPan

public class MenuPan  extends JPanel implements ActionListener{
WorkPan work=new WorkPan();
JButton view;
public menuPan() {
    setBackground(Color.white);
    setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
    setLayout(new FlowLayout(1, 15, 10));
    view=new JButton(" Registered Courses ");
    view.addActionListener(this);
    add(view);
}
@Override
public void actionPerformed(ActionEvent e) {
    work.TaskPannel();
}
}

WorkPAN课程: -

 class WorkPan extends JPanel{
   JPanel work=new JPanel();
   public WorkPan() {
     setBackground(Color.LIGHT_GRAY);
     setLayout(new BorderLayout(40, 50));

   }

   void TaskPannel() {
      System.out.println("here");
      add(new NewWork(),BorderLayout.CENTER);// adds NewWork panel 
   }
}

NewWork Class

  class NewWork extends JPanel{

  public NewWork(){

     setBackground(Color.red);       

  }
  }

1 个答案:

答案 0 :(得分:2)

一个问题 - 您创建一个workPan(应该重命名为WorkPan),在ActionListener中更改其状态,但从不将其添加到GUI。因此,您似乎正在更改未显示的GUI组件的状态,因此在GUI中不显示任何内容是有意义的。

建议:

  • 请务必仅创建一个 WorkPan参考
  • 请务必在GUI中显示此单个参考
  • 确保您的ActionListener在相同参考上调用相应的方法。

侧面推荐:

  • 学习并遵循Java命名约定,以便其他人可以更轻松地理解和遵循您的代码。
  • 要在GUI中交换JPanel,我强烈建议您使用CardLayout,而不是像当前一样手动添加和删除组件。请查看CardLayout Tutorial

我的解决方案可以工作,但您还必须调用revalidate并重新绘制以获取GUI来布局新组件并重新绘制它。请注意\\ !!评论

标记的添加和更改
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class FooWork {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            userFrame.main(args);
        });
    }
}

class NewWork extends JPanel {
    public NewWork() {
        setBackground(Color.red);
    }
}

class WorkPan extends JPanel {
    JPanel work = new JPanel();

    public WorkPan() {
        setBackground(Color.LIGHT_GRAY);
        setLayout(new BorderLayout(40, 50));
    }

    void TaskPannel() {
        System.out.println("here");
        add(new NewWork(), BorderLayout.CENTER);// adds NewWork panel

        // !!
        revalidate();
        repaint();
        // !!
    }
}

class MenuPan extends JPanel implements ActionListener {
    // !! WorkPan work = new WorkPan();
    WorkPan work; // !!
    JButton view; // !!

    // !!
    public MenuPan(WorkPan workPan) { // references are key

        // !!
        this.work = workPan; // set the reference!
        // !!

        setBackground(Color.white);
        setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
        setLayout(new FlowLayout(1, 15, 10));
        view = new JButton(" Registered Courses ");
        view.addActionListener(this);
        add(view);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        work.TaskPannel();
    }
}

class userFrame extends JFrame {
    public void Frame() {
        setTitle("TEST CASE");
        setSize(900, 670);

        // !!
        WorkPan workPan = new WorkPan();
        MenuPan menuPan = new MenuPan(workPan);
        // !!

        // !!
        // add(new MenuPan(), BorderLayout.NORTH);
        // add(new WorkPan(), BorderLayout.CENTER);
        add(menuPan, BorderLayout.NORTH);
        add(workPan, BorderLayout.CENTER);
        // !!

        setLocationRelativeTo(this);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        userFrame u = new userFrame();
        u.Frame();
    }
}

但同样,更清洁的是使用CardLayout来帮助进行交换:

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.*;

public class SwapStuff {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }

    private static void createAndShowGui() {
        SwapMainPanel mainPanel = new SwapMainPanel();
        JFrame frame = new JFrame("SwapStuff");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }
}

class SwapMainPanel extends JPanel {
    private CardLayout cardLayout = new CardLayout();
    private JPanel cardPanel = new JPanel(cardLayout);
    private ButtonPanel buttonPanel = new ButtonPanel(this); // pass the reference
    private WorkPanel workPanel = new WorkPanel();
    private ViewPanel viewPanel = new ViewPanel();

    public SwapMainPanel() {
        cardPanel.add(workPanel, workPanel.getClass().getName());
        cardPanel.add(viewPanel, viewPanel.getClass().getName());

        setLayout(new BorderLayout());
        add(buttonPanel, BorderLayout.PAGE_START);
        add(cardPanel, BorderLayout.CENTER);
    }

    // one possible way to swap "cards"
    public void nextCard() {
        cardLayout.next(cardPanel);
    }
}

class ButtonPanel extends JPanel {
    private SwapMainPanel mainPanel;

    public ButtonPanel(SwapMainPanel mainPanel) {
        this.mainPanel = mainPanel;  // set the reference!
        add(new JButton(new SwapAction("Swap Panels", KeyEvent.VK_S)));
    }

    private class SwapAction extends AbstractAction {
        public SwapAction(String name, int mnemonic) {
            super(name);
            putValue(MNEMONIC_KEY, mnemonic);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            mainPanel.nextCard();
        }
    }
}

class WorkPanel extends JPanel {
    public WorkPanel() {
        setBorder(BorderFactory.createTitledBorder("Work Panel"));
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(500, 400);
    }
}

class ViewPanel extends JPanel {
    public ViewPanel() {
        setBorder(BorderFactory.createTitledBorder("View Panel"));
        setBackground(Color.RED);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(500, 400);
    }
}