Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2014
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.16 KB | None | 0 0
  1. package test;
  2.  
  3. import java.awt.Dimension;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6.  
  7. import javax.swing.BorderFactory;
  8. import javax.swing.Box;
  9. import javax.swing.BoxLayout;
  10. import javax.swing.JButton;
  11. import javax.swing.JFrame;
  12. import javax.swing.JLabel;
  13. import javax.swing.JPanel;
  14. import javax.swing.JScrollPane;
  15. import javax.swing.JTextArea;
  16. import javax.swing.JTextField;
  17. import javax.swing.ScrollPaneConstants;
  18. import javax.swing.SwingUtilities;
  19. import javax.swing.border.EtchedBorder;
  20.  
  21. /**
  22. * a simple class that uses box layouts to make a resizable JFrame with some components
  23. * @author MarkW
  24. *
  25. */
  26. public class GuiTest extends JFrame {
  27.  
  28. public static void main(String[] args) {
  29.  
  30. //using an annoynmous class here of type runnable
  31. SwingUtilities.invokeLater(new Runnable() {
  32.  
  33. @Override
  34. public void run() {
  35. GuiTest gt = new GuiTest();
  36. gt.setVisible(true);
  37. }
  38.  
  39. });
  40. }
  41.  
  42. //our graphical components
  43. private JPanel mainPanel;
  44. private JTextArea textArea;
  45. private JTextField textField;
  46. private JButton okButton;
  47. private JButton clearButton;
  48. private JButton exitButton;
  49.  
  50. //our button listener
  51. private ButtonListener buttonListener;
  52.  
  53. //some dimensions
  54. private final Dimension TEXTAREASIZE = new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
  55. private final Dimension TEXTFIELDSIZE = new Dimension(100, 20);
  56. private final Dimension BUTTONSIZE = new Dimension(75,35);
  57.  
  58. /**
  59. * GuiTest constructor
  60. */
  61. public GuiTest() {
  62. //set some basic stuff
  63. this.setBounds(100, 100, 600, 450);
  64. this.setMinimumSize(new Dimension(550, 400));
  65. //close the app when the form is closed
  66. this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  67. this.setTitle("Guit Examples");
  68. //this is where we will set up our components
  69. init();
  70. }
  71.  
  72. public void init() {
  73. //build our main panel which will be our content pane
  74. mainPanel = new JPanel();
  75. //this is a box layout with a Y axis... adds components top down
  76. mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
  77. //this looks complicated,
  78. //but its just making an empty outer border thats 10 pixels on the top bottom left and right
  79. //and making a lowered etched border with a break in it for text as the inner border
  80. mainPanel.setBorder(BorderFactory.createCompoundBorder(
  81. BorderFactory.createEmptyBorder(10, 10, 10, 10),
  82. BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), "Box Layouts!")));
  83.  
  84. //I like to use a quick and dirty local JPanel to help arrange stuff
  85. JPanel tPanel;
  86.  
  87. //add our text area first
  88. textArea = new JTextArea();
  89. //give our text area a scroll bar
  90. JScrollPane scrollPane = new JScrollPane(textArea);
  91. scrollPane.setMaximumSize(TEXTAREASIZE);
  92. scrollPane.setBorder(BorderFactory.createCompoundBorder(
  93. BorderFactory.createEmptyBorder(5, 5, 5, 5),
  94. BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), "Output Window")));
  95. scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
  96. mainPanel.add(scrollPane);
  97. //this is an invisible component that grows to fill space vertically
  98. //its below the textArea scroll pane
  99. mainPanel.add(Box.createVerticalGlue());
  100.  
  101.  
  102. //do the text field stuff
  103. textField = new JTextField();
  104. textField.setMinimumSize(TEXTFIELDSIZE);
  105. textField.setMaximumSize(TEXTFIELDSIZE);
  106. textField.setPreferredSize(TEXTFIELDSIZE);
  107.  
  108. //time to use our temp panel
  109. tPanel = new JPanel();
  110. //x axis this time for left to right
  111. tPanel.setLayout(new BoxLayout(tPanel, BoxLayout.X_AXIS));
  112. //add a fixed width invisible component on the left
  113. tPanel.add(Box.createHorizontalStrut(15));
  114. //create a label for the text field
  115. tPanel.add(new JLabel("Enter Text:"));
  116. //add a small spacer
  117. tPanel.add(Box.createHorizontalStrut(3));
  118. //add the text field
  119. tPanel.add(textField);
  120.  
  121. //make our button listener now so we have it
  122. buttonListener = new ButtonListener();
  123.  
  124. //lets push the next items to the far right side of the form
  125. tPanel.add(Box.createHorizontalGlue());
  126. //add the buttons
  127. okButton = new JButton("Ok");
  128. okButton.addActionListener(buttonListener);
  129. okButton.setMinimumSize(BUTTONSIZE);
  130. okButton.setMaximumSize(BUTTONSIZE);
  131. okButton.setPreferredSize(BUTTONSIZE);
  132. tPanel.add(okButton);
  133. //a small spacer
  134. tPanel.add(Box.createHorizontalStrut(5));
  135. clearButton = new JButton("Clear");
  136. clearButton.addActionListener(buttonListener);
  137. clearButton.setMinimumSize(BUTTONSIZE);
  138. clearButton.setMaximumSize(BUTTONSIZE);
  139. clearButton.setPreferredSize(BUTTONSIZE);
  140. tPanel.add(clearButton);
  141. //a small spacer
  142. tPanel.add(Box.createHorizontalStrut(5));
  143. exitButton = new JButton("Exit");
  144. exitButton.addActionListener(buttonListener);
  145. exitButton.setMinimumSize(BUTTONSIZE);
  146. exitButton.setMaximumSize(BUTTONSIZE);
  147. exitButton.setPreferredSize(BUTTONSIZE);
  148. tPanel.add(exitButton);
  149. //lets put some space between the buttons and the right edge
  150. tPanel.add(Box.createHorizontalStrut(15));
  151.  
  152.  
  153. //add the temp panel to the main panel below the text area
  154. mainPanel.add(tPanel);
  155.  
  156. //this is where the main panel gets
  157. //added to the JFrames content pane
  158. this.getContentPane().add(mainPanel);
  159. }
  160.  
  161. /**
  162. * this class will listen for click events on the buttons
  163. * and do what should be done from there
  164. *
  165. * @author MarkW
  166. *
  167. */
  168. public class ButtonListener implements ActionListener {
  169.  
  170. @Override
  171. public void actionPerformed(ActionEvent e) {
  172.  
  173. if (e.getSource() == okButton) {
  174. SwingUtilities.invokeLater(new Runnable() {
  175. @Override
  176. public void run() {
  177. textArea.append(textField.getText() + "\r\n");
  178. textArea.setCaretPosition(textArea.getText().length());
  179. textField.setText("");
  180. }
  181. });
  182. }
  183. else if (e.getSource() == clearButton) {
  184. SwingUtilities.invokeLater(new Runnable() {
  185.  
  186. @Override
  187. public void run() {
  188. textArea.setText("");
  189. }
  190.  
  191. });
  192. }
  193. else if (e.getSource() == exitButton) {
  194.  
  195. setVisible(false);
  196. dispose();
  197. }
  198. }
  199.  
  200. }
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement