monroe6
asked on
problems with GUI BorderLayout
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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_WIDT H, WINDOW_HEIGHT);
window.setDefaultCloseOper ation(JFra me.EXIT_ON _CLOSE);
// set up Instructions
instructions = new JTextArea(usage);
instructions.setEditable(f alse); // can't edit instructions
instructions.setBackground (window.ge tBackgroun d());
instructions.setLineWrap(f alse);
instructions.setWrapStyleW ord(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(okHa ndler);
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(ActionEven t 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();
}
}
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_WIDT
window.setDefaultCloseOper
// set up Instructions
instructions = new JTextArea(usage);
instructions.setEditable(f
instructions.setBackground
instructions.setLineWrap(f
instructions.setWrapStyleW
// 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(okHa
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(ActionEven
{
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();
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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_WIDT
window.setDefaultCloseOper
// set up Instructions
instructions = new JTextArea(usage);
instructions.setEditable(f
instructions.setBackground
instructions.setLineWrap(f
instructions.setWrapStyleW
// 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(okHa
// 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(ActionEven
{
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();
}
}