Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

problems with GUI BorderLayout

Posted on 2007-11-14
9
257 Views
Last Modified: 2013-11-23
I am trying to get my GUI program to have a nice layout, but everything I do doesn't work. I have to make an enormous window size to make everything visible in the window work. I also have not figured out how to make my OK button a normal size. It consistently fills the whole bottom of the screen. So my questions are:

1. How do I make my OK button smaller?
2. What do I need to do to get everything in my window visible and compact?
3. How do I make get my window static so I cannot resize it with my mouse?

I tried adjusting my setLocation and setSize, but none of the changes work.
0
Comment
Question by:monroe6
  • 3
  • 2
  • 2
  • +2
9 Comments
 

Author Comment

by:monroe6
ID: 20280619
I am sorry, here is my code:


 import java.awt.*;
 import java.awt.event.*;
 import javax.swing.*;
 
 public class Week2Assignment2
 {
 
       private static final int WINDOW_WIDTH = 400; // pixels
      private static final int WINDOW_HEIGHT = 400;
      private static final int FIELD_WIDTH = 15;
      private static final int INSTRUCTION_WIDTH = 30;
      
 // variable data fields
 
       private String str1;
      private OkButtonHandler okHandler;

      
 // GUI fields
 
      private JFrame window;
      private String usage = "Enter a string of letters (spaces ok).";
      private JTextArea instructions;
      private JTextField str1Field;
      private JLabel str1Label;
      private JButton okB;
      private JRadioButton list1, list2;
            
// create method

 public Week2Assignment2()
 {
 
 // set up new window to contain all components
      
        window = new JFrame();
        window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      
            
 // set up Instructions
 
        instructions = new JTextArea(usage);
        instructions.setEditable(false); // can't edit instructions
        instructions.setBackground(window.getBackground());
        instructions.setLineWrap(false);
        instructions.setWrapStyleWord(false);

 // set up fields for data entry/display
 
        str1Label = new JLabel("Enter any string of letters");
      str1Field = new JTextField(FIELD_WIDTH);
      
// set up center panel
      
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new FlowLayout());
      
// set up panel for user input

        JPanel str1Panel = new JPanel();
        str1Panel.setLayout(new FlowLayout());
        str1Panel.add(str1Label);
       str1Panel.add(str1Field);
      
      
// add components to main panel

        mainPanel.add(str1Panel);
        
// obtain content pane for this window

        Container c = window.getContentPane();

// set layouts
      
        c.setLayout(new BorderLayout());
      
// create list1 radio button

      list1 = new JRadioButton("1. Determine length of string");
      list1.setSize(100, 70);
      list1.setLocation(500, 100);
      
// create list2 radio button

      list2 = new JRadioButton("2. Determine how many consonants or vowels are in the string.");
      list2.setSize(100, 70);
      list2.setLocation(600, 150);

             
// create Ok button
      
      okB = new JButton("Ok");
      okHandler = new OkButtonHandler();
      okB.addActionListener(okHandler);
        
// add all fields to the content pane
      
        c.add(instructions, BorderLayout.NORTH);
      c.add(mainPanel, BorderLayout.CENTER);
      c.add(okB, BorderLayout.SOUTH);
      c.add(list1, BorderLayout.WEST);
      c.add(list2, BorderLayout.EAST);

// make the window visible
  window.setSize(1000,400);
  window.setVisible(true);
      
}      

private class OkButtonHandler implements ActionListener
{
      public void actionPerformed(ActionEvent e)
      {
            
            String str2;
            String str3;
            
            str2 = "You entered " + str1Field.getText()
                         + ". Choose one of the following.\n";      
            str3 = "1. Determine length of string \n" +
                         "2. Determine how many consonants or vowels are in the string.\n";
                         
                         System.out.print (str2);
                         System.out.print (str3);
      }
}

// create main
            
      public static void main(String[] args)
      {

    Week2Assignment2 gui = new Week2Assignment2();
   }
}
0
 
LVL 23

Accepted Solution

by:
Siva Prasanna Kumar earned 240 total points
ID: 20280766
>>How do I make my OK button smaller?

As you have added  only Ok utton to the south directly using c.add(okB, BorderLayout.SOUTH); It occupies the complete with of your layout. try this,

