Results 1 to 5 of 5
Thread: JFrame
- 09-14-2010, 03:37 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 9
- Rep Power
- 0
JFrame
Hello everyone,
First of all I just want to let you all know that this is not homework. It was a piece of code that was given to us in class as an example of how a loop could create labels, text fields, and buttons in a JFrame.
I've taken it upon myself to add a BorderLayout to position those items neatly. If you complile the code you can see I'm not getting the results that I am hoping for.
Java Code:import java.util.Scanner; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.GridLayout; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.JButton; public class JFrameExercise extends JFrame { private static double _relWidth, _relHeight; public JFrameExercise() { // Set-up the basic JFrame this.setTitle("Day 2 Coding Exercise"); Dimension screenSize = this.getToolkit().getScreenSize(); this.setSize((int)(_relWidth * screenSize.width/100), (int)(_relHeight * screenSize.height/100)); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(EXIT_ON_CLOSE); // Create a JPanel JPanel pnl = new JPanel(new GridLayout(2,10,25,10)); pnl.setLayout(new BorderLayout()); // Add 10 sets of labels and text fields to the JPanel JLabel [] labels = new JLabel[10]; JTextField [] textflds = new JTextField[10]; for(int i = 0; i < labels.length; i++) { labels[i] = new JLabel("Label " + (i+1)); pnl.add(labels[i], BorderLayout.WEST); textflds[i] = new JTextField("Text Field " + (i+1)); pnl.add(textflds[i], BorderLayout.EAST); } // Add 5 buttons to the JPanel JButton [] buttons = new JButton[5]; for(int i = 0; i < buttons.length; i++) { buttons[i] = new JButton("Button " + (i+1)); pnl.add(buttons[i], BorderLayout.SOUTH); } // Add the panel to the frame this.add(pnl); // Display the frame this.setVisible(true); } public static void main(String[] args) { // Get the relative width and height of the JFrame Scanner input = new Scanner(System.in); System.out.print("Enter the relative width of the JFrame (<= 100%): "); _relWidth = input.nextDouble(); System.out.print("Enter the relative height of the JFrame (<= 100%): "); _relHeight = input.nextDouble(); // Create the JFrame new JFrameExercise(); } }
- 09-14-2010, 04:04 PM #2
Before doing any work on this, could you explain what you were hoping for? And describe what the code actually does and why the two are different?I'm not getting the results that I am hoping for.
- 09-14-2010, 05:25 PM #3
Member
- Join Date
- Sep 2010
- Posts
- 9
- Rep Power
- 0
Ok heres whats happening:
When the program was initially run it would automatically create 10 labels, 10 text fields, and 5 buttons. But it would put them all into a row. When I modified the code to add the BorderLayout.east/west/north/south it only displays the last label, text field, and button.
What I'm trying to achieve is to have all 10 labels, 10 text fields, and 5 buttons displayed as followed:
10 Labels EAST
10 Text Fields WEST
5 Buttons South
- 09-14-2010, 05:42 PM #4
Thanks. That seems clear enough.
You need to redesign your use of panels and layout managers.
You should use one panel with its layout manager for each border position. The components in the west and east are to be vertical and in the south horizontal. Try a GridLayout for west and east and a flowlayout for the south.
For the main panel use border layout. Add each of the above 3 panels to the main panel at its border position.Last edited by Norm; 09-14-2010 at 05:52 PM.
- 09-14-2010, 07:21 PM #5
Similar Threads
-
to pass a parameter from a jframe children to its jframe mother
By anix in forum NetBeansReplies: 5Last Post: 06-14-2010, 06:10 PM -
JFrame from a JFrame in project
By anah in forum AWT / SwingReplies: 2Last Post: 04-19-2010, 06:20 PM -
Passing data from one JFrame to another JFrame. - need help.
By Unsub in forum New To JavaReplies: 6Last Post: 04-12-2010, 11:33 AM -
Passing data from one JFrame to another JFrame
By tarami in forum New To JavaReplies: 3Last Post: 08-06-2009, 05:44 PM -
How to make a Jframe un-focusable when another Jframe is active?
By Robert_85 in forum Advanced JavaReplies: 4Last Post: 04-22-2009, 11:02 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks