Hi,


This is my first forum post and I'm very new to both the daniweb and programming community. I've been working on a project for a month or so and it involves clicking JButton to change the values of the text associated with the JButton clicked. Everything runs fine except one button expands to cover the entire screen when I click a button. What am I doing wrong?

Code's right here

// The Driver class

import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import javax.swing.JLabel;
import java.awt.Color;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JCheckBox;
import java.util.Random;
import java.awt.FlowLayout;
import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.Container;
import java.awt.Color;



public class Driver{
	public static void main(String[] args){

		Curriculum Curric = new Curriculum();
   Curric.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   Curric.setLocation(900, 29);
   Curric.setSize(375, 650);
   Curric.setVisible(true);
   Curric.setResizable(false);

	}
}
//end of driver class





//Main class

import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import javax.swing.JLabel;
import java.awt.Color;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JCheckBox;
import java.util.Random;
import java.awt.FlowLayout;
import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.Container;
import java.awt.Color;

public class Curriculum extends JFrame {
// Declare string variables for JButtons
 String Atext = "";
 String Btext = "";
 String Ctext = "";
 String Dtext = "";
//Declare JButtons
 JButton CurriculumIntro = new JButton("");
 JButton A = new JButton(Atext);
 JButton B = new JButton(Btext);
 JButton C = new JButton(Ctext);
 JButton D = new JButton(Dtext);




//Class Constructor
  Curriculum() {
  	super("Quiz Window (2.0!)");
    setSize(899, 679);
    setLocation(200,35);




  //Action Listener Declarations
  CurriculumIntro CI = new CurriculumIntro();
  CurriculumIntro.addActionListener(CI);
  A Aaction = new A();
  A.addActionListener(Aaction);
  B Baction = new B();
  B.addActionListener(Baction);
  C Caction = new C();
  C.addActionListener(Caction);
  D Daction = new D();
  D.addActionListener(Daction);



  //Add initial button/s

  add(CurriculumIntro);
CurriculumIntro.setBounds(100,100,100,100);

  }






//The action listener sets the main buttons bounds to 0 and adds the 4 new buttons
// in their respective places
public class CurriculumIntro implements ActionListener{
  public void actionPerformed(ActionEvent event){
  CurriculumIntro.setBounds(0,0,0,0);

  add(A);
  add(B);
  add(C);
  add(D);
A.setBounds(0,440,184,180);
A.setForeground(Color.magenta);
A.setBackground(Color.cyan);
B.setBounds(184,440,184,180);
B.setForeground(Color.magenta);
B.setBackground(Color.cyan);
C.setBounds(0,260,184,180);
C.setForeground(Color.magenta);
C.setBackground(Color.cyan);
D.setBounds(184,260,184,180);
D.setForeground(Color.magenta);
D.setBackground(Color.cyan);
A.setFont (new Font ("Lindsey", Font.BOLD, 30));
B.setFont (new Font ("Lindsey", Font.BOLD, 30));
C.setFont (new Font ("Lindsey", Font.BOLD, 30));
D.setFont (new Font ("Lindsey", Font.BOLD, 30));
  }}
// Button action listener classes
  public class A implements ActionListener {
  	public void actionPerformed(ActionEvent event){
  		System.out.println("Great job, you just pressed the A button!");
  		A.setText("A test");
  		
  	}
  }

  public class B implements ActionListener {
  	public void actionPerformed(ActionEvent event){
  		System.out.println("Great job, you just pressed the B button!");
  		B.setText("B test");
  	}
  }

    public class C implements ActionListener {
  	public void actionPerformed(ActionEvent event){
  		System.out.println("Great job, you just pressed the C button!");
  		C.setText("C test");
  	}
  }

    public class D implements ActionListener {
  	public void actionPerformed(ActionEvent event){
  		System.out.println("Great job, you just pressed the D button!");
  		D.setText("D test");
  	}
  }
}

I just want to be able to change the text of the 4 buttons on the bottom half of the screen without anything being disoriented. I've tried a number of techniques and nothing has worked so far. :(

It seems like no matter what there's always a larger button in the background. What am I doing wrong here? Help very much appreciated,

Avenger :)

You really need to look at layout managers rather than setting the sizes of the objects directly!

I made a small test using setBounds but couldnt get it to reproduce what you are seeing:

public class Test {
    public static void main(String[] args) {
    	
    	JFrame f = new JFrame();
    	
    	Container c = f.getContentPane();
    	c.setLayout(null);
    	final JButton b = new JButton("someLongText");
    	b.setBounds(50, 50, 200,50);
    	b.addActionListener(new ActionListener(){
    		@Override
			public void actionPerformed(ActionEvent arg0)
			{
				b.setText("otherText");
			}
    	});
    	
    	c.add(b, BorderLayout.CENTER);
    	
    	f.pack();
    	f.setVisible(true);
    }
}

However, really you should be using layout managers - this means things like resizing of the window are handled much better (along with LOTS of other problems you will come across if you use methods like setBounds) take a look at this:

public class Test {
    public static void main(String[] args) {
    	
    	JFrame f = new JFrame();
    	
    	Container c = f.getContentPane();
    	c.setLayout(new FlowLayout(FlowLayout.CENTER));
    	final JButton a = new JButton("someLongText");
    	final JButton b = new JButton("someOtherLongText");
    	
    	a.addActionListener(new ActionListener(){
    		@Override
			public void actionPerformed(ActionEvent arg0)
			{
				a.setText("otherText");
			}
    	});
    	
    	b.addActionListener(new ActionListener(){
    		@Override
			public void actionPerformed(ActionEvent arg0)
			{
				b.setText("otherText!!!");
			}
    	});
    	
    	c.add(a);
    	c.add(b);
    	
    	f.pack();
    	f.setVisible(true);
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.