gpt4 book ai didi

java - 动态添加和隐藏 JLayeredPane

转载 作者:行者123 更新时间:2023-11-30 05:10:39 25 4
gpt4 key购买 nike

我想在运行时动态添加/隐藏/删除 jlayeredpanes,并且在选择另一个 Pane 时还应该隐藏每个 Pane 上的内容。我已经尝试过以下代码,但我不知道如何执行此操作。以下代码在选择备用 Pane 时隐藏每个 Pane 的内容,但不会持续隐藏其内容。当我们将鼠标移到隐藏的内容区域上时,它们会再次可见。请帮助我!

public class floorsetup {

public static void createfloor(String name)
{
String name1=name+"_pane";
JButton b = new JButton(name);
final JLayeredPane jp = new JLayeredPane();
jp.setName(name1);
floor_plan.dynamicPane_floors.put(name1, jp);

//jp.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
jp.setAutoscrolls(true);
jp.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
jp.setMinimumSize(new java.awt.Dimension(1000, 700));
jp.setOpaque(true);
jp.setBounds(floor_plan.ground.getBounds());
floor_plan.jLayeredPane2.add(jp);
jp.addMouseListener(new java.awt.event.MouseAdapter() {
@Override
public void mouseClicked(java.awt.event.MouseEvent evt) {
//floor_plan.jLayeredPane2.setVisible(false);

int x = 0,y = 0;
//ComponentOrientation componentOrientation = jLayeredPane2.getComponentOrientation();
// Rectangle bounds = jLayeredPane2..getBounds();
// x=bounds.x;
//y=bounds.y;

//System.out.println(bounds);

x=evt.getX();
y=evt.getY();
System.out.println(x);
System.out.println(y);
// String name=floor_plan.table_name.getText();
String name="some name";
if(floor_plan.delete!=1)
tablesetup.addButton(name,x,y, (JLayeredPane) evt.getSource());
System.out.println((evt.getSource()));
}
});

b.setActionCommand(name);
b.setAlignmentX(Component.CENTER_ALIGNMENT);
b.setPreferredSize(new Dimension(125, 25));
b.setBackground(Color.green);

floor_plan.floors.add(b);
floor_plan.floors.add(Box.createRigidArea(new Dimension(10, 15)));
// b.setSize(125, 25);
floor_plan.dynamicButtons_floors.put(name, b);
MouseListenerClass M1 = new MouseListenerClass();
MouseClass M2 = new MouseClass();
b.addMouseMotionListener(M1);
b.addMouseListener(M2);
b.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {

if(floor_plan.delete==1)
{
removeButton(evt.getActionCommand());
}
else if(floor_plan.edit==1)
{
String edit_name = JOptionPane.showInputDialog("Name of the button:");
JButton source = (JButton) evt.getSource();
source.setActionCommand(edit_name);
source.setText(edit_name);
floor_plan.dynamicButtons_floors.put(edit_name, source);
}
else
{
String switcher=evt.getActionCommand();
switcher+="_pane";
switch_pane(switcher,evt);
}
}
});

floor_plan.floors.validate();

floor_plan.floors.repaint();
}
public static void removeButton(String name) {


JButton b = floor_plan.dynamicButtons_floors.remove(name);
floor_plan.jLayeredPane2.remove(b);
floor_plan.jLayeredPane2.invalidate();
floor_plan.jLayeredPane2.repaint();
}
public static void switch_pane(String name,ActionEvent evt)
{
JLayeredPane jp = floor_plan.dynamicPane_floors.get(name);
System.out.println(floor_plan.jLayeredPane2);
System.out.println(jp);
floor_plan.ground.setVisible(false);
floor_plan.ground.setEnabled(false);
jp.setVisible(true);
jp.moveToFront(floor_plan.ground);

}
}

这是使用 getText() 的一段代码。但我收到错误!!

 if(floor_plan.delete==1)
{
JButton source = (JButton) evt.getSource();
int index=floor_plan.floors.getComponentCount();
int val=0;

Component[] components = floor_plan.floors.getComponents();
for(int i=0; i<index;i++)
{

System.out.println(source.getName());
if(components[i].getText().equals(source.getName()))
{
val=i;
}
}
removeButton(evt.getActionCommand(),val);
}

最佳答案

听起来您可能想看看JTabbedPane。这将允许您拥有多个选项卡,每个选项卡中包含不同的内容。当用户选择一个选项卡时,仅显示该选项卡上的内容。

链接:

要通过按钮动态添加选项卡,您可以使用类似于以下的代码:

JButton newTabButton = new JButton("Add Tab");
newTabButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
JPanel newTab = new JPanel();
newTab.setLayout(null);
// Dynamic panel is a JTabbedPane
dynamicPanel.addTab(JOptionPane.showInputDialog("Name of tab"), newTab);
}
});

关于java - 动态添加和隐藏 JLayeredPane,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3485057/

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