Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.94 KB | None | 0 0
  1. import javax.swing.*;
  2.  
  3. class FixedWidthText {
  4.  
  5. public static void showLabel(int width, String units) {
  6. String content1 = "<html>"
  7. + "<body style='background-color: white; width: ";
  8. String content2 = "'>"
  9. + "<h1>Fixed Width</h1>"
  10. + "<p>Body width fixed at ";
  11. String content3
  12. = " using CSS. "
  13. + "Java's HTML"
  14. + " support includes support"
  15. + " for basic CSS.</p>";
  16. final String content = content1 + width + units
  17. + content2 + width + units + content3;
  18. Runnable r = () -> {
  19. JLabel label = new JLabel(content);
  20. JOptionPane.showMessageDialog(null, label);
  21. };
  22. SwingUtilities.invokeLater(r);
  23. }
  24.  
  25. public static void main(String[] args) {
  26. showLabel(160, "px");
  27. showLabel(200, "px");
  28. showLabel(50, "%");
  29. }
  30. }
  31.  
  32. button.setText("<html><center>"+"This is a"+"<br>"+"swing button"+"</center></html>");
  33.  
  34. public class MultiLineButton extends JTextArea implements MouseListener {
  35. /**
  36. *
  37. */
  38. private static final long serialVersionUID = 1L;
  39. private Color defaultColor;
  40. private Color highlight, lightHighlight;
  41. private BtnState state;
  42. private List<ActionListener> actionListeners;
  43.  
  44. public MultiLineButton(String text, Color defaultColor) {
  45. this.setEditable(false);
  46. this.setText(text);
  47. this.setLineWrap(true);
  48. this.setWrapStyleWord(true);
  49. this.addMouseListener(this);
  50. this.setBorder(new EmptyBorder(5, 10, 5, 10));
  51. state = BtnState.NORMAL;
  52. this.defaultColor = defaultColor;
  53. this.setBackground(defaultColor);
  54. highlight = new Color(122, 138, 153);
  55. lightHighlight = new Color(184, 207, 229);
  56. // clickedColor = new Color(r, g, b);/
  57. actionListeners = new ArrayList<>();
  58. }
  59.  
  60. @Override
  61. public Color getSelectionColor() {
  62. return getBackground();
  63. }
  64.  
  65. @Override
  66. public void mouseClicked(MouseEvent e) {
  67. }
  68.  
  69. @Override
  70. public void mousePressed(MouseEvent e) {
  71. setBackground(lightHighlight);
  72. state = BtnState.CLICKED;
  73. repaint();
  74. }
  75.  
  76. @Override
  77. public void mouseReleased(MouseEvent e) {
  78. for (ActionListener l : actionListeners) {
  79. l.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, this.getText()));
  80. }
  81. setBackground(defaultColor);
  82. state = BtnState.NORMAL;
  83. repaint();
  84. }
  85.  
  86. @Override
  87. public void mouseEntered(MouseEvent e) {
  88. state = BtnState.HOVERED;
  89. repaint();
  90. }
  91.  
  92. @Override
  93. public void mouseExited(MouseEvent e) {
  94. setBackground(defaultColor);
  95. state = BtnState.NORMAL;
  96. repaint();
  97. }
  98.  
  99. @Override
  100. public void paintBorder(Graphics g) {
  101. super.paintBorder(g);
  102. Graphics g2 = g.create();
  103. g2.setColor(highlight);
  104. switch (state) {
  105. case NORMAL:
  106. g2.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
  107. break;
  108. case HOVERED:
  109. g2.drawRect(1, 1, getWidth() - 3, getHeight() - 3);
  110. g2.setColor(lightHighlight);
  111. g2.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
  112. g2.drawRect(2, 2, getWidth() - 5, getHeight() - 5);
  113. break;
  114. case CLICKED:
  115. Border b = new BevelBorder(BevelBorder.LOWERED);
  116. b.paintBorder(this, g2, 0, 0, getWidth(), getHeight());
  117. break;
  118. }
  119. g2.dispose();
  120. }
  121.  
  122. public void addActionListener(ActionListener l) {
  123. actionListeners.add(l);
  124. }
  125.  
  126. public List<ActionListener> getActionListeners() {
  127. return actionListeners;
  128. }
  129. }
  130.  
  131. @Override
  132. public void paint(Graphics pGraphics)
  133. {
  134. super.paint(pGraphics);
  135.  
  136. Graphics2D g2d = (Graphics2D) pGraphics;
  137. FontRenderContext frc = g2d.getFontRenderContext();
  138.  
  139. String itemName = item.getName();
  140. AttributedString attributedString = new AttributedString(itemName);
  141. attributedString.addAttribute(TextAttribute.FONT, getFont());
  142. AttributedCharacterIterator iterator = attributedString.getIterator();
  143.  
  144. LineBreakMeasurer measurer = new LineBreakMeasurer(iterator, frc);
  145. float wrappingWidth = getSize().width - 15;
  146.  
  147. StringBuilder stringBuilder = new StringBuilder("<html><center>");
  148.  
  149. int previousIndex = 0;
  150. while (measurer.getPosition() < itemName.length())
  151. {
  152. if (previousIndex != 0) stringBuilder.append("<br>");
  153. stringBuilder.append(itemName.substring(previousIndex, measurer.getPosition()));
  154. previousIndex = measurer.getPosition();
  155.  
  156. measurer.nextLayout(wrappingWidth);
  157. }
  158.  
  159. if (previousIndex < itemName.length())
  160. {
  161. if (previousIndex != 0) stringBuilder.append("<br>");
  162. stringBuilder.append(itemName.substring(previousIndex));
  163. }
  164.  
  165. stringBuilder.append("</center></html>");
  166. setText(stringBuilder.toString());
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement