Results 1 to 8 of 8
- 01-17-2013, 02:19 AM #1
Member
- Join Date
- Jan 2013
- Posts
- 4
- Rep Power
- 0
Absolute Beginner trying to get one frame, with 2 panels.
Hi Guys
I'm completely new to java but love it so far. But at the moment im trying to do something completely frustrating.
I would like, one panel split vertically into two frames..... like looking at a book with the seem down the middle.
Across the top of each panel I would like aset of buttons that when clicked show info on football teams. I would like to use radio buttons, but have the left panel work independantly from the right panel. Ie, so I can select two different teams and do a comparrison.
So far, I have got only one panel, (with the buttons working), but i gant get the split, or the second panel. Do I need ti include co-ordinates or something?
I am using netbeans.... my code is below
(p.s. sorry if I havent used the forum ettiquette but I checked all other threads and none i found helped)
Thanks
/** -----------------------------------------------------------------------
Author : Anthony Barlow
Date Created : 21 December 2012
File Name : H2H.java
Purpose : Assignment 1
--------------------------------------------------------------------------- */
import java.awt.BorderLayout;
import javax.swing.*;
import java.awt.event.*;
class H2H extends JFrame implements ItemListener
{
JPanel panel = new JPanel();
String[] styles = { "Alvescot", "Bampton", "Carterton", "Ducklington", "Eynsham", "FC Hollybush" };
JComboBox box = new JComboBox( styles );
JRadioButton radioButton1 = new JRadioButton( "Alvescot");
JRadioButton radioButton2 = new JRadioButton( "Bampton");
JRadioButton radioButton3 = new JRadioButton( "Carterton");
JRadioButton radioButton4 = new JRadioButton( "Ducklington");
JRadioButton radioButton5 = new JRadioButton( "Eynsham");
JRadioButton radioButton6 = new JRadioButton( "FC Hollybush");
ButtonGroup team = new ButtonGroup();
JTextArea txtArea = new JTextArea( 30, 60 );
JRadioButton radioButton7 = new JRadioButton( "Alvescot");
JRadioButton radioButton8 = new JRadioButton( "Bampton");
JRadioButton radioButton9 = new JRadioButton( "Carterton");
JRadioButton radioButton10 = new JRadioButton( "Ducklington");
JRadioButton radioButton11 = new JRadioButton( "Eynsham");
JRadioButton radioButton12 = new JRadioButton( "FC Hollybush");
ButtonGroup team2 = new ButtonGroup();
JTextArea txtArea2 = new JTextArea( 30, 60 );
public H2H()
{
super("Input window");
setSize( 800,600);
setDefaultCloseOperation( EXIT_ON_CLOSE );
add(panel);
team.add( radioButton1 );
team.add( radioButton2 );
team.add( radioButton3 );
team.add( radioButton4 );
team.add( radioButton5 );
team.add( radioButton6 );
panel.add( radioButton1 );
panel.add( radioButton2 );
panel.add( radioButton3 );
panel.add( radioButton4 );
panel.add( radioButton5 );
panel.add( radioButton6 );
panel.add( box );
panel.add( txtArea );
radioButton1.addItemListener( this );
radioButton2.addItemListener( this );
radioButton3.addItemListener( this );
radioButton4.addItemListener( this );
radioButton5.addItemListener( this );
radioButton6.addItemListener( this );
box.addItemListener( this );
setVisible( true);
}
@Override
public void itemStateChanged( ItemEvent event)
{
if( event.getItemSelectable()==radioButton1) txtArea.setText( " Alvescot "
+ "\n Home Ground: Farm Park, Carterton Road, Alvescot"
+ "\n Manager: Paul Bircher"
+ "\n Current Record: P7, W5, D0, L2, F23 A7 Pts 15 "
+ "\n Leading Scorer: Chris Lawrence"
+ "\n "
+ "\n Titles: 3"
+ "\n Cups Won: 1"
+ "\n Premier Champions: 1999, 2003"
+ "\n Division One Champions: 1998"
+ "\n Junior Shield Winners: 2004"
+ "\n All Time Top Scorer: Ady Hussey (204)"
+ "\n "
+ "\n NOTES"
+ "\n Manager Paul Bircher has been at the helm for 24 years"
+ "\n Steve Bircher, son of Manager Paul, is hot on the heels of all time goal scorer Ady Hussey with 197 goals");
if( event.getItemSelectable()==radioButton2) txtArea.setText( "Bampton"
+ "\n Home Ground: Farm Park, Carterton Road, Alvescot"
+ "\n Manager: Paul Bircher"
+ "\n Current Record: P7, W5, D0, L2, F23 A7 Pts 15 "
+ "\n Leading Scorer: Chris Lawrence"
+ "\n "
+ "\n Titles: 3"
+ "\n Cups Won: 1"
+ "\n Premier Champions: 1999, 2003"
+ "\n Division One Champions: 1998"
+ "\n Junior Shield Winners: 2004"
+ "\n All Time Top Scorer: Ady Hussey (204)"
+ "\n "
+ "\n NOTES");
if( event.getItemSelectable()==radioButton3) txtArea.setText( "Carterton"
+ "\n Home Ground: Farm Park, Carterton Road, Alvescot"
+ "\n Manager: Paul Bircher"
+ "\n Current Record: P7, W5, D0, L2, F23 A7 Pts 15 "
+ "\n Leading Scorer: Chris Lawrence"
+ "\n "
+ "\n Titles: 3"
+ "\n Cups Won: 1"
+ "\n Premier Champions: 1999, 2003"
+ "\n Division One Champions: 1998"
+ "\n Junior Shield Winners: 2004"
+ "\n All Time Top Scorer: Ady Hussey (204)"
+ "\n "
+ "\n NOTES");
if( event.getItemSelectable()==radioButton4) txtArea.setText( "Ducklington"
+ "\n Home Ground: Farm Park, Carterton Road, Alvescot"
+ "\n Manager: Paul Bircher"
+ "\n Current Record: P7, W5, D0, L2, F23 A7 Pts 15 "
+ "\n Leading Scorer: Chris Lawrence"
+ "\n "
+ "\n Titles: 3"
+ "\n Cups Won: 1"
+ "\n Premier Champions: 1999, 2003"
+ "\n Division One Champions: 1998"
+ "\n Junior Shield Winners: 2004"
+ "\n All Time Top Scorer: Ady Hussey (204)"
+ "\n "
+ "\n NOTES");
if( event.getItemSelectable()==radioButton5) txtArea.setText( "Eynsham"
+ "\n Home Ground: Farm Park, Carterton Road, Alvescot"
+ "\n Manager: Paul Bircher"
+ "\n Current Record: P7, W5, D0, L2, F23 A7 Pts 15 "
+ "\n Leading Scorer: Chris Lawrence"
+ "\n "
+ "\n Titles: 3"
+ "\n Cups Won: 1"
+ "\n Premier Champions: 1999, 2003"
+ "\n Division One Champions: 1998"
+ "\n Junior Shield Winners: 2004"
+ "\n All Time Top Scorer: Ady Hussey (204)"
+ "\n "
+ "\n NOTES");
if( event.getItemSelectable()==radioButton6) txtArea.setText( "FC Hollybush"
+ "\n Home Ground: Farm Park, Carterton Road, Alvescot"
+ "\n Manager: Paul Bircher"
+ "\n Current Record: P7, W5, D0, L2, F23 A7 Pts 15 "
+ "\n Leading Scorer: Chris Lawrence"
+ "\n "
+ "\n Titles: 3"
+ "\n Cups Won: 1"
+ "\n Premier Champions: 1999, 2003"
+ "\n Division One Champions: 1998"
+ "\n Junior Shield Winners: 2004"
+ "\n All Time Top Scorer: Ady Hussey (204)"
+ "\n "
+ "\n NOTES");
if( (event.getItemSelectable()== box ) &&
( event.getStateChange()==ItemEvent.SELECTED))
txtArea.append( event.getItem().toString() + " selected");
}
public static void main ( String[] args )
{
H2H gui = new H2H();
}
}
- 01-17-2013, 02:47 AM #2
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
Re: Absolute Beginner trying to get one frame, with 2 panels.
Using absolute positioning is not recommended when creating a swing application. You should use the most suited layout manager to layout the components of your application. For this I suggest you to explore the following documents on layout manager: A Visual Guide to Layout Managers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)
Website: Learn Java by Examples
- 01-17-2013, 03:52 AM #3
Re: Absolute Beginner trying to get one frame, with 2 panels.
Why do they call it rush hour when nothing moves? - Robin Williams
- 01-17-2013, 11:03 AM #4
Member
- Join Date
- Jan 2013
- Posts
- 4
- Rep Power
- 0
- 01-17-2013, 04:03 PM #5
Member
- Join Date
- Dec 2012
- Posts
- 74
- Rep Power
- 0
Re: Absolute Beginner trying to get one frame, with 2 panels.
I suggest that you focus on using a BorderLayout for your JFrame, this will give you 5 areas, NORTH, CENTER, SOUTH, EAST, and WEST. You could put your buttons in a JPanel and put the JPanel in the NORTH. This button panel should probably use a FlowLayout which will just put the buttons where there's room, and will leave their size alone.I suggest you to explore the following documents on layout manager: A Visual Guide to Layout Managers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)
You could have another panel for your two JTextAreas and have this use a GridLayout with 1 row and 2 columns. You could place this JPanel with the text areas in it in the CENTER of the JFrame.
This is how you use layout managers and panels to have Swing automatically position and size your Components.
- 01-17-2013, 04:38 PM #6
Member
- Join Date
- Jan 2013
- Posts
- 4
- Rep Power
- 0
Re: Absolute Beginner trying to get one frame, with 2 panels.
Thanks guys... this is brilliant.
I'm ok with the theory... but its the actual CODE which I struggle with. I'm self taught and don't know the rules of java just yet... I'm really enjoying it though
any help with the code itself would be much appreciatred...
- 01-17-2013, 05:00 PM #7
Member
- Join Date
- Dec 2012
- Posts
- 74
- Rep Power
- 0
Re: Absolute Beginner trying to get one frame, with 2 panels.
In your constructor, for your JFrame, to setup a BorderLayout, you would do:any help with the code itself would be much appreciatred...
setLayout(new BorderLayout());
Since you want to have two panels that have the same radio buttons in them and they each work the same way, it makes sense to make this a subclass of JPanel. This way, you do the programming only once, and you easily have two instances of the panel by instantiating the class twice and add the panels twice to the JFrame.
The above code defines a panel, that will be added to JFrame in Border.CENTER which will in turn have a panel for each Team.Java Code:class TeamsPanel extends JPanel { public TeamsPanel() { setLayout(new GridLayout(1,2)); add(new TeamPanel()); add(new TeamPanel()); } }
The TeamPanel declaration would look like this:
It would define all of the radio buttons or combox box, and it would have a text area in it. And it is where the ItemListener and the itemStateChanged would go. This panel would have a BorderLayout, so that you could put the button panel across the top and put the text area in the center.Java Code:class TeamPanel extends JPanel implements ItemListener { }
I hope that this helps without doing too much for you.
- 01-17-2013, 05:18 PM #8
Member
- Join Date
- Jan 2013
- Posts
- 4
- Rep Power
- 0
Re: Absolute Beginner trying to get one frame, with 2 panels.
Thank you again... I THINK I follow that. Im guessing you're saying I should have one file for the main program and a smaller sub file for the buttons etc?
How much more can I push this and ask for help in how this is done... in stupid person terms as I'm not yet offais with all java speak.
thanks once again..... its already looking a lot better.
Also, I have another question but I'll save that for now :)
Similar Threads
-
Frame with 2 Panels
By collwill in forum New To JavaReplies: 9Last Post: 01-10-2013, 06:49 AM -
Help an absolute beginner for free...
By efth in forum New To JavaReplies: 6Last Post: 05-11-2011, 08:11 AM -
Advice for first step for absolute beginner
By FormerBGIuser in forum New To JavaReplies: 8Last Post: 03-07-2011, 10:54 AM -
Absolute beginner
By duff18 in forum New To JavaReplies: 15Last Post: 02-19-2011, 10:07 AM -
Frame size clipping pixels off right side with Absolute Positioning
By Psyclone in forum New To JavaReplies: 2Last Post: 02-08-2010, 12:03 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks