Logik22
12-12-2011, 07:03 PM
I'm new to Java so I decided to create a very basic program. I'm creating a Random Movie Picker. Basically you browse to a directory, the program will select a file at random, and then output the name.
I ran into 2 small problems with the GUI (Grid layout)
1) When I run the program the content pane is blank. It will only display if I click the frame (as if I'm resizing the window).
2) The logic of grid layout seems very basic and it reflects in the presentation of the program. Is there a way I can manipulate the size of the components in the grid? As it stands now the controls are stretched to fill the grid and it is not very functional (especially with long directory names). Is there a way I can manipulate the size of individual controls or is there another way I should be making the GUI?
import javax.swing.JFrame;
import javax.swing.SwingConstants;
import java.awt.*;
import javax.swing.*;
public class RMP {
public static class GUI extends JFrame
{
public static void main(String args[])
{
GUI create = new GUI();
}
//Variables
private static final int Width = 500;
private static final int Height = 200;
private JTextField txtDirectory, txtRandom;
private JLabel lblDirectory, lblRandom;
private JButton btnBrowse, btnPick;
public GUI()
{
//Customize JFrame
setTitle("Random Movie Picker");
setSize(Width, Height);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
//Create Pane
Container pane = getContentPane();
//Set Layout
pane.setLayout(new GridLayout (2, 3));
//Text Fields
txtDirectory = new JTextField(255);
txtRandom = new JTextField(255);
//Labels
lblDirectory = new JLabel("Directory: ", SwingConstants.LEFT);
lblRandom = new JLabel("Random Movie: ", SwingConstants.LEFT);
//Buttons
btnBrowse = new JButton("Browse...");
btnPick = new JButton("Pick My Movie!");
//Add components to Content Pane
pane.add(lblDirectory);
pane.add(txtDirectory);
pane.add(btnBrowse);
pane.add(lblRandom);
pane.add(txtRandom);
pane.add(btnPick);
}
}
}
I ran into 2 small problems with the GUI (Grid layout)
1) When I run the program the content pane is blank. It will only display if I click the frame (as if I'm resizing the window).
2) The logic of grid layout seems very basic and it reflects in the presentation of the program. Is there a way I can manipulate the size of the components in the grid? As it stands now the controls are stretched to fill the grid and it is not very functional (especially with long directory names). Is there a way I can manipulate the size of individual controls or is there another way I should be making the GUI?
import javax.swing.JFrame;
import javax.swing.SwingConstants;
import java.awt.*;
import javax.swing.*;
public class RMP {
public static class GUI extends JFrame
{
public static void main(String args[])
{
GUI create = new GUI();
}
//Variables
private static final int Width = 500;
private static final int Height = 200;
private JTextField txtDirectory, txtRandom;
private JLabel lblDirectory, lblRandom;
private JButton btnBrowse, btnPick;
public GUI()
{
//Customize JFrame
setTitle("Random Movie Picker");
setSize(Width, Height);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
//Create Pane
Container pane = getContentPane();
//Set Layout
pane.setLayout(new GridLayout (2, 3));
//Text Fields
txtDirectory = new JTextField(255);
txtRandom = new JTextField(255);
//Labels
lblDirectory = new JLabel("Directory: ", SwingConstants.LEFT);
lblRandom = new JLabel("Random Movie: ", SwingConstants.LEFT);
//Buttons
btnBrowse = new JButton("Browse...");
btnPick = new JButton("Pick My Movie!");
//Add components to Content Pane
pane.add(lblDirectory);
pane.add(txtDirectory);
pane.add(btnBrowse);
pane.add(lblRandom);
pane.add(txtRandom);
pane.add(btnPick);
}
}
}