gpt4 book ai didi

java删除选项卡并将jpanel恢复为原始状态

转载 作者:太空宇宙 更新时间:2023-11-04 12:53:48 25 4
gpt4 key购买 nike

我有一个带有按钮的 jpanel (jpanel A),按下时将打开一个选项卡 jpanel(示例 1)

enter image description here

因此,当您按下按钮时,您最终会看到 2 个选项卡 jpanel A 和示例 1。

原来的 japanel 现在是一个选项卡式面板

当我删除示例 1 时,我只剩下 jpanel A,但是该面板位于选项卡中。 enter image description here当另一个 jpanel(示例 1)被删除时,有没有办法可以恢复到原始 jpanel。

enter image description here

我制作了一个与我的类似的示例应用程序:

enter image description here

点击call a jpanel后你会得到。 enter image description here

然后点击 press for tab 2 次,你会得到 2 个新标签 enter image description here

删除 2 个选项卡后,您将得到: enter image description here

我想做的最终结果是: enter image description here即返回原始内容删除选项卡保留内容

该应用程序分为 3 类。

  1. CreatePanel 制作右侧 jpanel 并按住按钮制作 tab。
  2. MainGUI 创建 jframe 左侧以调用右侧面板。
  3. CloseButtonTabbedPane 以创建选项卡并控制它们。

这是代码:主GUI

               import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;

public class MainGUI extends JFrame {

private JPanel jPanelLeft;
private JPanel jPanelRight;
private JButton callPanelBtn;
private JTabbedPane tab;

public MainGUI() {

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(1280, 600); // set frame size
this.setVisible(true); // display frame
this.setTitle("Tab Example");

setLayout(new BorderLayout()); // layout manager
jPanelLeft = new JPanel();
jPanelLeft.setLayout(null);
jPanelLeft.setPreferredSize(new Dimension(400, 800)); // to set the size of the left panel
jPanelLeft.setBackground(Color.blue);
this.add(jPanelLeft, BorderLayout.WEST);

callPanelBtn = new JButton("Call a jPanel");
callPanelBtn.addActionListener(btn);
callPanelBtn.setBounds(150, 200, 150, 40);
jPanelLeft.add(callPanelBtn);

jPanelRight = new JPanel();
jPanelRight.setBorder(new EmptyBorder(0, 0, 0, 0));
jPanelRight.setLayout(new GridLayout(0, 1));
this.add(jPanelRight);

tab = new CloseButtonTabbedPane();

}//endd constructor

ActionListener btn = new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {

if (ae.getActionCommand() == "Call a jPanel") {
jPanelRight.add(new CreatePanel(t));
jPanelRight.revalidate();

}
}

};

// Actionlistener for CreatePanel
ActionListener t = new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {

if (tab.getTabCount() <= 8) {

// if no tabs make main tab
if (tab.getTabCount() == 0) {

tab.addTab("Main Content", jPanelRight); // name for listener not to close
add(tab);
revalidate();
repaint();
}

if (tab.getTabCount() > 0) {
JPanel newJPanel = new JPanel();
tab.addTab("new ", newJPanel);

tab.setSelectedIndex(tab.getTabCount() - 1);
}
}

}

};

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {

new MainGUI();
}

});

} // end main

}//end class

创建面板

                    import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;


public class CreatePanel extends JPanel {
private JPanel jPanel1;
private final JButton tabBTN;

public CreatePanel(ActionListener t){
jPanel1 = new JPanel();
jPanel1.setLayout(null);
jPanel1.setBorder(BorderFactory.createBevelBorder(1, Color.lightGray, Color.lightGray));
jPanel1.setPreferredSize(new Dimension(850, 300));
jPanel1.setBackground(Color.green);
add(jPanel1);

//add button for tabs
tabBTN = new JButton("Press for Tab");
tabBTN.setBounds(400, 100, 150, 30);
tabBTN.addActionListener(t);
jPanel1.add(tabBTN);
}//end constructor
}//end class

关闭按钮TabbedPane

                      import javax.swing.*;
import javax.swing.plaf.metal.MetalIconFactory;
import javax.swing.plaf.basic.BasicTabbedPaneUI;
import java.awt.image.BufferedImage;
import java.awt.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class CloseButtonTabbedPane extends JTabbedPane {

public CloseButtonTabbedPane() {

}

@Override
public void addTab(String title, Icon icon, Component component, String tip) {
super.addTab(title, icon, component, tip);
int count = this.getTabCount() - 1;
setTabComponentAt(count, new CloseButtonTab(component, title, icon));
}

@Override
public void addTab(String title, Icon icon, Component component) {
addTab(title, icon, component, null);
}

@Override
public void addTab(String title, Component component) {
addTab(title, null, component);
}

public class CloseButtonTab extends JPanel {

private Component tab;

public CloseButtonTab(final Component tab, String title, Icon icon) {
this.tab = tab;
setOpaque(false);
FlowLayout flowLayout = new FlowLayout(FlowLayout.CENTER, 3, 3);
setLayout(flowLayout);
setVisible(true);

JLabel jLabel = new JLabel(title);
jLabel.setIcon(icon);
add(jLabel);

JButton button = new JButton(MetalIconFactory.getInternalFrameCloseIcon(16));
button.setMargin(new Insets(0, 0, 0, 0));
button.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 1));
button.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
JTabbedPane tabbedPane = (JTabbedPane) getParent().getParent();
if (tabbedPane.getSelectedIndex() >= 1) {
tabbedPane.remove(tab);
}

}

public void mousePressed(MouseEvent e) {
}

public void mouseReleased(MouseEvent e) {
}

public void mouseEntered(MouseEvent e) {
JButton button = (JButton) e.getSource();
button.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1));
}

public void mouseExited(MouseEvent e) {
JButton button = (JButton) e.getSource();
button.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 1));
}
});

add(button);

}
}
}

最佳答案

我不知道是否有工厂解决方案,但对于所有 JComponent,您可以设置一个处理外观和感觉的自定义 UI 对象,即使用 setUI 方法的外观。 JTabbedPanes 的一种实现称为 BasicTabbedPaneUI,并具有一个 calculateTabAreaHeight 方法,用于计算顶部选项卡栏的高度。
因此,您可以将自己的 BasicTabbedPaneUI 实例设置为 JTabbedPane 并覆盖此方法,以便在仅存在一个选项卡时返回 0
将其放入您的构造函数中

[...]
tab = new CloseButtonTabbedPane();

tab.setUI(new BasicTabbedPaneUI(){
@Override
protected int calculateTabAreaHeight(int tabPlacement, int horizRunCount, int maxTabHeight){
if (tab.getTabCount() == 1) return 0;
return super.calculateTabAreaHeight(tabPlacement, horizRunCount, maxTabHeight);
}
});

关于java删除选项卡并将jpanel恢复为原始状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35577557/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com