0

This is my first time programming GUI in JAVA. I'm trying a converter program for my project, but I am not getting any output when I try running the program. My code isn't complete.

package Exams;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.Font;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import javax.swing.JTextPane;
import javax.swing.DropMode;

public class Project {

private JFrame frame;
private JTextField txtEnterText;
JTextArea txtDisplay = new JTextArea(300,300);

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Project window = new Project();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public Project() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.getContentPane().setEnabled(false);
    frame.setBounds(100, 100, 262, 398);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    txtEnterText = new JTextField();
    txtEnterText.setBounds(10, 11, 226, 37);
    txtEnterText.setFont(new Font("Tahoma", Font.PLAIN, 18));
    txtEnterText.setHorizontalAlignment(SwingConstants.RIGHT);
    frame.getContentPane().add(txtEnterText);
    txtEnterText.setColumns(10);

    //ROW 1------------------------------------------------------------------------------

     final JButton btn7 = new JButton("7");
     btn7.setBounds(10, 59, 50, 50);
     btn7.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            String EnterNumber = txtDisplay.getText() + btn7.getText();
            txtDisplay.setText(EnterNumber);
        }
     });
    btn7.setFont(new Font("Tahoma", Font.BOLD, 13));
    frame.getContentPane().add(btn7);

    final JButton btn8 = new JButton("8");
    btn8.setBounds(70, 59, 50, 50);
    btn8.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            String EnterNumber = txtDisplay.getText() + btn8.getText();
            txtDisplay.setText(EnterNumber);
        }
    });
    btn8.setFont(new Font("Tahoma", Font.BOLD, 13));
    frame.getContentPane().add(btn8);

    final JButton btn9 = new JButton("9");
    btn9.setBounds(130, 59, 50, 50);
    btn9.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            String EnterNumber = txtDisplay.getText() + btn9.getText();
            txtDisplay.setText(EnterNumber);
        }
    });
    btn9.setFont(new Font("Tahoma", Font.BOLD, 13));
    frame.getContentPane().add(btn9);

    JButton btndm = new JButton("dm");
    btndm.setBounds(190, 59, 50, 50);
    btndm.setFont(new Font("Tahoma", Font.BOLD, 11));
    frame.getContentPane().add(btndm);

    //ROW 2------------------------------------------------------------------------------

    final JButton btn4 = new JButton("4");
    btn4.setBounds(10, 119, 50, 50);
    btn4.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            String EnterNumber = txtDisplay.getText() + btn4.getText();
            txtDisplay.setText(EnterNumber);
        }
    });
    btn4.setFont(new Font("Tahoma", Font.BOLD, 13));
    frame.getContentPane().add(btn4);

    final JButton btn5 = new JButton("5");
    btn5.setBounds(70, 119, 50, 50);
    btn5.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            String EnterNumber = txtDisplay.getText() + btn5.getText();
            txtDisplay.setText(EnterNumber);
        }
    });
    btn5.setFont(new Font("Tahoma", Font.BOLD, 13));
    frame.getContentPane().add(btn5);

    final JButton btn6 = new JButton("6");
    btn6.setBounds(130, 119, 50, 50);
    btn6.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            String EnterNumber = txtDisplay.getText() + btn6.getText();
            txtDisplay.setText(EnterNumber);
        }
    });
    btn6.setFont(new Font("Tahoma", Font.BOLD, 13));
    frame.getContentPane().add(btn6);

    JButton btncm = new JButton("cm");
    btncm.setBounds(190, 119, 50, 50);
    btncm.setFont(new Font("Tahoma", Font.BOLD, 11));
    frame.getContentPane().add(btncm);

    //ROW 3------------------------------------------------------------------------


    final JButton btn1 = new JButton("1");
    btn1.setBounds(10, 179, 50, 50);
    btn1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            String EnterNumber = txtDisplay.getText() + btn1.getText();
            txtDisplay.setText(EnterNumber);
        }
    });
    btn1.setFont(new Font("Tahoma", Font.BOLD, 13));
    frame.getContentPane().add(btn1);

    final JButton btn2 = new JButton("2");
    btn2.setBounds(70, 179, 50, 50);
    btn2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            String EnterNumber = txtDisplay.getText() + btn2.getText();
            txtDisplay.setText(EnterNumber);
        }
    });
    btn2.setFont(new Font("Tahoma", Font.BOLD, 13));
    frame.getContentPane().add(btn2);

    final JButton btn3 = new JButton("3");
    btn3.setBounds(130, 179, 50, 50);
    btn3.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            String EnterNumber = txtDisplay.getText() + btn3.getText();
            txtDisplay.setText(EnterNumber);
        }
    });
    btn3.setFont(new Font("Tahoma", Font.BOLD, 13));
    frame.getContentPane().add(btn3);

    JButton btnmm = new JButton("mm");
    btnmm.setBounds(190, 179, 50, 50);
    btnmm.setFont(new Font("Tahoma", Font.BOLD, 10));
    frame.getContentPane().add(btnmm);

    //ROW 4---------------------------------------------------------------------- 

    final JButton btn0 = new JButton("0");
    btn0.setBounds(70, 239, 50, 50);
    btn0.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            String EnterNumber = txtDisplay.getText() + btn0.getText();
            txtDisplay.setText(EnterNumber);
        }
    });
    btn0.setFont(new Font("Tahoma", Font.BOLD, 13));
    frame.getContentPane().add(btn0);

    JButton btnkm = new JButton("km");
    btnkm.setBounds(10, 239, 50, 50);
    btnkm.setFont(new Font("Tahoma", Font.BOLD, 11));
    frame.getContentPane().add(btnkm);

    JButton btnmt = new JButton("mt");
    btnmt.setBounds(130, 239, 50, 50);
    btnmt.setFont(new Font("Tahoma", Font.BOLD, 11));
    frame.getContentPane().add(btnmt);

    JButton btn_microm = new JButton("micro_m");
    btn_microm.setBounds(190, 239, 50, 50);
    btn_microm.setFont(new Font("Tahoma", Font.BOLD, 11));
    frame.getContentPane().add(btn_microm);


    //ROW 5-----------------------------------------------------------------------

    JButton btnnm = new JButton("nm");
    btnnm.setBounds(10, 299, 50, 50);
    btnnm.setFont(new Font("Tahoma", Font.BOLD, 11));
    frame.getContentPane().add(btnnm);

    JButton btnmi = new JButton("mi");
    btnmi.setBounds(70, 299, 50, 50);
    btnmi.setFont(new Font("Tahoma", Font.BOLD, 13));
    frame.getContentPane().add(btnmi);

    JButton btnyd = new JButton("yd");
    btnyd.setBounds(130, 299, 50, 50);
    btnyd.setFont(new Font("Tahoma", Font.BOLD, 11));
    frame.getContentPane().add(btnyd);

    JButton btnft = new JButton("ft");
    btnft.setBounds(190, 299, 50, 50);
    btnft.setFont(new Font("Tahoma", Font.BOLD, 11));
    frame.getContentPane().add(btnft);

}
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Rochan
  • 25
  • 1
  • 5
  • Can we see (a) how you are launching this program, and (b) what you mean by "not getting any output"? No output at all seems unusual - do you mean at the console, or in the window you are expecting to open? – halfer Dec 18 '16 at 08:27
  • They mean probably the buttons are not reacting. I compiled the code and it's so – Top Sekret Dec 18 '16 at 08:32
  • 1
    1) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) Now as to the stated problem. There is only one text field. There is no call (at any point) to `txtEnterText.setText(..)`. Why would you expect output in it? – Andrew Thompson Dec 18 '16 at 08:42
  • 3) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. – Andrew Thompson Dec 18 '16 at 08:53

3 Answers3

1
private JTextField txtEnterText;

You did not set any text in txtEnterText, this is why your JTextField is empty. You need to set text in your JTextField txtEnterText.

Example:

txtEnterText.setText("SOME TEXT");
Md. Nasir Uddin Bhuiyan
  • 1,598
  • 1
  • 14
  • 24
1

but I am not getting any output when I try running the program

        String EnterNumber = txtDisplay.getText() + btn0.getText();
        txtDisplay.setText(EnterNumber);

Based on the code above you are trying to add text to your JTextArea, but you never add the JTextArea to the frame, so you never see the output.

So the solution would appear to be to add the text area to the frame and get rid of the text field which you never use.

Other problems with the above code:

  1. Variable names should NOT start with an upper case character. Most of your variable are correct. Be consistent!!!

  2. To add text to a text area you can just use the append(...) method.

Something like:

txtDisplay.append(enterNumber);
camickr
  • 321,443
  • 19
  • 166
  • 288
0

You didn't mean txtDisplay, right? You meant txtEnterText in every event handler. Run it and see numbers being displayed properly.

Top Sekret
  • 748
  • 5
  • 21