Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 7th, 2012  |  syntax: None  |  size: 2.24 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. public class NumberGuess extends JFrame implements ActionListener
  6. {
  7.         private static final long serialVersionUID = 1L;
  8.        
  9.         private JTextField txtGuess;
  10.         private JButton btnExit;
  11.         private JButton btnGuess;
  12.         private JLabel lblLastGuess;
  13.  
  14.         private NumberGuess() {
  15.                 super("Number Guess!");
  16.                 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  17.                 this.setLayout(new GridBagLayout());
  18.                 this.setLocationByPlatform(true);
  19.                
  20.                 JPanel mainPanel = new JPanel(new GridBagLayout());
  21.                 mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
  22.                
  23.                 txtGuess = new JTextField("");
  24.                 btnExit = new JButton("Exit");
  25.                 btnExit.addActionListener(this);
  26.                 btnGuess = new JButton("Guess!");
  27.                 btnGuess.addActionListener(this);
  28.                 lblLastGuess = new JLabel("You guessed: Nothing yet");
  29.                
  30.                 JPanel buttonPanel = new JPanel();
  31.                 buttonPanel.add(btnExit);
  32.                 buttonPanel.add(btnGuess);
  33.                
  34.                 this.addItem(mainPanel, new JLabel("Enter a guess: "), 0, 0, 1, 1);
  35.                 this.addItem(mainPanel, txtGuess, 1, 0, 2, 1);
  36.                 this.addItem(mainPanel, lblLastGuess, 0, 1, 3, 1);
  37.                 this.addItem(mainPanel, buttonPanel, 1, 2, 2, 1);
  38.                
  39.                 this.getRootPane().setDefaultButton(btnGuess);
  40.                 this.setContentPane(mainPanel);
  41.                 this.pack();
  42.                 this.setVisible(true);
  43.         }
  44.        
  45.         public void actionPerformed(ActionEvent e) {
  46.                 if(e.getSource() == btnExit)
  47.                         System.exit(0);
  48.                 else
  49.                         lblLastGuess.setText("You guessed: " + txtGuess.getText());
  50.         }
  51.        
  52.         public static void main(String[] args) {
  53.                 SwingUtilities.invokeLater(new Runnable() {
  54.                         public void run() {
  55.                                 try{ UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); }
  56.                                 catch(Exception e){}
  57.                                 new NumberGuess();
  58.                         }
  59.                 });
  60.         }
  61.        
  62.         private void addItem(Container p, Component c, int x, int y, int width, int height) {
  63.                 GridBagConstraints gc = new GridBagConstraints();
  64.                 gc.gridx = x;
  65.                 gc.gridy = y;
  66.                 gc.gridwidth = width;
  67.                 gc.gridheight = height;
  68.                 gc.ipadx = 1;
  69.                 gc.ipady = 1;
  70.                
  71.                 if(c instanceof JTextField) {
  72.                         gc.fill = GridBagConstraints.BOTH;
  73.                         gc.weightx = 1.0;
  74.                 }else if( c instanceof JPanel )
  75.                         gc.anchor = GridBagConstraints.EAST;
  76.                 else
  77.                         gc.anchor = GridBagConstraints.WEST;
  78.                
  79.                 p.add(c, gc);
  80.         }
  81. }