JButton中的自动换行

是否可以在JButtons中实现文本自动自动换行?我在运行时创建了几个动态按钮。我想在按钮上使用自动换行功能,以便可以对按钮进行更好的测试。有可能这样做吗?



暮色呼如
浏览 957回答 3
3回答

慕的地10843

最简单的方法是修改另一个支持自动换行的Component,以使其充当Button。我做了一个简单的类,它操纵JTextArea充当Button。&nbsp;public class MultiLineButton extends JTextArea implements MouseListener&nbsp; &nbsp; {&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; private static final long serialVersionUID = 1L;&nbsp; &nbsp; private Color defaultColor;&nbsp; &nbsp; private Color highlight, lightHighlight;&nbsp; &nbsp; private BtnState state;&nbsp; &nbsp; private List<ActionListener> actionListeners;&nbsp; &nbsp; public MultiLineButton(String text, Color defaultColor) {&nbsp; &nbsp; &nbsp; &nbsp; this.setEditable(false);&nbsp; &nbsp; &nbsp; &nbsp; this.setText(text);&nbsp; &nbsp; &nbsp; &nbsp; this.setLineWrap(true);&nbsp; &nbsp; &nbsp; &nbsp; this.setWrapStyleWord(true);&nbsp; &nbsp; &nbsp; &nbsp; this.addMouseListener(this);&nbsp; &nbsp; &nbsp; &nbsp; this.setBorder(new EmptyBorder(5, 10, 5, 10));&nbsp; &nbsp; &nbsp; &nbsp; state = BtnState.NORMAL;&nbsp; &nbsp; &nbsp; &nbsp; this.defaultColor = defaultColor;&nbsp; &nbsp; &nbsp; &nbsp; this.setBackground(defaultColor);&nbsp; &nbsp; &nbsp; &nbsp; highlight = new Color(122, 138, 153);&nbsp; &nbsp; &nbsp; &nbsp; lightHighlight = new Color(184, 207, 229);&nbsp; &nbsp; &nbsp; &nbsp; // clickedColor = new Color(r, g, b);/&nbsp; &nbsp; &nbsp; &nbsp; actionListeners = new ArrayList<>();&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public Color getSelectionColor() {&nbsp; &nbsp; &nbsp; &nbsp; return getBackground();&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void mouseClicked(MouseEvent e) {&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void mousePressed(MouseEvent e) {&nbsp; &nbsp; &nbsp; &nbsp; setBackground(lightHighlight);&nbsp; &nbsp; &nbsp; &nbsp; state = BtnState.CLICKED;&nbsp; &nbsp; &nbsp; &nbsp; repaint();&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; &nbsp; @Override&nbsp; &nbsp; public void mouseReleased(MouseEvent e) {&nbsp; &nbsp; &nbsp; &nbsp; for (ActionListener l : actionListeners) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; l.actionPerformed(new ActionEvent(this,&nbsp; &nbsp; &nbsp;ActionEvent.ACTION_PERFORMED, this.getText()));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; setBackground(defaultColor);&nbsp; &nbsp; &nbsp; &nbsp; state = BtnState.NORMAL;&nbsp; &nbsp; &nbsp; &nbsp; repaint();&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void mouseEntered(MouseEvent e) {&nbsp; &nbsp; &nbsp; &nbsp; state = BtnState.HOVERED;&nbsp; &nbsp; &nbsp; &nbsp; repaint();&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void mouseExited(MouseEvent e) {&nbsp; &nbsp; &nbsp; &nbsp; setBackground(defaultColor);&nbsp; &nbsp; &nbsp; &nbsp; state = BtnState.NORMAL;&nbsp; &nbsp; &nbsp; &nbsp; repaint();&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void paintBorder(Graphics g) {&nbsp; &nbsp; &nbsp; &nbsp; super.paintBorder(g);&nbsp; &nbsp; &nbsp; &nbsp; Graphics g2 = g.create();&nbsp; &nbsp; &nbsp; &nbsp; g2.setColor(highlight);&nbsp; &nbsp; &nbsp; &nbsp; switch (state) {&nbsp; &nbsp; &nbsp; &nbsp; case NORMAL:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g2.drawRect(0, 0, getWidth() - 1, getHeight() - 1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case HOVERED:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g2.drawRect(1, 1, getWidth() - 3, getHeight() - 3);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g2.setColor(lightHighlight);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g2.drawRect(0, 0, getWidth() - 1, getHeight() - 1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g2.drawRect(2, 2, getWidth() - 5, getHeight() - 5);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case CLICKED:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Border b = new BevelBorder(BevelBorder.LOWERED);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b.paintBorder(this, g2, 0, 0, getWidth(), getHeight());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; g2.dispose();&nbsp; &nbsp; }&nbsp; &nbsp; public void addActionListener(ActionListener l) {&nbsp; &nbsp; &nbsp; &nbsp; actionListeners.add(l);&nbsp; &nbsp; }&nbsp; &nbsp; public List<ActionListener> getActionListeners() {&nbsp; &nbsp; &nbsp; &nbsp; return actionListeners;&nbsp; &nbsp; }}BtnState只是一个具有常量NORMAL,HOVERED,CLICKED的枚举大多数代码仅用于使JTextArea看起来像JButton,并且效果很好。缺点之一是您无法通过ButtonModels修改它,但是对于大多数应用程序来说,这就足够了。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java