Can't Get GUI to align right (java)
I am making a simple GUI in java that computes my Grade Point Average so I can use it at the end of the quarter. If I can get this program to work it would help me out and would make my life a bit easier. (plus I want to impress a friend)
My question is that I don't know how to align my panels correctly. I want them to look something like this hideous drawing:
Code:
---------------------------------------------------------
|Title |
|--------------------------------------------------------|
| Class 1 | Class 2 |
|Grade? Credit hour | Grade? Credit hour |
| o B | o 3 | |
| o C | o 4 | |
| o D | o 5 | |
|--------------------------------------------------------|
| Class 4 | Class 5 |
| | |
| | |
| | |
---------------------------------------------------------|
| Calc Clear [ txt box] |
----------------------------------------------------------
I only know how to align this with
getContentPane().setLayout(new BorderLayout());
getContentPane().add(p1, BorderLayout.NORTH);
getContentPane().add(p2, BorderLayout.CENTER);
getContentPane().add(p3, BorderLayout.SOUTH);
And this simply will not work.
This may be a bit hard to decipher but this is my code. Keep in mind that it is not finished, I just wanted to get the structure down before I start adding everything else.
Code:
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class GPAcalc extends JFrame implements ActionListener
{
//Declare Radio buttons and the necessary buttons and text fields
JRadioButton jRBn2,jRBa,jRBb,jRBc,jRBd,jRB2a,jRB2b,jRB2c,jRB2d,jRB3a,jRB3b,jRB3c,jRB3d,jRB4a,jRB4b,jRB4c,jRB4d;
JTextField jTF1;
JButton jB1,jB2;
/*The main method **/
public static void main(String[] args)
{
GPAcalc frame = new GPAcalc();
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
} // End of main method
public GPAcalc()
{
setTitle("GPA Calculator");
JPanel p1 = new JPanel();
ButtonGroup bg = new ButtonGroup();
//add the Radio Buttons to a Panel
p1.setLayout(new GridLayout(5,1));
p1.add(new JLabel("Class 1"));
p1.add(jRBa = new JRadioButton("A",true));
p1.add(jRBb = new JRadioButton("B"));
p1.add(jRBc = new JRadioButton("C"));
p1.add(jRBd = new JRadioButton("D"));
bg.add(jRBa);
bg.add(jRBb);
bg.add(jRBc);
bg.add(jRBd);
JPanel p4 = new JPanel();
ButtonGroup bg1 = new ButtonGroup();
p4.setLayout(new GridLayout(4,1));
p4.add(jRBn2 = new JRadioButton("2"));
bg1.add(jRBn2);
//add the textfield to a panel
JPanel p2 = new JPanel();
p2.setLayout(new FlowLayout());
p2.add(new JLabel("The Button Selected is"));
p2.add(jTF1 = new JTextField(20));
jTF1.setEditable(false);
//add the buttons to a panel
JPanel p3 = new JPanel();
p3.setLayout(new FlowLayout());
p3.add(jB1 = new JButton("Button?"));
p3.add(jB2 = new JButton("Clear"));
// Set FlowLayout for the frame and add panels to the frame
getContentPane().setLayout(new BorderLayout());
getContentPane().add(p1, BorderLayout.NORTH);
getContentPane().add(p2, BorderLayout.CENTER);
getContentPane().add(p3, BorderLayout.SOUTH);
// Register listener
jB1.addActionListener(this);
jB2.addActionListener(this);
//Without the above two lines the buttons will not work
} // End of Constructor
public void actionPerformed(ActionEvent e)
{
int a1=0,a2=0,a3=0,a4=0;
int b1=0,b2=0,b3=0,b4=0;
int c1=0,c2=0,c3=0,c4=0;
int d1=0,d2=0,d3=0,d4=0;
int two1=0,two2=0,two3=0,two4=0;
int thr1=0,thr2=0,thr3=0,thr4=0;
int four1=0,four2=0,four3=0,four4=0;
int five1=0,five2=0,five3=0,five4=0;
if (e.getSource() == jB1)
{
// Get the radio button selected
if (jRBa.isSelected())
{
a1 = 4;
}
else if (jRB2a.isSelected())
{
a2 = 4;
}
else if (jRB3a.isSelected())
{
a3 = 4;
}
else if (jRB4a.isSelected())
{
a4 = 4;
}
// Set result in TextField jTF1
//jTF1.setText(x); FIX THIS!
}
if(e.getSource() == jB2)
{
jTF1.setText(" ");
}
}//End of ActionPerformed
}//End of class
I hope someone out there can help me with this.
Thank you
-Any
Re: Can't Get GUI to align right (java)
Quote:
Originally posted here by Any
I am making a simple GUI in java that computes my Grade Point Average so I can use it at the end of the quarter. If I can get this program to work it would help me out and would make my life a bit easier. (plus I want to impress a friend)
My question is that I don't know how to align my panels correctly. I want them to look something like this hideous drawing:
[...]
I hope someone out there can help me with this.
For the basic layout you want, you have a couple of approaches. For simplicity's sake, you could go with a BorderLayout, but that kinda limits where you can go, so I would go with a GridLayout with two columns and four rows (for now). What I'd end up doing would be:
Code:
------------------------------------------------------
| A |
|-----------------------------------------------------|
| B | C |
|-------------------------|---------------------------|
| D | E |
|-----------------------------------------------------|
| F |
------------------------------------------------------
A and F have a column width property of 2, and would contain the strings, buttons, etc, you want.
B, C, D, and E will contain JPanels that then have the data you want in them nested. Once you get enough Kung foo with Java's gui system, you'll be able to basically use GridBagLayout to do everything you want. ;)
Sun's provided a LOAD of tutorials to promote the use of Java, and one you might find especially useful is the GUI tutorial.
http://java.sun.com/docs/books/tutor...ing/index.html
Specifically, the section on laying out components. ;)
Edit: BTW, give yourself some sanity and make some radio button arrays instead of useing jrbA,B,C,D, etc... You should probably also be giving your list of A, B, C, and D a default value, like C or D. Otherwise you could have situations occur where you are calculating a value from none of the radbuttons. Might be even better to add an "N/A" radio button, and make that the default option, so you can check for it in calculations and ignore the entire sets.