默认做出来的JTabbedPane是这样的,现在我想让最上面的tab显示在左边,并且有一个“+”按钮,点击这个按钮就自动
生成一个新的标签页出来,就像浏览器标签页那个效果一样,各位大神能给个思路么?
------解决方案--------------------
- Java code
import java.awt.Dimension;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import javax.swing.BorderFactory;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JTabbedPane;public class TabPane { private JPanel pane1,pane2,pane3; private JTabbedPane tabbedPane; private int count; TabPane(){ JFrame jf=new JFrame(); jf.setSize(520,500); Dimension screen = jf.getToolkit().getScreenSize();//获取屏幕大小 jf.setLocation(screen.width/2-jf.getSize().width/2,screen.height/2-jf.getSize().height/2);//设置程序居中显示 tabbedPane = new JTabbedPane(JTabbedPane.TOP);//切换面边标题在上面 pane1=new JPanel(); pane2=new JPanel(); pane3=new JPanel(); count=3; tabbedPane.addTab("1",null,pane1,"第1个Tab");//加入面板,实现多面板切换 tabbedPane.addTab("2",null,pane2,"第2个Tab"); tabbedPane.addTab("3",null,pane3,"第3个Tab"); tabbedPane.addTab("+",null,null,"添加新面板"); tabbedPane.addMouseListener(new MouseAdapter() {//选项卡点击事件 public void mouseClicked(MouseEvent arg0) { addtab();//调用addtab方法 } }); tabbedPane.setBorder(BorderFactory.createEmptyBorder(35,0,0,0));//设置tabPane与周边的距离 jf.add(tabbedPane); jf.setVisible(true); } public void addtab(){ if(tabbedPane.getSelectedComponent()==null){ JPanel newpane=new JPanel();//新的面板 count++; tabbedPane.removeTabAt(tabbedPane.getTabCount()-1); tabbedPane.addTab(count+"",null,newpane,"第"+count+"个Tab"); tabbedPane.addTab("+",null,null,"添加新面板"); } } public static void main(String[] args){ new TabPane(); }}