3 Replies - 702 Views - Last Post: 01 November 2009 - 01:09 PM Rate Topic: -----

#1 kingofleinster   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 09-December 08

How to put gui into methods()

Posted 01 November 2009 - 07:54 AM

Hi Folks

Hope you can help , What would be the easiest way of putting the code below into methods .
import java.awt.*;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import javax.swing.*;







public class guessbox3 extends JFrame implements ActionListener  {









  private JButton[] buttons = new JButton[8];

  

  private int gscore = 0;



  private int counter  =0;



  private boolean b0select=false,b1select=false,b2select=false,b3select=false,b4select=false,b5select=false,b6select=false,b7select=false;



	guessbox3() {



		super("The Guessing Game");





		Container g = getContentPane();





		JPanel score = new JPanel();

		JLabel sc = new JLabel();

		sc.setText("Your Score :"+gscore);

		score.add(sc);







		JPanel buttonspanel = new JPanel();

			Icon icon = new ImageIcon("q.png");









						for(int i = 0; i < buttons.length; i++){



							buttons[i] = new JButton("?");

							buttons[i].addActionListener(this);



							buttonspanel.add(buttons[i]);

						  }











		JPanel reset = new JPanel();



			JButton resetbut = new JButton("reset");



			reset.add(resetbut);



		g.setLayout(new GridLayout(1,2));

		g.add(score);

		g.add(buttonspanel);

		g.add(reset);



		setSize(500,400);

		setVisible(true);



	}





public static void main(String [] args){



	guessbox3 gbox = new guessbox3();





	}



Any help be much appreciated

Is This A Good Question/Topic? 0
  • +

Replies To: How to put gui into methods()

#2 cdog5000   User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 79
  • Joined: 31-October 09

Re: How to put gui into methods()

Posted 01 November 2009 - 08:08 AM

View Postkingofleinster, on 1 Nov, 2009 - 06:54 AM, said:

Hi Folks

Hope you can help , What would be the easiest way of putting the code below into methods .
import java.awt.*;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import javax.swing.*;







public class guessbox3 extends JFrame implements ActionListener  {









  private JButton[] buttons = new JButton[8];

  

  private int gscore = 0;



  private int counter  =0;



  private boolean b0select=false,b1select=false,b2select=false,b3select=false,b4select=false,b5select=false,b6select=false,b7select=false;



	guessbox3() {



		super("The Guessing Game");





		Container g = getContentPane();





		JPanel score = new JPanel();

		JLabel sc = new JLabel();

		sc.setText("Your Score :"+gscore);

		score.add(sc);







		JPanel buttonspanel = new JPanel();

			Icon icon = new ImageIcon("q.png");









						for(int i = 0; i < buttons.length; i++){



							buttons[i] = new JButton("?");

							buttons[i].addActionListener(this);



							buttonspanel.add(buttons[i]);

						  }











		JPanel reset = new JPanel();



			JButton resetbut = new JButton("reset");



			reset.add(resetbut);



		g.setLayout(new GridLayout(1,2));

		g.add(score);

		g.add(buttonspanel);

		g.add(reset);



		setSize(500,400);

		setVisible(true);



	}





public static void main(String [] args){



	guessbox3 gbox = new guessbox3();





	}



Any help be much appreciated

It would be dumb to split that into methods IMO. Swing should never be split into methods. One thing you could do is instead of putting all the creation of the UI inside the constructor just put it into a createComponents() method. Make sure it's not static though.
Was This Post Helpful? 0
  • +
  • -

#3 ts230   User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 225
  • Joined: 11-July 09

Re: How to put gui into methods()

Posted 01 November 2009 - 08:28 AM

You could make methods that each do a specific thing, like initMenuBar(), initControls(), initToShow(), showFrame()
I use it in a code file with just about 2000 lines, and I can find most of my things in there every easily.
Was This Post Helpful? 0
  • +
  • -

#4 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: How to put gui into methods()

Posted 01 November 2009 - 01:09 PM

and please suppress useless blank lines... it takes an eternity to scroll down your code
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1