添加一个按钮组件到Java在Java标签面板 [英] Adding a button component to a java tabbed pane in java

查看:730
本文介绍了添加一个按钮组件到Java在Java标签面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建使用Java GUI swings.I我只是在Java波动初学者。
我的主要想法是创建两个标签,并在其中一个选项卡中添加一个按钮。

i am trying to create GUI using java swings.I am just a beginner in java swings. My primary idea was to create two tabs and add a button in one of the tabs.

我想写一个单独的类为每个标签,所以我创建了3个班外面一个人的主要method.and另外两个重present的标签。

I wanted to write a separate class for each tab so i created 3 classes out of which one has the main method.and the other two represent the tabs.

在一个选项卡我想在中间添加一个按钮并添加动作监听该按钮。

In one of the tabs i wanted to add a button in middle and add an action listener to that button.

下面是一个具有main方法的类。

below is the class which has the main method.

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);
        }

};

下面是code为其他选项卡也具有相同code从而重新present其他标签不同的类名tabs.although之一。

below is code for one of the tabs.although the other tab is also having the same code which represent other tab with different class name.

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);
        }
};

如预期这两个选项卡created.But我出的想法有关添加里面的标签之一的按钮。

as expected both the tabs are created.But i am out of ideas about adding a button inside one of the tabs.

现在我在这里的问题是,如果我想在一个选项卡中添加一个按钮,因为我已经做said.what我需要做什么?任何人都可以帮我吗?

Now my question here is if i wanted to add a button in one of the tabs as i already said.what do i need to do?can anyone help me?

推荐答案

我不知道我理解你的意图,但你可以尝试在的 TabComponentsDemo 中的 如何使用标签式窗格:卡口与自定义组件

I'm not sure I understand your intent, but you might try the approach shown in the TabComponentsDemo, discussed in How to Use Tabbed Panes: Tabs With Custom Components.

一个相关的例子是这里显示

A related example is shown here.

这篇关于添加一个按钮组件到Java在Java标签面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