我有 2 个类,mainFrame 和面板。通过单击 mainFrame 上的按钮,我从另一个类调用面板并将其设置在 JFrame(mainFrame 类)中的选项卡式窗格中。现在,我在面板类的面板上有另一个按钮 (btnRemove)。因此,当我单击该按钮时,我想从 mainFrame 类的选项卡式窗格中删除我的面板。我如何正确地写我的听众?
主框架类:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class MainFrame extends JFrame {
JTabbedPane tPane = new JTabbedPane();
JButton btn = new JButton("Add panel");
public MainFrame(){
setSize(400,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setLayout(new BorderLayout());
add(tPane, BorderLayout.CENTER);
add(btn,BorderLayout.NORTH);
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
panel p = new panel();
tPane.add("Panel",p);
}
});
}
public static void main(String[] args){
new MainFrame();
}
}
面板类:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Panel extends JPanel{
JButton btnRemove = new JButton("Remove panel");
public Panel(){
setLayout(new FlowLayout());
add(btnRemove);
btnRemove.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
}
});
}
}