![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Expert Programmer
|
1st Java Project...Still
[php]
import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.JPanel; public class lettercount { public static void main(String args[]) { Panel pan=new Panel(); pan.setLayout(new BorderLayout()); // STUPID LAYOUT JFrame app=new JFrame(); JButton fileButton=new JButton("Scan File"); char[] letterArray=new char[26]; int[] letterCount=new int[26]; int count=0; for(int i=97;i<=122;i++) { letterArray[count]=(char)i; //System.out.printf("%d = %s\n",count,letterArray[count]); count++; } app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); app.add(pan); //app.add(fileButton); app.setSize(450,250); app.setVisible(true); } } [/php] Im am completely clueless on how to implement this BorderLayout thing so that i can manage where my buttons and other components are being placed. [php] Panel pan=new Panel(); pan.setLayout(new BorderLayout()); [/php] these 2 lines of code are my main problem.. do i have to make my class extend JPanel??? Im lost.... I would like to add that im using the API page from sun as my reference.
__________________
"When in Rome, Do as the Romans Do" "Beauty is in the eye of the BEER holder" "Save your breath your going to need it for your blow up doll later" SearchLores.org |
|
|
|
|
|
#2 |
|
Expert Programmer
|
Ok i added this
[php] import java.awt.BorderLayout; [/php] but im starting to think this class and this syntax is for applets not console applications??? a little insight???
__________________
"When in Rome, Do as the Romans Do" "Beauty is in the eye of the BEER holder" "Save your breath your going to need it for your blow up doll later" SearchLores.org |
|
|
|
|
|
#3 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 748
Rep Power: 3
![]() |
Here's a thing I did for a project way back when (I'm surprised I still have the code :eek: ) Maybe it'll help you get the idea... (dont worry about all the different variables, just look at the add() calls.) There's 2 BorderLayouts in this BTW, one inside of the other. This wasn't an applet either
public TrafficSimControl(TrafficSimModel m)
{
world = m;
this.setLayout(new BorderLayout());
JPanel map = new JPanel();
map.setLayout(new BorderLayout());
// create and add the view to the model
view = new TrafficView(world, this);
world.addView(view);
view.addMouseListener(new MapListener());
// make scroll buttons
JButton scrollNorth, scrollEast, scrollWest, scrollSouth;
scrollNorth = new JButton("/\\");
scrollEast = new JButton(">");
scrollSouth = new JButton("\\/");
scrollWest = new JButton("<");
scrollNorth.addActionListener(new ScrollListener(Direction.NORTH));
scrollEast.addActionListener(new ScrollListener(Direction.EAST));
scrollSouth.addActionListener(new ScrollListener(Direction.SOUTH));
scrollWest.addActionListener(new ScrollListener(Direction.WEST));
map.add(BorderLayout.CENTER, view);
map.add(BorderLayout.NORTH, scrollNorth);
map.add(BorderLayout.EAST, scrollEast);
map.add(BorderLayout.SOUTH, scrollSouth);
map.add(BorderLayout.WEST, scrollWest);
add(BorderLayout.CENTER, map);
// make controlling buttons
JPanel options = new JPanel();
JButton pause = new JButton("pause");
JButton resume = new JButton("resume");
options.add(pause);
options.add(resume);
options.setBackground(Color.black);
add(BorderLayout.SOUTH, options);
// set up listeners for buttons
ButtonListener buttonListener = new ButtonListener();
pause.addActionListener(buttonListener);
resume.addActionListener(buttonListener);
} |
|
|
|
|
|
#4 |
|
Expert Programmer
|
can you give me a list of the imports...??? and of the surrounding class so i can see if u used this..
extends JPanel
__________________
"When in Rome, Do as the Romans Do" "Beauty is in the eye of the BEER holder" "Save your breath your going to need it for your blow up doll later" SearchLores.org |
|
|
|
|
|
#5 |
|
Expert Programmer
|
GRRR ok i really appreciate your help.. i used a borderlayout to add the button to the panel.. BUT it still takes up the entire EAST site or whatever side i put it on.. WHY CAN'T I CONTROL THE SIZE OF THE STUPID BUTTON... omg i hate java.
[php] import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.JPanel; import java.awt.BorderLayout; public class lettercount extends JPanel { public static void main(String args[]) { JPanel pan=new JPanel(); pan.setLayout(new BorderLayout()); JFrame app=new JFrame(); JButton fileButton=new JButton("Scan File"); char[] letterArray=new char[26]; int[] letterCount=new int[26]; int count=0; for(int i=97;i<=122;i++) { letterArray[count]=(char)i; //System.out.printf("%d = %s\n",count,letterArray[count]); count++; } pan.add(BorderLayout.EAST, fileButton); app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); app.add(pan); app.setSize(450,250); app.setVisible(true); } } [/php]
__________________
"When in Rome, Do as the Romans Do" "Beauty is in the eye of the BEER holder" "Save your breath your going to need it for your blow up doll later" SearchLores.org |
|
|
|
|
|
#6 |
|
Expert Programmer
|
i can't even control the sizeof the panel or where the panel is being placed... how do i put multiple panels on one frame??? or a row of buttons down the right side of a panel???? this is so damn crazy
__________________
"When in Rome, Do as the Romans Do" "Beauty is in the eye of the BEER holder" "Save your breath your going to need it for your blow up doll later" SearchLores.org |
|
|
|
|
|
#7 |
|
Expert Programmer
|
Java layouts suck... you should use a program that lets you drag and drop buttons onto a window, move them around and resize them. I think it's a waste of time learning layouts in Java. If you can't find a program like the one I described, I suggest absolutely positioning everything in the window using x and y coordinates, which gives you complete control over how everything is positioned (you can use getWidth() and getHeight() to allow elements to move and resize themselves when the window is resized).
|
|
|
|
|
|
#8 |
|
Expert Programmer
|
First off lol, this is for school i must do it this way. Second... OMG that is my whole problem.. i want to use X and Y coordinates to position them.. i have said that from the start in a previous thread. Can you give me examples on how to take control and position each one of my components seperatly using x and y coordinates.. thanks!
__________________
"When in Rome, Do as the Romans Do" "Beauty is in the eye of the BEER holder" "Save your breath your going to need it for your blow up doll later" SearchLores.org |
|
|
|
|
|
#9 |
|
Expert Programmer
|
Try something like the following:
getContentPane().setLayout(null); getContentPane().add(element); element.setBounds(24,500,72,36); // x, y, width, height element.setVisible(true); |
|
|
|
|
|
#10 |
|
Expert Programmer
|
what class does getContentPane() come from?
__________________
"When in Rome, Do as the Romans Do" "Beauty is in the eye of the BEER holder" "Save your breath your going to need it for your blow up doll later" SearchLores.org |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|