JPanels in Jframe not shown - java

I am making a simple layout for a calculator, actually i am new to java and learning the basics. My problem is that when i run this code, only a JFrame opens and the other panels n its buttons are not shown. PLz help , where my i going wrong.
import java.awt.*;
import javax.swing.*;
public class Layouts extends JFrame{
public Layouts(){
super("Calculator");
setLookAndFeel();
setSize(350,350);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BorderLayout border = new BorderLayout();
setLayout(border);
GridLayout numbers = new GridLayout(2,2);
row2.setLayout(numbers);
row2.add(one);
row2.add(two);
row2.add(three);
row2.add(four);
GridLayout operators = new GridLayout(2,2);
row3.setLayout(operators);
row3.add(plus);
row3.add(subtract);
row3.add(multiply);
row3.add(equals);
setVisible(true);
}
private void setLookAndFeel()
{
try
{
IManager.setLookAndFeel("com.sun.java.lang.plaf.nimbus.NimbusLookAndFeel");
}
catch(Exception exc)
{
}
}
//row 1
JPanel row1 = new JPanel();
JTextField text = new JTextField(20);
//row 2
JPanel row2 = new JPanel();
JButton one = new JButton("1");
JButton two = new JButton("2");
JButton three = new JButton("3");
JButton four = new JButton("4");
//row3
JPanel row3 = new JPanel();
JButton plus = new JButton("+");
JButton subtract = new JButton("-");
JButton multiply = new JButton("*");
JButton equals = new JButton("=");
public static void main(String[] args)
{
Layouts l1 = new Layouts();
}
}

Remember to add all of the components (i.e row2, row3 etc..)
Example:
add(row2,BorderLayout.CENTER)
add(row3,BorderLayout.SOUTH)

BorderLayout border = new BorderLayout();
setLayout(border);
But you aren't adding anything to border ! Add the numbers and operators.

You need to add the JPanels and JButtons to JFrame. The JFrame in this case is your Layouts class. So do something like :
row1.add(text);
this.add(row1);
row2.add(one);
row2.add(two);
row2.add(three);
row2.add(four);
this.add(row2);
...
check this tutorial is is a very useful one JButton, JPanel & JFrame examples

Related

Using JPanel to make a calculator: how to use layout?

