Solved

problems with GUI BorderLayout

Posted on 2007-11-14
9
253 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
copyEndy  challenge 15 58
countHi challenge 25 85
maven java path setting 5 50
Java and GPO 11 47
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now