Guest User

Untitled

a guest
Dec 10th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.27 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.EventQueue;
  3. import javax.swing.JFrame;
  4. import javax.swing.JLabel;
  5. import javax.swing.JPanel;
  6. import javax.swing.JScrollPane;
  7. import javax.swing.ScrollPaneConstants;
  8. import javax.swing.border.EmptyBorder;
  9.  
  10. public class Test extends JFrame
  11. {
  12.  
  13. private JPanel contentPane;
  14.  
  15. /**
  16. * Launch the application.
  17. */
  18. public static void main(String[] args)
  19. {
  20. EventQueue.invokeLater(new Runnable()
  21. {
  22. public void run()
  23. {
  24. try
  25. {
  26. Test frame = new Test();
  27.  
  28.  
  29. JScrollPane scrollPane = new JScrollPane();
  30. scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
  31. JPanel panel = new JPanel();
  32. panel.add(new JLabel("<html>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</html>"));
  33. scrollPane.setViewportView(panel);
  34. frame.add(scrollPane);
  35. frame.setVisible(true);
  36. }
  37. catch (Exception e)
  38. {
  39. e.printStackTrace();
  40. }
  41. }
  42. });
  43. }
  44.  
  45. /**
  46. * Create the frame.
  47. */
  48. public Test()
  49. {
  50. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  51. setBounds(100, 100, 450, 300);
  52. contentPane = new JPanel();
  53. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  54. contentPane.setLayout(new BorderLayout(0, 0));
  55. setContentPane(contentPane);
  56. }
  57.  
  58. }
  59.  
  60. import java.awt.EventQueue;
  61. import java.awt.GridBagConstraints;
  62. import java.awt.GridBagLayout;
  63.  
  64. import javax.swing.JFrame;
  65. import javax.swing.JLabel;
  66. import javax.swing.JPanel;
  67. import javax.swing.ScrollPaneConstants;
  68. import javax.swing.border.EmptyBorder;
  69. import javax.swing.JScrollPane;
  70.  
  71. public class Test extends JFrame
  72. {
  73.  
  74. private JPanel contentPane;
  75.  
  76. /**
  77. * Launch the application.
  78. */
  79. public static void main(String[] args)
  80. {
  81. EventQueue.invokeLater(new Runnable()
  82. {
  83. public void run()
  84. {
  85. try
  86. {
  87. Test frame = new Test();
  88. frame.setVisible(true);
  89.  
  90. }
  91. catch (Exception e)
  92. {
  93. e.printStackTrace();
  94. }
  95. }
  96. });
  97. }
  98.  
  99. /**
  100. * Create the frame.
  101. */
  102. public Test()
  103. {
  104. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  105. setBounds(100, 100, 450, 300);
  106. contentPane = new JPanel();
  107. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  108. setContentPane(contentPane);
  109. GridBagLayout gbl_contentPane = new GridBagLayout();
  110. gbl_contentPane.columnWidths = new int[] { 0, 0 };
  111. gbl_contentPane.rowHeights = new int[] { 0, 0 };
  112. gbl_contentPane.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
  113. gbl_contentPane.rowWeights = new double[] { 1.0, Double.MIN_VALUE };
  114. contentPane.setLayout(gbl_contentPane);
  115.  
  116. JScrollPane scrollPane = new JScrollPane();
  117. GridBagConstraints gbc_scrollPane = new GridBagConstraints();
  118. gbc_scrollPane.fill = GridBagConstraints.BOTH;
  119. gbc_scrollPane.gridx = 0;
  120. gbc_scrollPane.gridy = 0;
  121. contentPane.add(scrollPane, gbc_scrollPane);
  122.  
  123. JPanel panel = new JPanel();
  124. scrollPane.setViewportView(panel);
  125. scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
  126. GridBagLayout gbl_panel = new GridBagLayout();
  127. gbl_panel.columnWidths = new int[]{0, 0};
  128. gbl_panel.rowHeights = new int[]{0, 0};
  129. gbl_panel.columnWeights = new double[]{0.0, Double.MIN_VALUE};
  130. gbl_panel.rowWeights = new double[]{0.0, Double.MIN_VALUE};
  131. panel.setLayout(gbl_panel);
  132.  
  133. JLabel lblNewLabel = new JLabel("<html>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</html>");
  134. GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
  135. gbc_lblNewLabel.gridx = 0;
  136. gbc_lblNewLabel.gridy = 0;
  137. panel.add(lblNewLabel, gbc_lblNewLabel);
  138. }
  139.  
  140. }
  141.  
  142. import java.awt.EventQueue;
  143. import java.awt.GridBagConstraints;
  144. import java.awt.GridBagLayout;
  145.  
  146. import javax.swing.JFrame;
  147. import javax.swing.JLabel;
  148. import javax.swing.JPanel;
  149. import javax.swing.border.EmptyBorder;
  150.  
  151. public class Test extends JFrame
  152. {
  153.  
  154. private JPanel contentPane;
  155.  
  156. /**
  157. * Launch the application.
  158. */
  159. public static void main(String[] args)
  160. {
  161. EventQueue.invokeLater(new Runnable()
  162. {
  163. public void run()
  164. {
  165. try
  166. {
  167. Test frame = new Test();
  168. frame.setVisible(true);
  169.  
  170. }
  171. catch (Exception e)
  172. {
  173. e.printStackTrace();
  174. }
  175. }
  176. });
  177. }
  178.  
  179. /**
  180. * Create the frame.
  181. */
  182. public Test()
  183. {
  184. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  185. setBounds(100, 100, 450, 300);
  186. contentPane = new JPanel();
  187. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  188. setContentPane(contentPane);
  189. GridBagLayout gbl_contentPane = new GridBagLayout();
  190. gbl_contentPane.columnWidths = new int[] { 0, 0 };
  191. gbl_contentPane.rowHeights = new int[] { 0, 0 };
  192. gbl_contentPane.columnWeights = new double[] { 0.0, Double.MIN_VALUE };
  193. gbl_contentPane.rowWeights = new double[] { 0.0, Double.MIN_VALUE };
  194. contentPane.setLayout(gbl_contentPane);
  195.  
  196. JLabel lblNewLabel = new JLabel(
  197. "<html>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</html>");
  198. GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
  199. gbc_lblNewLabel.weightx = 1.0;
  200. gbc_lblNewLabel.fill = GridBagConstraints.BOTH;
  201. gbc_lblNewLabel.gridx = 0;
  202. gbc_lblNewLabel.gridy = 0;
  203. contentPane.add(lblNewLabel, gbc_lblNewLabel);
  204. }
  205.  
  206. }
  207.  
  208. import java.awt.*;
  209. import javax.swing.*;
  210. import javax.swing.border.EmptyBorder;
  211.  
  212. public class Test extends JFrame {
  213.  
  214. public static void main(String[] args) {
  215. EventQueue.invokeLater(new Runnable() {
  216. public void run() {
  217. try {
  218. Test frame = new Test();
  219.  
  220. JTextArea message = new JTextArea("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
  221. ,6,40);
  222. // make it look & act like a label
  223. message.setWrapStyleWord(true);
  224. message.setLineWrap(true);
  225. message.setEditable(false);
  226. message.setFocusable(false);
  227. message.setOpaque(false);
  228.  
  229. JScrollPane scrollPane = new JScrollPane(message,
  230. ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
  231. ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER
  232. );
  233. frame.add(scrollPane);
  234. frame.pack();
  235. frame.setVisible(true);
  236. }
  237. catch (Exception e) {
  238. e.printStackTrace();
  239. }
  240. }
  241. });
  242. }
  243.  
  244. public Test() {
  245. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  246. setLocationByPlatform(true);
  247. }
  248. }
  249.  
  250. import java.awt.BorderLayout;
  251. import java.awt.Dimension;
  252. import java.awt.EventQueue;
  253. import java.awt.Rectangle;
  254.  
  255. import javax.swing.JFrame;
  256. import javax.swing.JLabel;
  257. import javax.swing.JPanel;
  258. import javax.swing.JScrollPane;
  259. import javax.swing.ScrollPaneConstants;
  260. import javax.swing.Scrollable;
  261. import javax.swing.border.EmptyBorder;
  262.  
  263. public class TestScrollLabel extends JFrame
  264. {
  265.  
  266. private JPanel contentPane;
  267.  
  268. /**
  269. * Launch the application.
  270. */
  271. public static void main(String[] args)
  272. {
  273. EventQueue.invokeLater(new Runnable()
  274. {
  275. public void run()
  276. {
  277. try
  278. {
  279. TestScrollLabel frame = new TestScrollLabel();
  280. JScrollPane scrollPane = new JScrollPane();
  281. scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
  282. JPanel panel = new MyPanel();
  283. //BorderLayout is a good choice here
  284. panel.setLayout(new BorderLayout());
  285. panel.add(new JLabel("<html>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</html>"));
  286. scrollPane.setViewportView(panel);
  287. frame.add(scrollPane);
  288. frame.setVisible(true);
  289. }
  290. catch (Exception e)
  291. {
  292. e.printStackTrace();
  293. }
  294. }
  295. });
  296. }
  297.  
  298. static class MyPanel extends JPanel implements Scrollable{
  299.  
  300. @Override
  301. public Dimension getPreferredScrollableViewportSize() {
  302.  
  303. return this.getPreferredSize();
  304. }
  305.  
  306. @Override
  307. public int getScrollableUnitIncrement(Rectangle visibleRect,
  308. int orientation, int direction) {
  309. return 50;
  310. }
  311.  
  312. @Override
  313. public int getScrollableBlockIncrement(Rectangle visibleRect,
  314. int orientation, int direction) {
  315. return 80;
  316. }
  317.  
  318. /*
  319. * (non-Javadoc)
  320. * @see javax.swing.Scrollable#getScrollableTracksViewportWidth()
  321. */
  322. @Override
  323. public boolean getScrollableTracksViewportWidth() {
  324. return true;
  325. }
  326.  
  327. @Override
  328. public boolean getScrollableTracksViewportHeight() {
  329. return false;
  330. }
  331.  
  332. }
  333.  
  334. /**
  335. * Create the frame.
  336. */
  337. public TestScrollLabel()
  338. {
  339. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  340. setBounds(100, 100, 450, 300);
  341. contentPane = new JPanel();
  342. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  343. contentPane.setLayout(new BorderLayout(0, 0));
  344. setContentPane(contentPane);
  345. }
  346.  
  347. }
Add Comment
Please, Sign In to add comment