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 4 of 4

Thread: button

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default button

    Hi, I try to make Button in separate class.

    I don't undersatnd where to put this part of code
    	panel.add(Button.draw);
    //

    public class Panel
    {
    	public Panel()
    	{
    		// draw panel
     
    		JFrame frame = new JFrame();
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(300, 200);
    		frame.setTitle("Parking simulation panel");
     
    		frame.setLayout(new BorderLayout());
     
    		JPanel panel = new JPanel(new FlowLayout());
    		frame.add(panel); // add panel on the frame
     
    		frame.setVisible(true);
    	}
     
    	public void addOnPanel (JButton button) {
     
    	}
    }
     
     
    public class Button
    {
    	private int ButtonName;
    	private String ButtonDesc;
     
     
    	public Button(int ButtonName, String ButtonDesc) {
    		this.ButtonName = ButtonName;
    		this.ButtonDesc = ButtonDesc;
    		draw(ButtonName);
    	}
     
     
    	public JButton draw(int ButtonName)
    	{
    		JButton button = new JButton();
    		button.setText("some text");
    		button.setBackground(Color.BLUE);
     
    		return button;
    	}
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,809
    My Mood
    Mellow
    Thanks
    177
    Thanked 626 Times in 613 Posts

    Default Re: button

    Just as you created an instance of the JFrame and an instance of the JPanel in the Panel() constructor, you must also create an instance of the Button,

    Button button = new Button(); // using the proper constructor call

    You will then add button to the panel almost as you've indicated in your question, except you'll use the instance button, and 'draw' is not a proper method call. It should be:

    panel.add( button.draw() );

    This is probably an exercise to learn about Swing by playing around with many things at once, but the design and especially the variable naming is very odd. Why don't you find some good tutorials, like those Oracle offers, and work through those?

    If I'm wrong, and this work is being done for an academic assignment, I'd be curious to know what the assignment is to determine if what you're doing is appropriate.

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: button

    I'm trying to write a small program that I thought out myself. I want to have a button "make new button". When I click it, the new object Button should be done and added on the frame. The problem is just with how new button will add yourself on the screen.

    What did you mean "like those Oracle offers"?

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,809
    My Mood
    Mellow
    Thanks
    177
    Thanked 626 Times in 613 Posts

    Default Re: button

    Like those Oracle offers: Swing Tutorials/Threads. There are many others covering most topics in Core Java and several beyond. Ignore the bias in the Swing tutorials towards using the GUIBuilder in Netbeans as the way to learn and use Swing. Keep learning to code Swing "by hand."

    I don't know why new Swing users or budding GUI designers often think it's a cool idea to pop components in and out of their user interfaces. It's not especially pleasant to the user to have interface controls coming and going, but it also complicates the interface for little to no value that I can see. But that's me.

    You'll want to check out a JFrame's default layout and where items are added to that layout by default. You can find these topics covered in the JFrame API or in the Oracle tutorials.

Similar Threads

  1. button overlapping the other button
    By xchan in forum AWT / Java Swing
    Replies: 10
    Last Post: April 23rd, 2013, 10:18 AM
  2. Radio Button Help Please!
    By Rick Sebastian in forum What's Wrong With My Code?
    Replies: 18
    Last Post: March 3rd, 2013, 06:26 PM
  3. using button
    By donzii in forum AWT / Java Swing
    Replies: 1
    Last Post: December 22nd, 2012, 07:13 AM
  4. Button help
    By lf2killer in forum Java Theory & Questions
    Replies: 2
    Last Post: November 10th, 2012, 09:09 AM
  5. button positioning
    By pds8475 in forum Java Theory & Questions
    Replies: 1
    Last Post: January 29th, 2011, 08:35 AM