gpt4 book ai didi

java - 移动后组件不再可调整大小

转载 作者:太空宇宙 更新时间:2023-11-04 08:52:26 24 4
gpt4 key购买 nike

我的问题与 Swing 编程有关。我想通过将组件(组件 x)从其父面板(组件 a)中删除并将其添加到组件 a 的父组件(组件 b)之一中来放大组件(组件 x)。在此之前,我对 b 中的所有组件调用 setVisible(false)。之后我想通过从 b 中删除它并添加到 a 来恢复它。

此后,所有组件都无法再调整大小。

为什么会这样?

一个简单的例子:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTabbedPane;

public class SwingTest {

private static ViewPanel layer1;
private static JFrame frame;
private static JTabbedPane tabbedPane;
private static ViewPanel root;

public static void main(String[] args) {
frame = new JFrame();
frame.setLayout(new BorderLayout());
frame.setMinimumSize(new Dimension(800, 600));

root = new ViewPanel();
root.setBackground(Color.blue);
root.setPreferredSize(new Dimension(400, 600));
root.setLayout(new BorderLayout());
root.add(new JLabel("blue area"));

layer1 = new ViewPanel();
layer1.setBackground(Color.red);
layer1.setPreferredSize(new Dimension(400, 600));
layer1.setLayout(new BorderLayout());

tabbedPane = new JTabbedPane();
tabbedPane.add("A", new JLabel("A label"));
tabbedPane.setPreferredSize(new Dimension(400, 600));

layer1.add(tabbedPane);

root.add(layer1);
frame.add(root, BorderLayout.NORTH);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setVisible(true);

Thread t = new Thread() {

@Override
public void run() {
try {
Thread.sleep(8000);
System.out.println("start");
for (Component c : root.getComponents()) {
c.setVisible(false);
}
layer1.remove(tabbedPane);
root.add(tabbedPane);

Thread.sleep(8000);

root.remove(tabbedPane);
layer1.add(tabbedPane);

for (Component c : root.getComponents()) {
c.setVisible(true);
c.repaint();
}
} catch (InterruptedException e) {
//...
}
}
};
t.start();
}
}

最佳答案

After that all components are not longer resizable.

我认为 LayoutManager 的选择掩盖了效果。一般来说,在 event dispatch thread 上构建 GUI 是个好主意。 ,并且使用 sleep() 阻止该线程是一个坏主意。此替代示例使用 javax.swing.Timer 定期更改计时器的 actionPerformed() 方法中的内容。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.Timer;

public class SwingTest implements ActionListener, Runnable {

private JFrame frame;
private ViewPanel root;
private ViewPanel layer1;
private JTabbedPane tabbedPane;
private Timer t = new Timer(1000, this);
private boolean remove = true;

public static void main(String[] args) {
EventQueue.invokeLater(new SwingTest());
}

private static class ViewPanel extends JPanel {

public ViewPanel(Color color) { // default FlowLayout
this.setPreferredSize(new Dimension(400, 500));
this.setBackground(color);
}
}

@Override
public void run() {
frame = new JFrame(); // default BorderLayout
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(600, 600));
root = new ViewPanel(Color.blue);
root.add(new JLabel("Blue area"));
layer1 = new ViewPanel(Color.red);
tabbedPane = new JTabbedPane();
tabbedPane.add("A", new JLabel("A label", JLabel.CENTER));
tabbedPane.setPreferredSize(root.getPreferredSize());
layer1.add(tabbedPane);
root.add(layer1);
root.add(new JLabel("Blue area"));
frame.add(root, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
t.start();
}

@Override
public void actionPerformed(ActionEvent e) {
if (remove) {
for (Component c : root.getComponents()) {
c.setVisible(false);
}
layer1.remove(tabbedPane);
} else {
layer1.add(tabbedPane);
for (Component c : root.getComponents()) {
c.setVisible(true);
}
}
remove = !remove;
}
}

关于java - 移动后组件不再可调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3047043/

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