I'm working on a question that simply states to make an GUI that looks like This calculator, it doesn't have to function, just look like it. So I think I have the JPanel and JButton components right but I can't organize the fields to make it come out right. I'm pretty new so any crash course on how to organize a GUI would be appreciated.
Here's what I have so far:
// Using a JPanel to help lay out components.
import java.awt.GridLayout;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextField;
public class Calculator extends JFrame
{
private final JPanel buttonJPanel2, buttonJPanel3, buttonJPanel4,
buttonJPanel5; // panel to hold buttons
private final JButton[] buttons3, buttons4, buttons5;
private final JButton[] buttons2;
private final JTextField buttonJPanel1;
// no-argument constructor
public Calculator()
{
super("Calculator");
buttonJPanel1 = new JTextField();
add(buttonJPanel1, BorderLayout.NORTH); // add panel1 to JFrame
buttons2 = new JButton[4];
buttons2[0] = new JButton("7");
buttons2[1] = new JButton("8");
buttons2[2] = new JButton("9");
buttons2[3] = new JButton("/");
buttonJPanel2 = new JPanel();
buttonJPanel2.setLayout(new GridLayout(1, buttons2.length));
add(buttonJPanel2, BorderLayout.AFTER_LAST_LINE); // add panel2 to JFrame
buttons3 = new JButton[4];
buttons3[0] = new JButton("4");
buttons3[1] = new JButton("5");
buttons3[2] = new JButton("6");
buttons3[3] = new JButton("*");
buttonJPanel3 = new JPanel();
buttonJPanel3.setLayout(new GridLayout(1, buttons3.length));
add(buttonJPanel3, BorderLayout.AFTER_LAST_LINE); // add panel3 to JFrame
buttons4 = new JButton[4];
buttons4[0] = new JButton("1");
buttons4[1] = new JButton("2");
buttons4[2] = new JButton("3");
buttons4[3] = new JButton("-");
buttonJPanel4 = new JPanel();
buttonJPanel4.setLayout(new GridLayout(1, buttons4.length));
add(buttonJPanel4, BorderLayout.AFTER_LAST_LINE); // add panel4 to JFrame
buttons5 = new JButton[4];
buttons2[0] = new JButton("0");
buttons5[1] = new JButton(".");
buttons5[2] = new JButton("=");
buttons5[3] = new JButton("+");
buttonJPanel5 = new JPanel();
buttonJPanel5.setLayout(new GridLayout(1, buttons5.length));
add(buttonJPanel5, BorderLayout.AFTER_LAST_LINE); // add panel5 to
//JFrame
}
public static void main(String[] args)
{
PanelFrame panelFrame = new PanelFrame();
panelFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panelFrame.setSize(700, 500);
panelFrame.setVisible(true);
}
} // end class PanelFrame
In short: every component has to be declared,
JButton button1;
initialized
button1 = new JButton("Click me!");
and added to the component above it in the hierarchy (in this case the panel)
panel1.add(button1);
If you do not add the components to the panel and the panel to the frame they will not be part of the GUI, thus not visible.
A JPanel can be set to adjust its layout in different ways using a LayoutManager as you have done with GridLayout (which seems fitting for a calculator). I suggest you read about how to use the grid layout here.
Hope I could help :)
You need to add the buttons to the `JPanel'. For example:
for(JButton b : buttons2) { buttonJPanel2.add(b); }
BorderLayout accepts one component at each location, so if you set BorderLayout.AFTER_LAST_LINE twice, the last add overwrites previous one.

JAVA Swing Components won't show up

For some reason some of the Swing components don't show up when I run the program and I can't figure out why. Only the multiply label, multiply button, total label, and stop button show up. The rest don't work.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class BabyCalculatorFinal extends JFrame{
private JLabel AdditionLabel;
private JTextField AdditionField;
private JButton AdditionButton;
private JPanel Multiplication;
private JLabel MultiplicationLabel;
private JTextField MultiplicationField;
private JButton MultiplicationButton;
private JPanel Total;
private JLabel TotalLabel;
private JTextField TotalField;
JButton StopButton;
public BabyCalculatorFinal(){
setDefaultCloseOperation(EXIT_ON_CLOSE);// 1st thing to do
setName("Baby Calculator Final"); // 2nd thing to do
setLayout(new GridLayout(3,0)); //sets grid layout for the entire thing with 3 rows
// Create Action Event
BabyCalculatorListener Listener = new BabyCalculatorListener();
//Addition
//Addition Set Layout
JPanel Addition = new JPanel(new BorderLayout());
//Addition Features
AdditionLabel = new JLabel("Amount to add"); //Create label
AdditionField = new JTextField(10);
AdditionButton = new JButton("Add");
//Organize Addition Panel
Addition.add(AdditionLabel, BorderLayout.WEST);//IMPORTANT FORMAT
Addition.add(AdditionLabel, BorderLayout.CENTER);
Addition.add(AdditionButton, BorderLayout.EAST);
//Add addition Panel to Frame
add(Addition);
AdditionButton.addActionListener(Listener);
//Multiplictation
//Multiplication Set Layout
Multiplication = new JPanel();
Multiplication.setLayout(new BorderLayout());//Trying a different way of setting the layout
//Multiplication Features
MultiplicationLabel = new JLabel("Amount to Multiply"); //Create label
MultiplicationField = new JTextField(10);
MultiplicationButton = new JButton("Multiply");
//Organize Multiplication Panel
Addition.add(MultiplicationLabel, BorderLayout.WEST);
Addition.add(MultiplicationLabel, BorderLayout.CENTER);
Addition.add(MultiplicationButton, BorderLayout.EAST);
//Add Multiplication Panel to Frame
add(Multiplication);
MultiplicationButton.addActionListener(Listener);
//Total
Total = new JPanel(new FlowLayout(10));
TotalLabel = new JLabel("Total");
TotalField = new JTextField();
TotalField.setText("0.0");
TotalField.setVisible(false);
StopButton = new JButton("Stop");
Total.add(TotalLabel);
Total.add(TotalField);
Total.add(StopButton);
//Add Total Panel to Frame
add(Total);
pack();
setVisible(true);
}
public static void main(String[] args){
JFrame myFrame = new BabyCalculatorFinal();
}
public class BabyCalculatorListener implements ActionListener{
public void actionPerformed(ActionEvent e){
String totalText = TotalField.getText();
double total = Double.parseDouble(totalText);
if (e.getSource() == AdditionButton){
String additionText = AdditionField.getText();
double addition = Double.parseDouble(additionText);
total += addition;
}
else{
String multiplicationText = MultiplicationField.getText();
double multiplication = Double.parseDouble(multiplicationText);
total += multiplication;
}
TotalField.setText(total + "");
}
}
}
Your code is full of typos (?), for example you're adding AdditionLabel twice to the JPanel instead of adding AdditionLabel and AdditionField. And you're not using the Multiplication panel after creating it but instead overriding the contents of the Addition panel. The corrected snippet that adds the components should be (I changed the variable names to conform to Java conventions):
additionLabel = new JLabel("Amount to add"); // Create label
additionField = new JTextField(10);
additionButton = new JButton("Add");
// Organize addition Panel
addition.add(additionLabel, BorderLayout.WEST);// IMPORTANT FORMAT
addition.add(additionField, BorderLayout.CENTER); // instead of additionLabel
addition.add(AdditionButton, BorderLayout.EAST);
// Add addition Panel to Frame
add(addition);
AdditionButton.addActionListener(Listener);
// Multiplictation
// Multiplication Set Layout
multiplication = new JPanel();
multiplication.setLayout(new BorderLayout());// Trying a different way
// of setting the layout
// Multiplication Features
multiplicationLabel = new JLabel("Amount to Multiply"); // Create label
multiplicationField = new JTextField(10);
multiplicationButton = new JButton("Multiply");
// Organize Multiplication Panel
multiplication.add(multiplicationLabel, BorderLayout.WEST); // instead of Addition
multiplication.add(multiplicationField, BorderLayout.CENTER);
multiplication.add(multiplicationButton, BorderLayout.EAST);

usage of BorderLayout

I am having a problem with BorderLayout, that was set to the green JPanel side. It does not display elements on the EAST in a row order. Do I have to combine this with GridBagLayout ? Could someone advice me how should I tackle this problem?
Basically the problem is of displaying objects inside green area below when I am using
Current layout:
My aim is to achieve this layout:
public class GUILayout {
public static void main(String[] args) {
JFrame jf = new JFrame();
JButton jbO = new JButton("CSIS0396");
JButton jbl = new JButton("Final");
JButton jb2 = new JButton("2010");
JButton jb3 = new JButton("Exam");
JPanel panel = new JPanel();
JPanel panel2 = new JPanel();
JButton object_btn = new JButton("Object");
JButton oriented_btn = new JButton("Oriented");
JButton programming_btn = new JButton("Programming");
JButton and_btn = new JButton("and");
JButton java_btn = new JButton("Java");
BorderLayout layout = new BorderLayout();
panel.setLayout(layout);
panel2.setLayout(layout);
panel.add(BorderLayout.CENTER,object_btn);
panel.add(BorderLayout.WEST,oriented_btn);
panel.add(BorderLayout.WEST,programming_btn);
panel.add(BorderLayout.WEST,and_btn);
panel.add(BorderLayout.WEST,java_btn);
panel2.add(BorderLayout.NORTH, jbO);
panel2.add(BorderLayout.SOUTH, jb2);
panel2.add(BorderLayout.WEST, jbl);
panel2.add(BorderLayout.EAST, jb3);
panel.setBackground(Color.GREEN);
panel2.setBackground(Color.RED);
jf.getContentPane().add(panel);
jf.getContentPane().add(panel2, BorderLayout.EAST);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(400, 300);
jf.setVisible(true);
}
}
don't share same layout for multiple component and use box layout for left panel to positioning buttons
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GUILayout {
public static void main(String[] args) {
JFrame jf = new JFrame();
JButton jbO = new JButton("CSIS0396");
JButton jbl = new JButton("Final");
JButton jb2 = new JButton("2010");
JButton jb3 = new JButton("Exam");
JPanel panel = new JPanel();
JPanel panel2 = new JPanel();
JButton object_btn = new JButton("Object");
JButton oriented_btn = new JButton("Oriented");
JButton programming_btn = new JButton("Programming");
JButton and_btn = new JButton("and");
JButton java_btn = new JButton("Java");
BorderLayout layout = new BorderLayout();
panel2.setLayout(layout);
panel.setLayout( new BoxLayout(panel, BoxLayout.PAGE_AXIS));
panel.add(object_btn);
panel.add(oriented_btn);
panel.add(programming_btn);
panel.add(and_btn);
panel.add(java_btn);
panel2.add(BorderLayout.NORTH, jbO);
panel2.add(BorderLayout.SOUTH, jb2);
panel2.add(BorderLayout.WEST, jbl);
panel2.add(BorderLayout.EAST, jb3);
panel.setBackground(Color.GREEN);
panel2.setBackground(Color.RED);
jf.setLayout(new BorderLayout());
jf.getContentPane().add(panel ,BorderLayout.WEST);
jf.getContentPane().add(panel2, BorderLayout.EAST);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(400, 300);
jf.setVisible(true);
}
}
The first problem is that you're having your panels share a layout. You must use a new BorderLayout for each of them.
The second problem is that a BorderLayout can only have one component in each constraint position. When you try to add multiple components to the WEST position, each one replaces the one that was previously in that position:
panel.add(BorderLayout.WEST,oriented_btn);
// Implicitly removes oriented_btn from panel
panel.add(BorderLayout.WEST,programming_btn);
// Implicitly removes programming_btn from panel
panel.add(BorderLayout.WEST,and_btn);
// Implicitly removes and_btn from panel
panel.add(BorderLayout.WEST,java_btn);
The solution is to put them in their own container, such as a Box or a JPanel with a GridLayout:
Box box = Box.createVerticalBox();
// Or:
//JComponent box = new JPanel(new GridLayout(0, 1));
box.add(oriented_btn);
box.add(programming_btn);
box.add(and_btn);
box.add(java_btn);
panel.add(BorderLayout.WEST, box);

setVgap(0), setHgap(0) not working in gridlayout?

I am trying to build an app that has some components such as buttons, labels, text fields, a menu bar and a picture (I didn't tackle the image problem yet so there is no code for that).
So I made a grid layout for my frame and constructed 6 panels with their respective components as explained in the code bellow. But when I run it it doesn't show anything at first, just a blank frame, unless I maximize the window. Because when I do that everything appears to be working fine. Except for 2 things.
I have setVgap() and setHgap() to zero but there are still gaps between the components. and the 2nd thing is that the BorderLayout.NORTH, (..).SOUTH etc don't seem to work either.
public class Window extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel menupanel = new JPanel();
public Window() {
super("Image Application");
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
requestFocus();
// Setting Layout
GridLayout grid = new GridLayout(6, 0, 0, 0);
//grid.setVgap(0);
//grid.setHgap(0);
this.setLayout(grid);
// Menu
JMenuBar menubar = new JMenuBar();
JMenu menu = new JMenu("Options");
JButton button = new JButton("Reset");
// Buttons
menupanel.add(new JButton("Allign Left"));
menupanel.add(new JButton("Allign Center"));
menupanel.add(new JButton("Allign Right"));
// Picture
JPanel p1 = new JPanel();
// 2x JLabels and ComboBoxes to get the preferred dimensions
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
JLabel b2 = new JLabel("Width: ");
JLabel b3 = new JLabel("Height: ");
JTextField box1 = new JTextField(25);
JTextField box2 = new JTextField(25);
// Resize Button
JPanel p4 = new JPanel();
JButton b4 = new JButton("Resize");
// Adding Components to their panels
p2.add(b2);
p2.add(box1);
p3.add(b3);
p3.add(box2);
p4.add(b4);
menu.add(button);
menubar.add(menu);
// add all of the panels to JFrame
this.add(menupanel);
this.add(p1, BorderLayout.NORTH);
this.add(p2, BorderLayout.SOUTH);
this.add(p3, BorderLayout.WEST);
this.add(p4, BorderLayout.EAST);
this.setJMenuBar(menubar);
pack();
setVisible(true);
}
public static void main(String args[]) {
Window w = new Window();
}
}
Any ideas?
~EDIT1 changed according to first 2 comments, the pack(); seems to fix the problem that i needed to maximise the window to see the comp's ( -Thanks ), but the setVgap() problem remains.
~EDIT2 when I run it this window is shown:
While I want it to look more like this:
AGAIN Ignore the picture
~EDIT3 Well, I changed the value that was passed in the constructor for the Hgap and it does change accordingly for different values but it seems that zero Hgap is still ~10 pixels wide?! Also I noted that the gap doesn't change between the menubar and the first Jbuttons, but only for the ret of the components.
~EDIT4 It also works for negative int's..?! I am lost here..
please to compare, you should using second parameter for GridLayout, then setVgap() will works (frame.setLayout(new GridLayout(6, 0, 5, 5));), here is only zero value,
Window is reserved word in Java for awt.Window, don't to use this Object name as class name
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class MyWindow {
private static final long serialVersionUID = 1L;
private JPanel menupanel = new JPanel();
private JFrame frame = new JFrame("Image Application");
public MyWindow() {
// Menu
JMenuBar menubar = new JMenuBar();
JMenu menu = new JMenu("Options");
JButton button = new JButton("Reset");
// Buttons
menupanel.add(new JButton("Allign Left"));
menupanel.add(new JButton("Allign Center"));
menupanel.add(new JButton("Allign Right"));
// Picture
JPanel p1 = new JPanel();
p1.setBackground(Color.RED);
// 2x JLabels and ComboBoxes to get the preferred dimensions
JPanel p2 = new JPanel();
p2.setBackground(Color.ORANGE);
JLabel b2 = new JLabel("Width: ");
p2.add(b2);
JTextField box1 = new JTextField(25);
p2.add(box1);
JPanel p3 = new JPanel();
p3.setBackground(Color.BLUE);
JLabel b3 = new JLabel("Height: ");
JTextField box2 = new JTextField(25);
p3.add(b3);
p3.add(box2);
// Resize Button
JPanel p4 = new JPanel();
p4.setBackground(Color.MAGENTA);
JButton b4 = new JButton("Resize");
// Adding Components to their panels
p4.add(b4);
menu.add(button);
menubar.add(menu);
// add all of the panels to JFrame
frame.setLayout(new GridLayout(6, 0, 5, 5));
frame.add(menupanel);
frame.add(p1);
frame.add(p2);
frame.add(p3);
frame.add(p4);
frame.setJMenuBar(menubar);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
MyWindow w = new MyWindow();
}
});
}
}

Unwanted line between labels and radio group in Java

In my project, I use Swing controls. I had used a label together with a button group, but there is an unwanted line. Please help. There is a label associated with each radio button group. The unwanted line is there.how to add the labels and corresponding radio button group to the same panel
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//import java.util.Arrays;
public class Online extends JFrame {
static JRadioButton[] choice = new JRadioButton[6];
JFrame jtfMainFrame, jtfMainFrame1;
public void createWindow() {
jtfMainFrame = new JFrame("Online Examination");
jtfMainFrame.setSize(800, 500);
jtfMainFrame.setLocation(200, 150);
jtfMainFrame.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
JPanel pa = new JPanel();
JPanel panlabels = new JPanel(new GridLayout(0, 1, 0, 60));
JPanel pancontrols = new JPanel(new GridLayout(0, 1, 0, 60));
JPanel panEast = new JPanel();
JPanel pan = new JPanel(new FlowLayout());
JLabel qLabel = new JLabel("Question.");
qLabel.setOpaque(true);
qLabel.setForeground(Color.blue);
qLabel.setBackground(Color.lightGray);
JLabel aLabel = new JLabel("Question.");
aLabel.setOpaque(true);
aLabel.setForeground(Color.blue);
aLabel.setBackground(Color.lightGray);
JLabel bLabel = new JLabel("a.");
bLabel.setOpaque(true);
bLabel.setForeground(Color.blue);
bLabel.setBackground(Color.lightGray);
JLabel cLabel = new JLabel("b.");
cLabel.setOpaque(true);
cLabel.setForeground(Color.blue);
cLabel.setBackground(Color.lightGray);
JLabel dLabel = new JLabel("c.");
dLabel.setOpaque(true);
dLabel.setForeground(Color.blue);
dLabel.setBackground(Color.lightGray);
JLabel eLabel = new JLabel("d.");
eLabel.setOpaque(true);
eLabel.setForeground(Color.blue);
eLabel.setBackground(Color.lightGray);
panlabels.add(aLabel, BorderLayout.WEST);
panlabels.add(bLabel, BorderLayout.CENTER);
panlabels.add(cLabel, BorderLayout.CENTER);
panlabels.add(dLabel, BorderLayout.CENTER);
panlabels.add(eLabel, BorderLayout.CENTER);
//panlabels.add(fLabel, BorderLayout.WEST);
//fLabel.setVisible(false);
JLabel ques = new JLabel("q");
ques.setBackground(Color.red);
choice[1] = new JRadioButton("a");
choice[1].setBackground(Color.red);
choice[2] = new JRadioButton("b");
choice[2].setBackground(Color.red);
choice[3] = new JRadioButton("c");
choice[3].setBackground(Color.red);
choice[4] = new JRadioButton("d");
choice[4].setBackground(Color.red);
ButtonGroup bGroup = new ButtonGroup();
pancontrols.add(ques, BorderLayout.WEST);
for (int i = 1; i < 5; i++) {
// pancontrols.add(aLabel,BorderLayout.WEST);
bGroup.add(choice[i]);
pancontrols.add(choice[i], BorderLayout.WEST);
}
choice[4].setVisible(true);
panEast.add("West", panlabels);
panEast.add("West", pancontrols);
pa.add("Center", panEast);
pa.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(), "Select your answer"));
//getContentPane().add(label);
//to be deleted pa.add("South", pan);
pa.setBackground(Color.pink);
jtfMainFrame.add(pa);
jtfMainFrame.setExtendedState(Frame.MAXIMIZED_BOTH);
jtfMainFrame.setVisible(true);
}
public static void main(String[] args) {
Online r = new Online();
r.createWindow();
}
}
First, your naming convention for the choice labels is off. qLabel="questions", aLabel="questions", bLabel="a", cLabel="b", etc. I would suggest you fix that to eliminate confusion and make your code more readable (ie only have one label that is a question).
Second, your use of panEast.add("West",panlabels); and the other two statements is generally not suggested. Read up on BorderLayout to find the more accepted method of doing this:
http://docs.oracle.com/javase/tutorial/uiswing/layout/border.html
As for your problem, I have rewritten your code so things do line up, I will try to point out what I commented out to help:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//import java.util.Arrays;
public class Online extends JFrame {
static JRadioButton[] choice = new JRadioButton[6];
JFrame jtfMainFrame, jtfMainFrame1;
public void createWindow() {
jtfMainFrame = new JFrame("Online Examination");
jtfMainFrame.setSize(800, 500);
jtfMainFrame.setLocation(200, 150);
jtfMainFrame.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
JPanel pa = new JPanel();
//JPanel panlabels = new JPanel(new GridLayout(0, 1, 0, 60));
JPanel pancontrols = new JPanel(new GridLayout(0, 2, 0, 60));
JPanel panEast = new JPanel();
JPanel pan = new JPanel(new BorderLayout());
JLabel qLabel = new JLabel("Question.");
qLabel.setOpaque(true);
qLabel.setForeground(Color.blue);
qLabel.setBackground(Color.lightGray);
JLabel aLabel = new JLabel("Question.");
aLabel.setOpaque(true);
aLabel.setForeground(Color.blue);
aLabel.setBackground(Color.lightGray);
JLabel bLabel = new JLabel("a.");
bLabel.setOpaque(true);
bLabel.setForeground(Color.blue);
bLabel.setBackground(Color.lightGray);
JLabel cLabel = new JLabel("b.");
cLabel.setOpaque(true);
cLabel.setForeground(Color.blue);
cLabel.setBackground(Color.lightGray);
JLabel dLabel = new JLabel("c.");
dLabel.setOpaque(true);
dLabel.setForeground(Color.blue);
dLabel.setBackground(Color.lightGray);
JLabel eLabel = new JLabel("d.");
eLabel.setOpaque(true);
eLabel.setForeground(Color.blue);
eLabel.setBackground(Color.lightGray);
//panlabels.add(fLabel, BorderLayout.WEST);
//fLabel.setVisible(false);
JLabel ques = new JLabel("q");
ques.setBackground(Color.red);
choice[1] = new JRadioButton("a");
choice[1].setBackground(Color.red);
choice[2] = new JRadioButton("b");
choice[2].setBackground(Color.red);
choice[3] = new JRadioButton("c");
choice[3].setBackground(Color.red);
choice[4] = new JRadioButton("d");
choice[4].setBackground(Color.red);
ButtonGroup bGroup = new ButtonGroup();
//pancontrols.add(new JLabel(""));
pancontrols.add(ques, BorderLayout.WEST);
for (int i = 1; i < 5; i++) {
// pancontrols.add(aLabel,BorderLayout.WEST);
bGroup.add(choice[i]);
}
pancontrols.add(qLabel);
pancontrols.add(ques);
pancontrols.add(bLabel);
pancontrols.add(choice[1]);
pancontrols.add(cLabel);
pancontrols.add(choice[2]);
pancontrols.add(dLabel);
pancontrols.add(choice[3]);
pancontrols.add(eLabel);
pancontrols.add(choice[4]);
pancontrols.setSize(400,200);
choice[4].setVisible(true);
pa.add(pancontrols);
pa.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(), "Select your answer"));
//getContentPane().add(label);
//to be deleted pa.add("South", pan);
pa.setBackground(Color.pink);
jtfMainFrame.add(pa);
jtfMainFrame.setExtendedState(Frame.MAXIMIZED_BOTH);
jtfMainFrame.setVisible(true);
}
public static void main(String[] args) {
Online r = new Online();
r.createWindow();
}
}
Basically, I removed your unnecessary panels. the only panel you add stuff to now is the pancontrols panel. I changed it to new JPanel(new Gridlayout(0,2,0,60)). With the two column GridLayout you will always have things line up bound wise, due to GridLayout making everything the same size.
Lastly I pulled the adding of choices[] from the loop and did that along with the labels to make sure things line up. I removed all the panels being added except the pancontrols being added to pa, which I assume you want to add more question panels to pa in that case. That covers your question in particular, but there is quite a lot of stuff you should do (ie use choice[0] for example, eliminate unused labels and panels, etc.)

Categories

Resources