Hello,

I have created a simple frame, and in it there are 3 panels.I have a button in the bottom panel, and when pressed should display the text "You pressed the button", in the mainPanel.However this is not working.Can anyone shed some light as to what I am doing wrong as everything looks in order to me.

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.util.Random;


public class GUI extends JFrame implements ActionListener {

	
	private JPanel buttonPanel;
	private JPanel mainPanel;
	private JButton press;
	


	
	
	
	GUI(){
		
		setTitle("Simple Frame");
		setSize(649,480);
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		setContentPane(createPane());
		
		setVisible(true);
		
	
	
	}
	
	private Container createPane() {
	
		mainPanel = new JPanel();
		mainPanel.setLayout(new BorderLayout());
		mainPanel.setBackground(Color.GREEN);
		
		mainPanel.add(createButtonPanel(), BorderLayout.SOUTH);
	
		
		return mainPanel;
	
	}
	
	

	
	
	
	public Container createButtonPanel() {
	
		buttonPanel = new JPanel();
		buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		
		// Action listener added to button.
		press = new JButton("Press me");
		press.addActionListener(this);
		
		buttonPanel.add(deal);
		
		return buttonPanel;	
	}
	
	
	
	
	// The actionPerformed method.
	public void actionPerformed(ActionEvent e){
	
		if(e.getSource() == deal) {
			// add the text(JLabel) to the main panel, pane.
			JLabel text = new JLabel("You pressed button!");
			mainPanel.add(text);
			
			//I have tried a variation of this without the following line uncommented out.
			//setContentPane(createPane());
		}	
	}
	
	
	

}

Recommended Answers

All 2 Replies

setContentPane(createPane()); I don't think this is necessary, but it's probably not causing your problem.


Here's your actionPerformed.

public void actionPerformed(ActionEvent e){
 		if(e.getSource() == deal) {
			// add the text(JLabel) to the main panel, pane.
			JLabel text = new JLabel("You pressed button!");
			mainPanel.add(text);
 			
	}	
}

First thing I'd do if I were trying to track this down would be to make sure this is actually being called. Put a distinctive println in the if{} body - "actionPerformed: getSource==deal" or something like that, so you know exactly what it's telling you when you see it on the console. (ie, avoid generic messages like "testing", which tell you little when there's a lot of them)

That would give me two possible sources of error: either the JLabel is being added, or it isn't.
If it isn't, you'd want to go and look at why your getSource()==deal isn't coming back true.

If not, you'd want to look at why just adding a JLabel to a JPanel doesn't change the look of the JPanel. Is there anything else that you need to do to tell the JPanel to redraw itself?
(look over the JPanel API for help here)

I don't think this code will compile. What is "deal"? It looks like your button is called press.

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.