Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: My GUI will not show up.

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Location
    Morgantown
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default My GUI will not show up.

    I have written this program and I cannot get the GUI to show up. I cannot figure out what is wrong.
    I appreciate any help guys.
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class TestScores extends JFrame
    {
      private JLabel TestScoreLabel,TestWeightLabel;
      private JTextField TST1,TST2,TST3,TST4,TWT1,TWT2,TWT3,TWT4,TA;
      private JButton Calculate,Exit;
     
      private CalculateButtonHandler cbHandler;
      private ExitButtonHandler ebHandler;
     
      public void TestScoresProgram()
      {
        TestScoreLabel = new JLabel("Test Scores",SwingConstants.CENTER);
        TestWeightLabel = new JLabel("Test Weight",SwingConstants.CENTER);
     
        TST1 = new JTextField(10);
        TST2 = new JTextField(10);
        TST3 = new JTextField(10);
        TST4 = new JTextField(10);
        TWT1 = new JTextField(10);
        TWT2 = new JTextField(10);
        TWT3 = new JTextField(10);
        TWT4 = new JTextField(10);
        TA = new JTextField(20);
     
        Calculate = new JButton("Calculate");
        cbHandler = new CalculateButtonHandler();
        Calculate.addActionListener(cbHandler);
     
        Exit =  new JButton("Exit");
        ebHandler = new ExitButtonHandler();
        Exit.addActionListener(ebHandler);
     
        setTitle("Test Average Calculater");
     
        Container pane = getContentPane();
     
        pane.setLayout(new GridLayout(7,2));
     
        pane.add(TestScoreLabel);
        pane.add(TestWeightLabel);
        pane.add(TST1);
        pane.add(TWT1);
        pane.add(TST2);
        pane.add(TWT2);
        pane.add(TST3);
        pane.add(TWT3);
        pane.add(TST4);
        pane.add(TWT4);
        pane.add(TA);
        pane.add(Calculate);
        pane.add(Exit);
     
        pane.setSize(400,500);
        pane.setVisible(true);
      }
     
      private class CalculateButtonHandler implements ActionListener
      {
        public void actionPerformed(ActionEvent e)
        {
          double TS1,TS2,TS3,TS4;
          double TW1,TW2,TW3,TW4,Ave;
          TS1 = Double.parseDouble(TST1.getText());
          TS2 = Double.parseDouble(TST2.getText());
          TS3 = Double.parseDouble(TST3.getText());
          TS4 = Double.parseDouble(TST4.getText());
          TW1 = Double.parseDouble(TWT1.getText());
          TW2 = Double.parseDouble(TWT2.getText());
          TW3 = Double.parseDouble(TWT3.getText());
          TW4 = Double.parseDouble(TWT4.getText());
          Ave = (TS1 * TW1) + (TS2 * TW2) + (TS3 * TW3) + (TS4 * TW4);
          TA.setText("Your test average is " + Ave);
        }
      }
     
      private class ExitButtonHandler implements ActionListener
      {
        public void actionPerformed(ActionEvent e)
        {
          System.exit(0);
        }
      }
     
      public static void main(String[] args)
      {
        TestScores testObject = new TestScores();
      }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: My GUI will not show up.

    So annoying. Variable names and method names begin with lowercase letters. Aren't they teaching this anymore?

    Your GUI is built in TestScoresProgram(). I'd like to know why, but I'd also like to know where that method is called. I don't know that that solves all of your problems, but it's a good start.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Location
    Morgantown
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: My GUI will not show up.

    Quote Originally Posted by GregBrannon View Post
    So annoying. Variable names and method names begin with lowercase letters. Aren't they teaching this anymore?

    Your GUI is built in TestScoresProgram(). I'd like to know why, but I'd also like to know where that method is called. I don't know that that solves all of your problems, but it's a good start.
    Sorry that's just how it is written in my book. And when I try to call the TestScoresProgram() it gives me an error that TestScoresProgram cannot be resolved to a type.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: My GUI will not show up.

    Show how you're calling it.

    And if this is how the program is written in your book, including the name of the class not matching any call to instantiate it, the lack of a constructor, the variable names that don't follow Java's naming conventions, and the main() method that doesn't run the Swing GUI on the EDT, I recommend you throw this book away and get a new one.

    With very minor corrections (most commented), this is how it should be done except for variable names. You can and should fix those. Still throw the book away.
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    // the filename in which this source code is saved should be
    // TestScoresProgram.java
    public class TestScoresProgram extends JFrame
    {
        private JLabel TestScoreLabel, TestWeightLabel;
        private JTextField TST1, TST2, TST3, TST4, TWT1, TWT2, TWT3, TWT4, TA;
        private JButton Calculate, Exit;
     
        private CalculateButtonHandler cbHandler;
        private ExitButtonHandler ebHandler;
     
        // the default, no-argument constructor
        public TestScoresProgram()
        {
            TestScoreLabel = new JLabel( "Test Scores", SwingConstants.CENTER );
            TestWeightLabel = new JLabel( "Test Weight", SwingConstants.CENTER );
            TST1 = new JTextField( 10 );
            TST2 = new JTextField( 10 );
            TST3 = new JTextField( 10 );
            TST4 = new JTextField( 10 );
            TWT1 = new JTextField( 10 );
            TWT2 = new JTextField( 10 );
            TWT3 = new JTextField( 10 );
            TWT4 = new JTextField( 10 );
            TA = new JTextField( 20 );
            Calculate = new JButton( "Calculate" );
            cbHandler = new CalculateButtonHandler();
            Calculate.addActionListener( cbHandler );
            Exit =  new JButton( "Exit" );
            ebHandler = new ExitButtonHandler();
            Exit.addActionListener( ebHandler );
            setTitle( "Test Average Calculater" );
            Container pane = getContentPane();
            pane.setLayout( new GridLayout( 7, 2 ) );
            pane.add( TestScoreLabel );
            pane.add( TestWeightLabel );
            pane.add( TST1 );
            pane.add( TWT1 );
            pane.add( TST2 );
            pane.add( TWT2 );
            pane.add( TST3 );
            pane.add( TWT3 );
            pane.add( TST4 );
            pane.add( TWT4 );
            pane.add( TA );
            pane.add( Calculate );
            pane.add( Exit );
     
            // notice these methods are applied to the JFrame, not the
            // Container pane
            pack();
            setVisible( true );
     
        }
     
        private class CalculateButtonHandler implements ActionListener
        {
            public void actionPerformed( ActionEvent e )
            {
                double TS1, TS2, TS3, TS4;
                double TW1, TW2, TW3, TW4, Ave;
                TS1 = Double.parseDouble( TST1.getText() );
                TS2 = Double.parseDouble( TST2.getText() );
                TS3 = Double.parseDouble( TST3.getText() );
                TS4 = Double.parseDouble( TST4.getText() );
                TW1 = Double.parseDouble( TWT1.getText() );
                TW2 = Double.parseDouble( TWT2.getText() );
                TW3 = Double.parseDouble( TWT3.getText() );
                TW4 = Double.parseDouble( TWT4.getText() );
                Ave = ( TS1 * TW1 ) + ( TS2 * TW2 ) + ( TS3 * TW3 ) + ( TS4 * TW4 );
                TA.setText( "Your test average is " + Ave );
            }
        }
     
        private class ExitButtonHandler implements ActionListener
        {
            public void actionPerformed( ActionEvent e )
            {
                System.exit( 0 );
            }
        }
     
        // runs the GUI on the EDT
        public static void main( String[] args )
        {
            SwingUtilities.invokeLater( new Runnable()
            {
                public void run()
                {
                    new TestScoresProgram();
                }
            } );
        }
    }

  5. #5
    Junior Member
    Join Date
    Nov 2013
    Location
    Morgantown
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: My GUI will not show up.

    I am calling it this way
    public static void main(String[] args)
      {
        TestScoresProgram test = new TestScoresProgram();
      }

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: My GUI will not show up.

    In case the update is not obvious, so my update to post #4.

    And as for your latest post, of course you can't declare the variable test to be of type TestScoresProgram when TestScoresProgram doesn't exist as a class. It's a method, not a type. And why declare a variable 'test' at all? It's not used, so why bother? I would show you how it could be done with your current code, but I think it would be counter productive. Instead, your code should be modified as I've done above in post #4.

Similar Threads

  1. jdbc integration with Gui to show user information retrieved from mysql
    By harem_najat in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 22nd, 2013, 07:25 PM
  2. Problems getting floating point number to show in GUI...
    By Eclecstatic in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 18th, 2012, 11:48 PM
  3. New frame should only show GUI once!
    By gerre in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 30th, 2012, 10:34 PM
  4. Beginner: Show Image in Label when Results Show Up
    By Big Bundy in forum Java Theory & Questions
    Replies: 3
    Last Post: April 4th, 2011, 02:43 PM