gpt4 book ai didi

java - JLayeredPane 之后未出现组件

转载 作者:行者123 更新时间:2023-12-01 10:04:04 27 4
gpt4 key购买 nike

我有一个从 JFrame 扩展的类。我里面有多个 JLayeredPanes。我想从一个 JLayeredPane 切换到另一个。为此,我所做的是;

  1. 删除当前的 JLayeredPane,如 remove(pane1);
  2. 重绘();
  3. 将组件添加到新的 JLayeredPane 并设置边界
  4. 添加( Pane 2)

这将删除我当前的 JLayeredPane 并显示下一个 JLayeredPane。但组件没有添加到正确的位置。例如,我有一个包含文本区域的 jscrollpane。仅出现滚动 Pane ,并且缺少文本区域。此外,组合框的大小也发生了变化。为什么会发生这种情况?

这是一个包含整个 java 文件的 zip 文件 http://s000.tinyupload.com/index.php?file_id=33381866612121130517

最佳答案

我结合了这个JLayeredPane example有了这个CardLayout example演示多个可选择的分层 Pane 。

image

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;

/**
* @see https://stackoverflow.com/a/36587584/230513
* @see https://stackoverflow.com/a/6432291/230513
* @see https://stackoverflow.com/questions/6432170
*/
public class CardPanel extends JLayeredPane {

private static final Dimension d = new Dimension(320, 240);
private static final Random random = new Random();
private static final JPanel cards = new JPanel(new CardLayout());
private static final JComboBox combo = new JComboBox();
private final String name;

public CardPanel(String name) {
this.name = name;
this.setBackground(new Color(random.nextInt()));
this.add(new LayerPanel(1 * d.height / 8), 100);
this.add(new LayerPanel(2 * d.height / 8), 101);
this.add(new LayerPanel(3 * d.height / 8), 102);
}

@Override
public Dimension getPreferredSize() {
return d;
}

@Override
public String toString() {
return name;
}

private static class LayerPanel extends JPanel {

private static final Random r = new Random();
private int n;
private Color color = new Color(r.nextInt());

public LayerPanel(int n) {
this.n = n;
this.setOpaque(false);
this.setBounds(n, n, d.width / 2, d.height / 2);
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(color);
g2d.fillRoundRect(0, 0, getWidth(), getHeight(), 16, 16);
g2d.setColor(Color.black);
g2d.drawString(String.valueOf(n), 5, getHeight() - 5);
}
}

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

@Override
public void run() {
create();
}
});
}

private static void create() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
for (int i = 1; i < 9; i++) {
CardPanel p = new CardPanel("Panel " + String.valueOf(i));
combo.addItem(p);
cards.add(p, p.toString());
}
JPanel control = new JPanel();
combo.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
JComboBox jcb = (JComboBox) e.getSource();
CardLayout cl = (CardLayout) cards.getLayout();
cl.show(cards, jcb.getSelectedItem().toString());
}
});
control.add(combo);
f.add(cards, BorderLayout.CENTER);
f.add(control, BorderLayout.SOUTH);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}

关于java - JLayeredPane 之后未出现组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36585780/

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