JPanel panel = new JPanel();
panel.add(okB);
 c.add(panel, BorderLayout.SOUTH);

This will fix the first one.

3rd one that is to fix the size of the window

window = new JFrame();
        window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

>> Add this line here window.setResizable(false);
0
 
LVL 23

Assisted Solution

by:rama_krishna580
rama_krishna580 earned 20 total points
ID: 20280781
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:monroe6
ID: 20280988
I almost got it, but now everything runs on the same line. How do I fix that?

import java.awt.*;
 import java.awt.event.*;
 import javax.swing.*;
 
 public class Week2Assignment2
 {
 
       private static final int WINDOW_WIDTH = 400; // pixels
      private static final int WINDOW_HEIGHT = 400;
      private static final int FIELD_WIDTH = 15;
      private static final int INSTRUCTION_WIDTH = 30;
      
 // variable data fields
 
       private String str1;
      private OkButtonHandler okHandler;

      
 // GUI fields
 
      private JFrame window;
      private String usage = "Enter a string of letters (spaces ok).";
      private JTextArea instructions;
      private JTextField str1Field;
      private JLabel str1Label;
      private JButton okB;
      private JRadioButton list1, list2;
            
// create method

 public Week2Assignment2()
 {
 
 // set up new window to contain all components
      
        window = new JFrame();
        window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      
            
 // set up Instructions
 
        instructions = new JTextArea(usage);
        instructions.setEditable(false); // can't edit instructions
        instructions.setBackground(window.getBackground());
        instructions.setLineWrap(false);
        instructions.setWrapStyleWord(false);

 // set up fields for data entry/display
 
        str1Label = new JLabel("Enter any string of letters");
      str1Field = new JTextField(FIELD_WIDTH);
      
// set up center panel
      
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new FlowLayout());
      
// set up panel for user input

        JPanel str1Panel = new JPanel();
        str1Panel.setLayout(new FlowLayout());
        str1Panel.add(str1Label);
       str1Panel.add(str1Field);
      
      
// add components to main panel

        mainPanel.add(str1Panel);
        
// obtain content pane for this window

        Container c = window.getContentPane();

// set layouts
      
        c.setLayout(new BorderLayout());
      
// create list1 radio button

      list1 = new JRadioButton("1. Determine length of string");
      list1.setSize(100, 70);
      list1.setLocation(500, 100);
      
      JPanel panel2 = new JPanel();
      panel2.add(list1);
      
// create list2 radio button

      list2 = new JRadioButton("2. Determine how many consonants or vowels are in the string.");
      list2.setSize(100, 70);
      list2.setLocation(600, 150);
      
      JPanel panel3 = new JPanel();
      panel3.add(list2);

             
// create Ok button
      
      okB = new JButton("Ok");
      okHandler = new OkButtonHandler();
      okB.addActionListener(okHandler);
      
      JPanel panel = new JPanel();
      panel.add(okB);
        
// add all fields to the content pane
      
        c.add(instructions, BorderLayout.NORTH);
      c.add(mainPanel, BorderLayout.WEST);
      c.add(panel, BorderLayout.SOUTH);
      c.add(panel2, BorderLayout.CENTER);
      c.add(panel3, BorderLayout.EAST);

// make the window visible
  window.setSize(950,150);
  window.setVisible(true);
  window.setResizable(false);
      
}      

private class OkButtonHandler implements ActionListener
{
      public void actionPerformed(ActionEvent e)
      {
            
            String str2;
            String str3;
            
            str2 = "You entered " + str1Field.getText()
                         + ". Choose one of the following.\n";      
            str3 = "1. Determine length of string \n" +
                         "2. Determine how many consonants or vowels are in the string.\n";
                         
                         System.out.print (str2);
                         System.out.print (str3);
      }
}

// create main
            
      public static void main(String[] args)
      {

    Week2Assignment2 gui = new Week2Assignment2();
   }
}
0
 
LVL 23

Assisted Solution

by:Siva Prasanna Kumar
Siva Prasanna Kumar earned 240 total points
ID: 20281041
This seems to be a assignment and hence as per the rules I think we are only allowed to direct you towards solution.
 Any way My above suggestions do work. And now things you need to do are you need to group the radio buttons

