JButton和JField标签/文字?

Sam*_*Sam 3 html java swing multiline jbutton

我有一个JButton我想填写的文本将分为两行,但是,当我输入测试和使用"\n""\r"我仍然得到一行文本.

这也是我的元素的情况jList.

谢谢

Bal*_*a R 8

使用HTML

JButton button = new JButton("<html><body>line 1 <br /> line 2</body></html>");
Run Code Online (Sandbox Code Playgroud)

  • 1)对于一个更复杂的例子(避免必须计算```的位置),请参阅[LabelRenderTest](http://stackoverflow.com/questions/5853879/java-swing-obtain-image-of -jframe/5853992#5853992).2)另请参阅Java教程中的[如何在Swing组件中使用HTML](http://download.oracle.com/javase/tutorial/uiswing/components/html.html),特别注意它对禁用组件的说明. (2认同)