Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // CustomValidator
- import customValidator.TextFieldValidator;
- import javax.swing.*;
- import java.awt.Color;
- import java.awt.*;
- import java.awt.event.*;
- public class MainFrame extends JFrame {
- // form objects
- private static final int FRAME_WIDTH = 535, FRAME_HEIGHT = 465, FRAME_X = 150, FRAME_Y = 250;
- private JTextField txtOne, txtTwo, txtThree;
- private JButton btnReset, btnOk;
- private JLabel lblHints, lblGuesses, lblTitle;
- private JTextArea txtOutput;
- private JScrollPane pnlScroll;
- private String x;
- private int guesses;
- private TextFieldValidator validator1, validator2, validator3;
- Fermi fermi = new Fermi();
- public static void main(String[] args) {
- MainFrame jframe;
- jframe = new MainFrame();
- jframe.setVisible(true);
- }
- public MainFrame() {
- // constructing JFrame
- this.setTitle("Fermi Guessing Game");
- this.setSize(FRAME_WIDTH, FRAME_HEIGHT);
- this.setLocation(FRAME_X, FRAME_Y);
- this.setResizable(false);
- this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- // -------------------------------------------- construct container
- JPanel contentPanel = new JPanel();
- // set layout manager of JPanel
- contentPanel.setLayout(null);
- // -------------------------------------------- constructing labels
- lblTitle = new JLabel("Fermi Guessing Game");
- lblTitle.setFont(new Font("Arial", Font.BOLD, 14));
- lblTitle.setBounds(22, 19, 156, 16);
- contentPanel.add(lblTitle);
- lblGuesses = new JLabel("Enter your three guesses (0-9):");
- lblGuesses.setFont(new Font("Arial", Font.PLAIN, 14));
- lblGuesses.setBounds(22, 47, 201, 16);
- contentPanel.add(lblGuesses);
- lblHints = new JLabel("Hints:");
- lblHints.setFont(new Font("Arial", Font.PLAIN, 14));
- lblHints.setBounds(253, 47, 61, 16);
- contentPanel.add(lblHints);
- // -------------------------------------------- constructing txtFields
- txtOne = new JTextField();
- txtOne.setBounds(19, 69, 93, 46);
- txtOne.setColumns(10);
- contentPanel.add(txtOne);
- txtTwo = new JTextField();
- txtTwo.setColumns(10);
- txtTwo.setBounds(19, 118, 93, 46);
- contentPanel.add(txtTwo);
- txtThree = new JTextField();
- txtThree.setColumns(10);
- txtThree.setBounds(19, 167, 93, 46);
- contentPanel.add(txtThree);
- // -------------------------------------------- constructing button objects
- btnOk = new JButton("Ok");
- btnOk.setFont(new Font("Arial", Font.PLAIN, 14));
- btnOk.setBounds(19, 216, 93, 46);
- contentPanel.add(btnOk);
- //btnOk action listener
- btnOk.addActionListener((ActionEvent e) -> onOk(e));
- btnReset = new JButton("Reset");
- btnReset.setFont(new Font("Arial", Font.BOLD, 14));
- btnReset.setBounds(19, 370, 93, 46);
- contentPanel.add(btnReset);
- //btnReset action listener
- btnReset.addActionListener((ActionEvent e) -> onReset(e));
- // -------------------------------------------- scrollbar
- pnlScroll = new JScrollPane();
- pnlScroll.setBounds(253, 69, 265, 347);
- contentPanel.add(pnlScroll);
- // -------------------------------------------- constructing txtArea
- txtOutput = new JTextArea();
- pnlScroll.setViewportView(txtOutput);
- txtOutput.setEditable(false);
- this.add(contentPanel);
- // game numbers
- fermi.generateNum();
- resetInput();
- }
- private void onOk(ActionEvent e) {
- Fermi fermi = new Fermi();
- // validator objects
- validator1 = new TextFieldValidator(txtOne);
- validator1.setRegExp("\\d");
- validator2 = new TextFieldValidator(txtTwo);
- validator2.setRegExp("\\d");
- validator3 = new TextFieldValidator(txtThree);
- validator3.setRegExp("\\d");
- // check if textfield inputs follow regExp
- if (validator1.check()) {
- validator1.setErrorColor(Color.RED);
- } if (validator2.check()) {
- validator2.setErrorColor(Color.RED);
- } if (validator3.check()) {
- validator3.setErrorColor(Color.RED);
- }
- int a, b, c;
- // validate textfields
- a = Integer.parseInt(txtOne.getText());
- b = Integer.parseInt(txtTwo.getText());
- c = Integer.parseInt(txtThree.getText());
- //System.out.println(fermi.generateNum());
- System.out.println(a);
- System.out.println(b);
- System.out.println(c);
- // guess counter
- guesses++;
- // clear for next game
- resetInput();
- // calculate hint and output
- //String hint = fermi.hint(a, b, c);
- //txtOutput.append(a + " " + b + " " + c + " : " + hint);
- // winner winner chicken dinner
- // if (hint.equals("Fermi Fermi Fermi")) {
- // txtOne.setEnabled(false);
- // txtTwo.setEnabled(false);
- // txtThree.setEnabled(false);
- // btnOk.setEnabled(false);
- // txtOutput.append("Congratulations! Guesses: " + guesses);
- // }
- }
- private void onReset(ActionEvent e) {
- Fermi fermi = new Fermi();
- // when reset is clicked -- reset game
- //fermi.generateNum();
- resetInput();
- txtOutput.setText("");
- btnOk.setEnabled(true);
- txtOne.setEnabled(true);
- txtTwo.setEnabled(true);
- txtThree.setEnabled(true);
- // reset guess count back to 0
- guesses = 0;
- }
- private void resetInput() {
- // reset all inputs on interface
- x = "";
- txtOne.setText(x);
- txtOne.requestFocusInWindow();
- txtTwo.setText(x);
- txtThree.setText(x);
- txtOutput.setText(x);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement