JTabbedPane 中使用单个 JPanel 的多个选项卡?
是否可以对 JTabbedPane
中的多个选项卡项使用单个 JPanel
?
EG:
JTabbedPanel tabs=new JTabbePanel();
JPanel panel=new JPanel();
JButton but=new JButton("TEXT");
but.addActionlistener(this);
panel.add(but)
tabs.add("First",panel);
tabs.add("Second",panel);
在 JTabbedPane
中添加一个 ActionListener
以通知程序选项卡更改(将 cur_tab 更改为选项卡编号)
public void actionPerformed(..)
{ System.out.println("Now in "+cur_tab); }
Is it possible to use a single JPanel
for multiple tab items in JTabbedPane
?
EG:
JTabbedPanel tabs=new JTabbePanel();
JPanel panel=new JPanel();
JButton but=new JButton("TEXT");
but.addActionlistener(this);
panel.add(but)
tabs.add("First",panel);
tabs.add("Second",panel);
An ActionListener
is added to the JTabbedPane
to notify the program of tab changes (change cur_tab to tab number)
public void actionPerformed(..)
{ System.out.println("Now in "+cur_tab); }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
此处
taken from here
听起来您需要
JPanel
的子类。只需创建一个处理复杂布局的抽象类,并让子类创建所需的必要 GUI 元素。如果您只是插入一个文本框、按钮等,您甚至可能不需要每个选项卡的子类。只需创建基类的多个实例并添加您需要的组件即可。
新面板和其他 GUI 项目相对便宜。 Swing GUI 中的性能问题更可能来自事件处理或触发过多的事件,而不是其复杂程度或组件数量。让事情更容易维护和理解,然后再担心性能。
Sounds like you need a subclass of
JPanel
. Just create an abstract class that handles the complex layout and have the subclasses create the necessary GUI elements that are required.If you are just inserting a single text box, button, etc, you may not even need subclasses per tab. Just create multiple instances of the base class and add the component you need.
New panels and other GUI items are relatively cheap. Performance issues in a Swing GUI are more likely to come from event handling or firing too many events rather than how complex or how many components it has. Make things easier to maintain and understand, then worry about performance.
如果您将创建返回
JPanel
的类,那么是的,这是可能的,但请注意:
存在两个
Tab
不能包含相同组件架构的长期错误,我的意思是架构例如,一个Tab
包含JPanel + JTextField + JButton
,但第二个JPanel
必须包含另一个JComponent 的数字或类型
>不幸的是(没什么特别的)
BugsDatabase
此时无法访问if you'll create class that returns
JPanel
then yes that's possiblebut Notice:
there are long time Bug that two
Tabs
can't contains same component schemas, with schemas I means for example oneTab
containsJPanel + JTextField + JButton
, but then secondJPanel
must contains anotherNumbers or Type of JComponents
unfortunatelly (nothing special)
BugsDatabase
isn't accesible in this moment我有一个带有内存密集型组件的面板,我只想创建它的一个实例,但使用有吸引力的 JTabbedPane 更改它的行为。
我通过为每个选项卡创建空面板以及包含我的(单个)复杂组件的第三个面板来完成此操作。在 JTabbedPane 的 StateChange 事件中,我从前两个面板中删除第三个面板,并将其添加到新选择的面板中。
有点 hacky,但效果很好。
I had a panel with memory-intensive components on it which I only wanted to create one instance of, but change the behaviour of it using the attractive JTabbedPane.
I did it by creating empty panels for each tab, and a third panel that contains my (single) complicated components. On the StateChange event for the JTabbedPane, I remove the third panel from whichever of the first two it was in, and add it to whichever one is newly selected.
Bit hacky, but it works fine.
您错了。
只需在函数 initComponents() 的第一个选项卡上设置一个面板,如下所示:
然后使用:
使用此方法,您将在 2 个选项卡上拥有相同的组件。
您可以使用 StateChanged 事件来更改此选项卡中的操作。
例如:
You are wrong.
Just set a panel on the first tab in function initComponents() like that:
Then use:
Using this metode you will have the same component on 2 tabs.
You can use StateChanged Event to change actions in this tabs.
For example:
下面将允许您将具有不同标题的相同组件添加到 JTabbedPane:
The following will allow you to add the same component with different titles to a JTabbedPane: