将按钮组件添加到java中的java选项卡式窗格

我正在尝试使用java swings创建GUI。我只是java swings的初学者。 我的主要想法是创建两个选项卡,并在其中一个选项卡中添加一个按钮。

我想为每个选项卡编写一个单独的类,所以我创建了3个类,其中一个有主方法。另外两个代表选项卡。

在其中一个选项卡中,我想在中间添加一个按钮,并为该按钮添加一个动作侦听器。

下面是具有主要方法的类。

public class abc { JFrame frame; JTabbedPane tabPane; ImageIcon close; Dimension size; int tabCounter = 0; abc_export exp; abc_import imp; public static void main(String[] args) { abc jtab = new abc(); jtab.start(); } public void start(){ exp=new abc_export(); imp=new abc_import(); tabPane.addTab(null, exp.panel); tabPane.addTab(null, imp.panel); tabPane.setTabComponentAt(tabPane.getTabCount()-1, exp.tab); tabPane.setTabComponentAt(tabPane.getTabCount()-1, imp.tab); } public abc() { // Create a frame frame = new JFrame(); // Create the tabbed pane. tabPane = new JTabbedPane(); // Create a button to add a tab // Create an image icon to use as a close button close = new ImageIcon("C:/JAVAJAZZUP/tabClose.gif"); size = new Dimension(close.getIconWidth()+1, close.getIconHeight()+1); //Adding into frame frame.add(tabPane, BorderLayout.CENTER); frame.setSize(300, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }; 

下面是其中一个选项卡的代码。尽管其他选项卡也具有相同的代码,代表具有不同类名的其他选项卡。

 public class abc_import { ImageIcon close; Dimension size; int tabCounter = 0; JPanel tab; final JPanel panel; public abc_import() { close = new ImageIcon("C:/JAVAJAZZUP/tabClose.gif"); size = new Dimension(close.getIconWidth()+1, close.getIconHeight()+1); //Adding into frame JLabel label = null; panel = new JPanel(); // Create a panel to represent the tab tab = new JPanel(); tab.setOpaque(false); String str = "abc_import"; label = new JLabel(str); tab.add(label, BorderLayout.WEST); } }; 

正如预期的那样,两个选项卡都已创建。但我没有在其中一个选项卡中添加按钮的想法。

现在我的问题是,如果我想在其中一个标签中添加一个按钮,就像我已经说过的那样。我需要做什么?有人可以帮助我吗?

我不确定我是否理解您的意图,但您可以尝试使用TabComponentsDemo显示的方法,在如何使用选项卡式窗格:使用自定义组件的选项卡中进行了讨论。

这里显示了一个相关的例子。

您可以尝试使用setTabComponentAt方法。

这个methd有参数setTabComponentAt(int index, Component component) ,你可以在其中提到你想要的组件。

你可以在这里引用链接。