可以解釋對象有人可以解釋爲什麼在這裏縮進文本「Options」?這在BoxLayout中看起來像一個bug。 TIA爲什麼使用Swing BoxLayout
import javax.swing.*;
import java.awt.*;
public class BoxLayoutIssue {
public static void main(String[] args) {
JFrame f = new JFrame();
f.setSize(240, 250);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JLabel label = new JLabel("Option");
panel.add(label);
JLabel a = new JLabel("A");
JLabel b = new JLabel("B");
JLabel c = new JLabel("C");
JLabel d = new JLabel("D");
JLabel e = new JLabel("E");
Box box = Box.createVerticalBox();
box.add(a);
box.add(b);
box.add(c);
box.add(d);
box.add(e);
JScrollPane jscrlpBox = new JScrollPane(box);
jscrlpBox.setPreferredSize(new Dimension(140, 90));
panel.add(jscrlpBox);
f.add(panel);
f.setVisible(true);
}
}
與您的問題無關 - 您應該在AWT事件線程中執行此操作。請參閱SwingUtilities.invokeLater(Runnable)。 – 2010-02-22 01:20:01
也許。 :)我只是一起鞭打這件事來解釋我遇到的問題。 – sproketboy 2010-02-23 10:07:56