see the example here

http://www.java2s.com/Code/Java/Swing-JFC/UsingJRadioButtons.htm
0
 
LVL 17

Assisted Solution

by:contactkarthi
contactkarthi earned 40 total points
ID: 20281213
0
 
LVL 23

Assisted Solution

by:Siva Prasanna Kumar
Siva Prasanna Kumar earned 240 total points
ID: 20281232
@contactkarthi:

GridBagLayout is the toughest layout in swing and I don't think such a small app need it.
0
 
LVL 17

Assisted Solution

by:contactkarthi
contactkarthi earned 40 total points
ID: 20282043
you need to visualise how the components will be place based on the values you specify..

initially there will be a couple of trial and error.. but once you get the point it is the best flexible way for placing your components..
0
 
LVL 92

Assisted Solution

by:objects
objects earned 200 total points
ID: 20285027
try a boxlayout

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Week2Assignment2
{

      private static final int WINDOW_WIDTH = 400; // pixels
      private static final int WINDOW_HEIGHT = 400;
      private static final int FIELD_WIDTH = 15;
      private static final int INSTRUCTION_WIDTH = 30;

      // variable data fields

      private String str1;
      private OkButtonHandler okHandler;

      // GUI fields

      private JFrame window;
      private String usage = "Enter a string of letters (spaces ok).";
      private JTextArea instructions;
      private JTextField str1Field;
      private JLabel str1Label;
      private JButton okB;
      private JRadioButton list1, list2;

      // create method

      public Week2Assignment2()
      {

            // set up new window to contain all components

            window = new JFrame();
            window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            // set up Instructions

            instructions = new JTextArea(usage);
            instructions.setEditable(false); // can't edit instructions
            instructions.setBackground(window.getBackground());
            instructions.setLineWrap(false);
            instructions.setWrapStyleWord(false);

            // set up fields for data entry/display

            str1Label = new JLabel("Enter any string of letters");
            str1Field = new JTextField(FIELD_WIDTH);

            // set up center panel

            JPanel mainPanel = new JPanel();
            mainPanel.setLayout(new FlowLayout());

            // set up panel for user input

            JPanel str1Panel = new JPanel();
            str1Panel.setLayout(new FlowLayout());
            str1Panel.add(str1Label);
            str1Panel.add(str1Field);

            // add components to main panel

            mainPanel.add(str1Panel);

            // obtain content pane for this window

            Container c = window.getContentPane();

            // set layouts

            c.setLayout(new BorderLayout());

            // create list1 radio button

            JPanel form = new JPanel();
            form.setLayout(new BoxLayout(form, BoxLayout.Y_AXIS));

            form.add(mainPanel);
            list1 = new JRadioButton("1. Determine length of string");

            form.add(list1);

            // create list2 radio button

            list2 = new JRadioButton(
                    "2. Determine how many consonants or vowels are in the string.");

            form.add(list2);

            // create Ok button

            okB = new JButton("Ok");
            okHandler = new OkButtonHandler();
            okB.addActionListener(okHandler);

            JPanel panel = new JPanel();
            panel.add(okB);

            // add all fields to the content pane

            c.add(instructions, BorderLayout.NORTH);
            c.add(panel, BorderLayout.SOUTH);
            c.add(form, BorderLayout.CENTER);

            // make the window visible
            window.pack();
            window.setVisible(true);
            window.setResizable(false);

      }

      private class OkButtonHandler implements ActionListener
      {
            public void actionPerformed(ActionEvent e)
            {

                  String str2;
                  String str3;

                  str2 = "You entered " + str1Field.getText()
                          + ". Choose one of the following.\n";
                  str3 = "1. Determine length of string \n"
                          + "2. Determine how many consonants or vowels are in the string.\n";

                  System.out.print(str2);
                  System.out.print(str3);
            }
      }

      // create main

      public static void main(String[] args)
      {

            Week2Assignment2 gui = new Week2Assignment2();
      }
}
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
HashTable highest marks enumeration alternative 9 42
use lov values 2 61
throw exception 21 58
Setup GlassFish 4 21
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question