我正在使用JFrame來顯示帶有一些文本和2個按鈕的消息框。如何根據框的大小自動換行文字?Java:文本沒有包裝在JFrame對話框中
這裏是我當前的代碼:
dialogFrame = new JFrame();
JButton newButton = new JButton("New");
newButton.addActionListener(new newUploadAction());
JButton resumeButton = new JButton("Resume");
resumeButton.addActionListener(new resumeUploadAction());
//dialogFrame.setUndecorated(true);
JPanel addPanel = new JPanel();
JPanel addPanel2 = new JPanel();
addPanel.add(newButton);
addPanel.add(resumeButton);
String text = "<html><p>A previous control file exists for this file. ";
text += "Would you like to initiate a new transfer or resume the previous one?</p></html>";
JLabel testLabel = new JLabel(text);
//testLabel.setPreferredSize(new Dimension(1, 1));
addPanel2.add(testLabel);
Container content = dialogFrame.getContentPane();
//content.setBackground(Color.white);
content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS));
content.add(addPanel2);
content.add(addPanel);
dialogFrame.setSize(200,200);
dialogFrame.setVisible(true);
dialogFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
我讀的地方,叫
testLabel.setPreferredSize(new Dimension(1, 1));
會導致包裝的行爲我想,但只是導致文本完全無法顯示了。
可能的重複 - http://stackoverflow.com/questions/2420742/make-a-jlabel-wrap-its-text-by-setting-a-max-width – mre 2011-06-01 03:25:41