fork download
  1. /* package whatever; // don't place package name! */
  2. import java.awt.*;
  3.  
  4. import javax.swing.*;
  5. import javax.swing.event.*;
  6.  
  7. import java.util.Calendar;
  8. import java.util.Date;
  9.  
  10. import java.util.*;
  11. import java.lang.*;
  12. import java.io.*;
  13.  
  14. /* Name of the class has to be "Main" only if the class is public. */
  15. class Ideone extends JFrame
  16. {
  17. JLabel nameLabel, streetLabel, stateLabel, dateLabel,
  18. ageLabel, sexLabel, optionsLabel, aboutLabel;
  19. JTextField nameText, streetText;
  20. JComboBox stateList;
  21. JSpinner dateSpin;
  22. JSlider ageSlider;
  23. JRadioButton maleRadio, femaleRadio;
  24. ButtonGroup sexGroup;
  25. JCheckBox morningCheck, afterNCheck, eveningCheck;
  26. JTextArea aboutYou;
  27.  
  28.  
  29. public static void main(String[] args){
  30.  
  31. new Ideone();
  32.  
  33. }
  34.  
  35. public Ideone(){
  36.  
  37. // Create the frame, position it and handle closing it
  38.  
  39. this.setLocationRelativeTo(null);
  40. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  41. this.setTitle("Random Layout");
  42.  
  43. JPanel thePanel = new JPanel();
  44.  
  45. thePanel.setLayout(new GridBagLayout());
  46.  
  47. nameLabel = new JLabel(" Name:");
  48.  
  49. addComp(thePanel, nameLabel, 0, 0, 1, 1, GridBagConstraints.EAST, GridBagConstraints.NONE);
  50.  
  51. nameText = new JTextField(30);
  52.  
  53. addComp(thePanel, nameText, 1, 0, 2, 1, GridBagConstraints.WEST, GridBagConstraints.NONE);
  54.  
  55. streetLabel = new JLabel(" Street:");
  56.  
  57. addComp(thePanel, streetLabel, 0, 1, 1, 1, GridBagConstraints.EAST, GridBagConstraints.NONE);
  58.  
  59. streetText = new JTextField(30);
  60.  
  61. addComp(thePanel, streetText, 1, 1, 2, 1, GridBagConstraints.WEST, GridBagConstraints.NONE);
  62.  
  63. // Create a set of radio buttons and wrap them in a group
  64.  
  65. Box sexBox = Box.createVerticalBox();
  66. maleRadio = new JRadioButton("Male");
  67. femaleRadio = new JRadioButton("Female");
  68. ButtonGroup sexGroup = new ButtonGroup();
  69. sexGroup.add(maleRadio);
  70. sexGroup.add(femaleRadio);
  71. sexBox.add(maleRadio);
  72. sexBox.add(femaleRadio);
  73. sexBox.setBorder(BorderFactory.createTitledBorder("Sex"));
  74. addComp(thePanel, sexBox, 3, 0, 2, 1, GridBagConstraints.WEST, GridBagConstraints.NONE);
  75.  
  76. // Create a flow layout panel and space components 10px apart
  77.  
  78. JPanel statePanel = new JPanel();
  79. statePanel.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 0));
  80.  
  81. stateLabel = new JLabel("State");
  82.  
  83. statePanel.add(stateLabel);
  84.  
  85. // Create a combo box that will hold the states
  86.  
  87. String[] states = {"PA", "NY", "OH", "WV", "NJ"};
  88.  
  89. stateList = new JComboBox(states);
  90.  
  91. statePanel.add(stateList);
  92.  
  93. dateLabel = new JLabel("Appointment Date");
  94.  
  95. statePanel.add(dateLabel);
  96.  
  97. // Get todays date
  98.  
  99. Date todaysDate = new Date();
  100.  
  101. // Create a date spinner & set default to today, no minimum, or max
  102. // Increment the days on button presses
  103. // Can also increment YEAR, MONTH, or DAY_OF_MONTH
  104.  
  105. dateSpin = new JSpinner(new SpinnerDateModel(todaysDate, null, null,
  106. Calendar.DAY_OF_MONTH));
  107.  
  108. // DateEditor is an editor that handles displaying & editing the dates
  109.  
  110. JSpinner.DateEditor dateEditor = new JSpinner.DateEditor(dateSpin, "dd/MM/yy");
  111. dateSpin.setEditor(dateEditor);
  112.  
  113. statePanel.add(dateSpin);
  114.  
  115. ageLabel = new JLabel("Age: 50");
  116.  
  117. statePanel.add(ageLabel);
  118.  
  119. // Creates a slider with a min value of 1 thru 100
  120. // and an initial value of 1
  121.  
  122. ageSlider = new JSlider(1, 99, 50);
  123.  
  124. // Create an instance of ListenForEvents to handle events
  125.  
  126. ListenForSlider lForSlider = new ListenForSlider();
  127.  
  128. // Tell Java that you want to be alerted when an event
  129. // occurs on the slider
  130.  
  131. ageSlider.addChangeListener(lForSlider);
  132.  
  133. statePanel.add(ageSlider);
  134.  
  135. addComp(thePanel, statePanel, 1, 2, 5, 1, GridBagConstraints.EAST, GridBagConstraints.NONE);
  136.  
  137. // Create check boxs and assign them to a group
  138.  
  139. JCheckBox morningCheck, afterNCheck, eveningCheck;
  140.  
  141. Box optionBox = Box.createVerticalBox();
  142. morningCheck = new JCheckBox("Morning");
  143. afterNCheck = new JCheckBox("Afternoon");
  144. eveningCheck = new JCheckBox("Evening");
  145.  
  146. optionBox.add(morningCheck);
  147. optionBox.add(afterNCheck);
  148. optionBox.add(eveningCheck);
  149. optionBox.setBorder(BorderFactory.createTitledBorder("Time of Day"));
  150. addComp(thePanel, optionBox, 1, 3, 2, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE);
  151.  
  152. // Create a text area that is 6 columns high and 40 long
  153.  
  154. aboutYou = new JTextArea(6, 40);
  155.  
  156. // Set the default text for the text area
  157.  
  158. aboutYou.setText("Tell us something about you");
  159.  
  160. // If text doesn't fit on a line, jump to the next
  161.  
  162. aboutYou.setLineWrap(true);
  163.  
  164. // Makes sure that words stay intact if a line wrap occurs
  165.  
  166. aboutYou.setWrapStyleWord(true);
  167.  
  168. // Adds scroll bars to the text area ----------
  169.  
  170. JScrollPane scrollbar1 = new JScrollPane(aboutYou, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  171.  
  172. // Other options: VERTICAL_SCROLLBAR_ALWAYS, VERTICAL_SCROLLBAR_NEVER
  173.  
  174. addComp(thePanel, scrollbar1, 2, 3, 3, 1, GridBagConstraints.EAST, GridBagConstraints.NONE);
  175.  
  176. this.add(thePanel);
  177.  
  178. // Adjusts the size of the frame to best work for the components
  179.  
  180. this.pack();
  181.  
  182. this.setVisible(true);
  183.  
  184. // Define if the user can resize the frame (true by default)
  185. this.setResizable(false);
  186.  
  187. }
  188.  
  189. // Sets the rules for a component destined for a GridBagLayout
  190. // and then adds it
  191.  
  192. private void addComp(JPanel thePanel, JComponent comp, int xPos, int yPos, int compWidth, int compHeight, int place, int stretch){
  193.  
  194. GridBagConstraints gridConstraints = new GridBagConstraints();
  195.  
  196. gridConstraints.gridx = xPos;
  197. gridConstraints.gridy = yPos;
  198. gridConstraints.gridwidth = compWidth;
  199. gridConstraints.gridheight = compHeight;
  200. gridConstraints.weightx = 100;
  201. gridConstraints.weighty = 100;
  202. gridConstraints.insets = new Insets(5,5,5,5);
  203. gridConstraints.anchor = place;
  204. gridConstraints.fill = stretch;
  205.  
  206. thePanel.add(comp, gridConstraints);
  207.  
  208. }
  209.  
  210. // Implements ActionListener so it can react to events on components
  211.  
  212. private class ListenForSlider implements ChangeListener{
  213.  
  214. // Called when the spinner is changed
  215. public void stateChanged(ChangeEvent e) {
  216.  
  217. // Check if the source of the event was the button
  218.  
  219. if(e.getSource() == ageSlider){
  220.  
  221. // Change the value for the label next to the spinner
  222.  
  223. ageLabel.setText("Age: " + ageSlider.getValue() );
  224.  
  225.  
  226. }
  227.  
  228. }
  229.  
  230.  
  231.  
  232. }
  233. }
Runtime error #stdin #stdout #stderr 0.21s 2846720KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Exception in thread "main" java.awt.HeadlessException: 
No X11 DISPLAY variable was set, but this program performed an operation which requires it.
	at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:204)
	at java.awt.Window.<init>(Window.java:536)
	at java.awt.Frame.<init>(Frame.java:420)
	at java.awt.Frame.<init>(Frame.java:385)
	at javax.swing.JFrame.<init>(JFrame.java:189)
	at Ideone.<init>(Main.java:35)
	at Ideone.main(Main.java:31)