[英]How do i remove contents of a JTabbedPane after a menu item action
我正在使考试软件具有表格1,表格2和表格3的列表,并且旁边是带有选项卡术语1,术语2,术语3的jtabbedpane。
String[] lists={"Form 1","Form 2","Form 3,"Form 4"};
JList forms=new JList(lists);
JTabbedPane tabs=new JTabbedPane();
//add lists to frame
//add component to tabbedpane("term1","term 2',"term 3")
//i created acionlistener for list value changed
现在,我的jlist中的所有项目的term1,2和3都有不同的值,如何更改选项卡窗格中面板的内容? 我努力了
tabs.removeAll();
tabs.add(term1);
tabs.validate();
但它不起作用
我认为,基本上,例如,您想使用JTabbedPane#add(String, Component)
方法
tabs.add("Term 1", term1);
以防万一,这是我用来测试我的想法的代码...
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestTabbedPane100 {
public static void main(String[] args) {
new TestTabbedPane100();
}
public TestTabbedPane100() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JTabbedPane tabbedPane;
private int index;
public TestPane() {
setLayout(new BorderLayout());
tabbedPane = new JTabbedPane();
addTabs();
add(tabbedPane);
JButton btn = new JButton("Update");
add(btn, BorderLayout.SOUTH);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
tabbedPane.removeAll();
addTabs();
}
});
}
protected void addTabs() {
for (int count = 0; count < 10; count++, index++) {
tabbedPane.add("Testing" + index, new JLabel("Testing" + index));
}
}
}
}
要添加标签,请使用addTab(...)
方法addTab(...)
。 要删除,请使用removeTabAt(int i)
。
阅读JTabbedPane
教程
下一个示例添加一个新选项卡/删除选中的/全部删除。 我认为它可以帮助您:
public class Form extends JFrame {
private static JTabbedPane tabs;
private static int counter;
public static void main(String[] args) {
final JFrame frame = new JFrame("Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton add = new JButton("add");
add.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
JLabel l = new JLabel(++counter+"");
tabs.addTab("New Tab "+counter, l);
}
});
JButton remove = new JButton("remove");
remove.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
int selectedIndex = tabs.getSelectedIndex();
if(selectedIndex != -1){
tabs.removeTabAt(selectedIndex);
}
}
});
JButton removeAll = new JButton("remove all");
removeAll.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
while (tabs.getTabCount() > 0){
tabs.removeTabAt(0);
}
}
});
JPanel north = new JPanel();
north.add(add);
north.add(remove);
north.add(removeAll);
frame.getContentPane().add(north,BorderLayout.NORTH);
tabs = new JTabbedPane();
tabs.setPreferredSize(new Dimension(200,200));
frame.add(new JScrollPane(tabs),BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.