如果我运行以下示例,右侧的JSplitPane会闪烁。是否有避免这种情况的方法?
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class FlickerTest
{
int width = 1;
private void create()
{
final JFrame f = new JFrame("JSplitPane");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p1 = new JPanel();
p1.setPreferredSize(new Dimension(100, 300));
JPanel p2 = new JPanel();
p2.setPreferredSize(new Dimension(0,0));
p2.setBackground(Color.gray);
JSplitPane jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, p1, p2);
jsp.setSize(new Dimension(400, 800));
Timer timer = new Timer(1, new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
width++;
if (width == 2)
{
try
{
Thread.sleep(1500);
}
catch (Exception ex)
{
}
}
int frameWidth = f.getWidth() + width;
Dimension d = new Dimension(frameWidth, f.getHeight());
f.setSize(d);
if (width > 20)
{
Timer t = (Timer) e.getSource();
t.stop();
}
}
});
f.add(jsp);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
timer.start();
}
public static void main(String[] args) throws Exception
{
new FlickerTest().create();
}
}