0

Like I said the JButton GR is set to the default size (size of window) when I click JButton MN. When the program is started the JButton GR has the right size (200 by 20), when clicked the menu button appears also at the right size (200 by 20), but when the menu button is clicked the GR JButton is at its default size. When the full size GR JButton is clicked the Menu button reappears with the right size. I'm using BlueJ (school dose not allow other IDEs).

import java.util.Scanner;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JButton;
public class MAIN
{
    public static void main(String args[])
    {
        ActionClass actionEvent = new ActionClass();

        //Main window
        JFrame Program1 = new JFrame("Program1");
        Program1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Program1.setPreferredSize(new Dimension(800, 600));
        Program1.pack();
        Program1.setVisible(true);

        //menu button (returns to home Menu)
        JButton MN = new JButton("MENU");
        MN.setBounds(300, 10, 200, 20);
        MN.setVisible(false);
        Program1.add (MN);
        //MN.setActionCommand("1");

        // Enter GRC
        JButton GR = new JButton("GRC");
        GR.setBounds(300, 40, 200, 20);
        GR.setVisible(true);
        Program1.add (GR);
        //GR.setActionCommand("2");

        GR.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent GRH)
            {
                MN.setVisible(true);
                GR.setVisible(false);
            }
        }
        );

        MN.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent MNH)
            {
                MN.setVisible(false);
                GR.setVisible(true);
            }
        }
        );

    }
}
Blip
  • 3,061
  • 5
  • 22
  • 50
  • 1) Java GUIs have to work on different OS', screen size, screen resolution etc. 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) 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 May 01 '15 at 07:02
  • `MN.setVisible(true); GR.setVisible(false);` Use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) as shown in [this answer](http://stackoverflow.com/a/5786005/418556). – Andrew Thompson May 01 '15 at 07:04
  • 1
    JButtons MN and GR should be declared as local variables or missing final .... – mKorbel May 01 '15 at 07:04
  • 1
    For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). And as pointed out by @mKorbel, that code example won't compile as written! Don't post 'something like' the code being used! – Andrew Thompson May 01 '15 at 07:06
  • visibility should be last code line in class – mKorbel May 01 '15 at 07:07
  • So that means i have to rewrite the program using layout managers, and layout padding and borders? Or is there a way i can just fix this bug and use it only on one OS? – BEN2006 May 01 '15 at 07:19

1 Answers1

2

Like you said the Jbutton GR is ...

  • JFrame has BorderLayout as default LayoutManager in API,

  • use Java naming conventions

  • use LayoutManager instead of NullLayout and wrong way

  • setVisible(true); should be last code line, because your are in risk that all JComponents are added to already visible container (e.g. mouse over repainting those JComponents)

  • use Initial Thread


  • simplest as is possible

enter image description here

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

public class Main {

    private JFrame myProgram1 = new JFrame("myProgram1");
    private JButton myMN = new JButton("MENU");
    private JButton myGR = new JButton("myGRC");

    public Main() {
        myGR.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent myGRH) {
                myMN.setVisible(true);
                myGR.setVisible(false);
            }
        });
        myProgram1.add(myMN, BorderLayout.SOUTH);
        myMN.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent myMNH) {
                myMN.setVisible(false);
                myGR.setVisible(true);
            }
        });
        myProgram1.add(myGR, BorderLayout.NORTH);
        myProgram1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myProgram1.pack();
        myProgram1.setVisible(true);
    }

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Main();
            }
        });
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319