Results 1 to 2 of 2
  1. #1
    Jononomous is offline Member
    Join Date
    Mar 2008
    Posts
    6
    Rep Power
    0

    Unhappy JLabel and text concept issues

    Basically I want to display the output with a JLabel from the text fields where the data is entered, and when the Calculate button is pressed to display this output. Here is what I have so far but I feel I'm kind of off track a bit.

    ###################



    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

    public class TextFrame extends JFrame implements ActionListener{

    private static final int FRAME_WIDTH = 300;
    private static final int FRAME_HEIGHT = 200;
    private static final int FRAME_X_ORIGIN = 150;
    private static final int FRAME_Y_ORIGIN = 250;

    private static final int BUTTON_WIDTH = 80;
    private static final int BUTTON_HEIGHT = 30;

    private static final double SHIPPING = 0.12;

    private JButton calculateTotal;
    private JLabel prompt;
    private JLabel prompt2;
    private JLabel display;
    private JLabel display2;

    private JTextField inputLine;
    private JTextField inputLine2;
    private JTextField inputLine3;

    public static void main(String[] args) {

    TextFrame frame = new TextFrame();
    frame.setVisible(true);

    }

    public TextFrame() {


    Container contentPane;

    setSize(FRAME_WIDTH, FRAME_HEIGHT);
    setResizable(false);
    setTitle("Project");
    setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);

    contentPane = getContentPane();
    contentPane.setLayout(new FlowLayout());

    prompt = new JLabel( );
    prompt.setText("Enter ID Code: ");
    prompt.setSize(150,25);
    contentPane.add(prompt);

    inputLine = new JTextField( );
    inputLine.setColumns(10);
    contentPane.add(inputLine);

    prompt2 = new JLabel( );
    prompt2.setText("Weight: Pounds | Ounces: ");
    prompt2.setSize(150,25);
    contentPane.add(prompt2);

    inputLine2 = new JTextField( );
    inputLine2.setColumns(5);
    contentPane.add(inputLine2);

    inputLine3 = new JTextField( );
    inputLine3.setColumns(5);
    contentPane.add(inputLine3);

    inputLine.addActionListener(this);
    inputLine2.addActionListener(this);
    inputLine3.addActionListener(this);

    calculateTotal = new JButton("Calculate");
    calculateTotal.setSize(BUTTON_WIDTH, BUTTON_HEIGHT);
    contentPane.add(calculateTotal);

    display = new JLabel( );
    display.setText(" ID Weight Shipping Charge ");
    display.setSize(150,25);
    contentPane.add(display);

    String idcode = inputLine.getText();
    String pounds = inputLine2.getText();
    String ounces = inputLine3.getText();

    display2 = new JLabel( );
    display2.setText(idcode);
    display2.setSize(150,25);
    contentPane.add(display2);



    calculateTotal.addActionListener(this);

    setDefaultCloseOperation(EXIT_ON_CLOSE);

    }

    public void actionPerformed(ActionEvent event) {


    if (event.getSource() instanceof JButton) {
    JButton clickedButton = (JButton) event.getSource();




    } else {
    setTitle("You entered '" + inputLine.getText() + "'");
    }


    }



    }



    Any Help would be very much appreciated.

    Thanks,

    - GoOsE

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class TF extends JFrame implements ActionListener {
        private static final int FRAME_WIDTH = 300;
        private static final int FRAME_HEIGHT = 200;
        private static final int FRAME_X_ORIGIN = 150;
        private static final int FRAME_Y_ORIGIN = 250;
    
        private static final int BUTTON_WIDTH = 80;
        private static final int BUTTON_HEIGHT = 30;
    
        private static final double SHIPPING = 0.12;
    
        private JButton calculateTotal;
        private JLabel prompt;
        private JLabel prompt2;
        private JLabel display;
        private JLabel display2;
    
        private JTextField inputLine;
        private JTextField inputLine2;
        private JTextField inputLine3;
    
        public static void main(String[] args) {
            TF frame = new TF();
            frame.setVisible(true);
        }
    
        public TF() {
            Container contentPane;
    
            setSize(FRAME_WIDTH, FRAME_HEIGHT);
            setResizable(false);
            setTitle("Project");
            setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
    
            contentPane = getContentPane();
            contentPane.setLayout(new FlowLayout());
    
            prompt = new JLabel( );
            prompt.setText("Enter ID Code: ");
            prompt.setSize(150,25);
            contentPane.add(prompt);
    
            inputLine = new JTextField( );
            inputLine.setColumns(10);
            contentPane.add(inputLine);
    
            prompt2 = new JLabel( );
            prompt2.setText("Weight: Pounds | Ounces: ");
            prompt2.setSize(150,25);
            contentPane.add(prompt2);
    
            inputLine2 = new JTextField( );
            inputLine2.setColumns(5);
            contentPane.add(inputLine2);
    
            inputLine3 = new JTextField( );
            inputLine3.setColumns(5);
            contentPane.add(inputLine3);
    
            inputLine.addActionListener(this);
            inputLine2.addActionListener(this);
            inputLine3.addActionListener(this);
    
            calculateTotal = new JButton("Calculate");
            calculateTotal.setSize(BUTTON_WIDTH, BUTTON_HEIGHT);
            contentPane.add(calculateTotal);
    
            display = new JLabel( );
            display.setText(" ID Weight Shipping Charge ");
            display.setSize(150,25);
            contentPane.add(display);
    
    //        String idcode = inputLine.getText();
    //        String pounds = inputLine2.getText();
    //        String ounces = inputLine3.getText();
    
            display2 = new JLabel( );
    //        display2.setText(idcode);
            display2.setSize(150,25);
            contentPane.add(display2);
    
            calculateTotal.addActionListener(this);
    
            setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
    
        public void actionPerformed(ActionEvent event) {
            // If this is all you're going to do in this method then
            // you don't need to find out which component sent the event.
            // You can do this for any event received:
            String idcode = inputLine.getText();
            String pounds = inputLine2.getText();
            String ounces = inputLine3.getText();
            String s = "ID Code: " + idcode + ", " + pounds + " pounds " +
                        ounces + " ounces";
            display2.setText(s);
            
    //        if (event.getSource() instanceof JButton) {
    //            JButton clickedButton = (JButton) event.getSource();
    //        } else {
    //            setTitle("You entered '" + inputLine.getText() + "'");
    //        }
        }
    }

Similar Threads

  1. mail concept
    By indirani in forum New To Java
    Replies: 3
    Last Post: 04-16-2008, 01:30 PM
  2. mail concept
    By thamizhisai in forum Advanced Java
    Replies: 4
    Last Post: 04-11-2008, 07:19 AM
  3. What is RMI concept in Spring Framework
    By Java Tip in forum Spring Framework
    Replies: 0
    Last Post: 04-02-2008, 10:36 AM
  4. JLabel
    By Jack in forum AWT / Swing
    Replies: 2
    Last Post: 07-02-2007, 01:55 PM
  5. JLabel
    By Freddie in forum AWT / Swing
    Replies: 2
    Last Post: 05-29-2007, 02:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •