[英]Java: “Add Tab Button” for a JTabbedPane
Is it possible to add a button to a tabbed pane like in firefox. 是否可以像在Firefox中一样向选项卡式窗格添加按钮。
The plus-button is what I want. 加号按钮是我想要的。
Thanks 谢谢
I think you should be able to manage it by building your own JTabbedPaneUI
and setting it on the JTabbedPane
using setUI
. 我认为你应该能够通过构建自己的
JTabbedPaneUI
并使用setUI
在JTabbedPane
上设置它来管理它。
Your ComponentUI
has methods to get a hold of the accessible children. 您的
ComponentUI
具有获取可访问子级的方法。 If you specify a JButton
and a JLabel
then you may be in business. 如果您指定
JButton
和JLabel
那么您可能正在开展业务。
I haven't attempted this myself though. 我自己并没有尝试过这个。 This is "at your own risk" :)
这是“你自担风险”:)
You can try this: 你可以试试这个:
public static void main (String[] args) {
JFrame parent = new JFrame ();
final JTabbedPane pane = new JTabbedPane ();
pane.addTab ("test", null);
FlowLayout f = new FlowLayout (FlowLayout.CENTER, 5, 0);
// Make a small JPanel with the layout and make it non-opaque
JPanel pnlTab = new JPanel (f);
pnlTab.setOpaque (false);
// Create a JButton for adding the tabs
JButton addTab = new JButton ("+");
addTab.setOpaque (false); //
addTab.setBorder (null);
addTab.setContentAreaFilled (false);
addTab.setFocusPainted (false);
addTab.setFocusable (false);
pnlTab.add (addTab);
pane.setTabComponentAt (pane.getTabCount () - 1, pnlTab);
ActionListener listener = new ActionListener () {
@Override
public void actionPerformed (ActionEvent e) {
String title = "Tab " + String.valueOf (pane.getTabCount () - 1);
pane.addTab (title, new JLabel (title));
}
};
addTab.setFocusable (false);
addTab.addActionListener (listener);
pane.setVisible (true);
parent.add (pane);
parent.setSize (new Dimension (400, 200));
parent.setVisible (true);
}
I have tried several solutions and came with this one: 我尝试了几种解决方案并附带了这个解决方案:
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
public class TestTab {
public static void main(String[] args) {
JFrame parent = new JFrame();
final JTabbedPane tabEntity = new JTabbedPane();
tabEntity.addTab("Details", null, new JScrollPane());
tabEntity.addTab("Context", null, new JScrollPane());
tabEntity.addTab("", null, new JScrollPane());
addButtonToTab(tabEntity);
parent.add(tabEntity);
parent.setSize(new Dimension(400, 200));
parent.setVisible(true);
}
public static void addButtonToTab(final JTabbedPane tabEntity) {
tabEntity.setTabComponentAt(tabEntity.getTabCount() - 1, new JButton(
"+"));
}
}
So you have: 所以你有了:
Write Following Code in Default Constructor Of Class 在类的默认构造函数中编写以下代码
JPanel panel = new JPanel();
tabbedPane.addTab("Welcome", null, panel, null);
tabbedPane.addTab(" + ", null, panel1, null);
tabbedPane.addChangeListener(new ChangeListener()
{
public void stateChanged(ChangeEvent evt)
{
JTabbedPane tabbedPane = (JTabbedPane)evt.getSource();
if(tabbedPane.getSelectedIndex() == tabbedPane.indexOfTab(" + "))
{
createTab();
}
}
});
And Create Method to declare and initialized int tab2 = 2;
并创建方法来声明和初始化
int tab2 = 2;
at Starting of main class. 在主要课程的开始。 Its Worked.
它的工作。
private void createTab()
{
tabbedPane.addTab("New Tab",new Panel());
tabbedPane.addTab(" + ",null,panel1,null);
tabbedPane.setSelectedIndex(tab2);
tab2++;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.