Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
so i'm learning java and i was trying to make a button.
the problem is that whenever i create a button it is as big as the screen.
can someone fix this please ?

Main.java:
Java
package com.softbite.library.TextBasedGame;

import javax.swing.JFrame;


public class Frame  {

	
	// method to create a frame
	public void createframe(JFrame frame) {
		
	 frame.setSize( 300, 300 );
	 frame.setVisible( true );
	
	  
	}



}


Frame.java:
Java
package com.softbite.library.TextBasedGame;

import javax.swing.JFrame;


public class Frame  {

	
	// method to create a frame
	public void createframe(JFrame frame) {
		
	 frame.setSize( 300, 300 );
	 frame.setVisible( true );
	
	  
	}



}


NewButton.java

Java
package com.softbite.library.TextBasedGame;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import javax.swing.JButton;
import javax.swing.JComponent;


public class NewButton extends JComponent implements MouseMotionListener, ActionListener {

	
	JButton Button;
	
	public NewButton(String message) {
		
	Button = new JButton(message);
	 setLayout( new FlowLayout() );
	 add( Button );
	 Button.addActionListener( this );
	 addMouseMotionListener( this );
	 

	 //JPanel layout
	    Button.setLayout(null);

	}
	
	public void actionPerformed( ActionEvent e ) {
		 if ( e.getSource() == Button )	 
		
			 
		 	 Button.setVisible(false);
		 
		 }
		 
	// change location of button
	public void ButtonLocation( int x,int z ) {
		
		
		Button.setLocation(x,z);
		
		
		
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

}


What I have tried:

i searched on google but i did not find an solution.
Posted
Updated 6-Dec-17 11:24am

1 solution

Your button is created without any constraining controls or size so it will fill the available space. No idea what the other two classes are for. Take a look at How to Use Buttons, Check Boxes, and Radio Buttons (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900