Reputation: 727
I am trying to align several elements in a Java Applet, I am not able to do it. Please help me. Here is my code:
public class User extends JApplet implements ActionListener,ItemListener {
public int sentp,recievedp,corruptedp;
public String stetus;
public double energi,nodeid,en;
TextField name,time,initene,ampco,acttran;
Button rsbt,vlbt,insd ;
Choice ch,ch2;
public String patrn;
public void init() {
this.setSize(800,600);
Label namep= new Label("\n\nEnter the No. of Nodes: ", Label.CENTER);
name = new TextField(5);
Label timep = new Label("\n\nEnter time of Simulation: ",Label.CENTER);
time = new TextField(5);
Label initen = new Label("\n\nInitial Energy Of Each Sensor Node:");
initene = new TextField("10^-4 Joules");
initene.setEditable(false);
Label ampcon = new Label("\n\nAmplifier Constant:");
ampco = new TextField("10^-12 Joules");
ampco.setEditable(false);
Label acttrans = new Label("\n\nEnergy Required To Activate Transmitter/Reciever:");
acttran = new TextField("50 ^ -9 Joules");
acttran.setEditable(false);
Label chp = new Label("\n\nSelect Radio Model:",Label.CENTER);
rsbt = new Button("Run The Simulation");
ch= new Choice();
ch.add("");
ch.add("Gaussian RadioModel");
ch.add("Rayleigh RadioModel");
Label pat= new Label("\n\nDistribution Pattern");
ch2= new Choice();
ch2.add("");
ch2.add("Rectangular Pattern");
ch2.add("Linear Pattern");
ch2.add("Random Pattern");
vlbt=new Button("ViewLog");
insd=new Button("Details of node");
JPanel cp = new JPanel();
this.add(cp);
GridBagLayout gridb = new GridBagLayout();
Container cont = getContentPane();
cont.setLayout(gridb);
add(cp);
GroupLayout layout = new GroupLayout(cp);
cp.setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
layout.setVerticalGroup(
layout.createSequentialGroup()
.addGroup(layout.createParallelGroup()
.addComponent(namep)
.addComponent(name)
.addComponent(rsbt))
.addGroup(layout.createSequentialGroup()
.addComponent(timep)
.addComponent(time)
.addComponent(vlbt))
.addGroup(layout.createSequentialGroup()
.addComponent(chp)
.addComponent(ch))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(pat)
.addComponent(ch2))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(initen)
.addComponent(initene))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(ampcon)
.addComponent(ampco))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(acttrans)
.addComponent(acttran))
);
rsbt.addActionListener(this);
vlbt.addActionListener(this);
insd.addActionListener(this);
ch.addItemListener(this);
ch2.addItemListener(this);
}
Upvotes: 2
Views: 4545
Reputation: 7126
When you say this
JPanel cp = new JPanel();
it means you're going to get a FlowLayout
automatically. You probably want a different one.
Edit: OK, I can see where you're giving cp
a GroupLayout
, but I don't see where you pack()
the window that cp
is in or call setVisible()
.
Edit: But that doesn't matter for an applet.
Upvotes: 3
Reputation: 24626
Try this, is this good for you :
import java.awt.*;
import javax.swing.*;
public class AppletLayout extends JApplet
{
private JTextField noNodeField;
private JTextField timeSimField;
private JTextField iniEngField;
private JTextField ampConsField;
private JTextField engReqField;
private JComboBox selModeCombo;
private JComboBox distPattCombo;
private JButton runSimButton;
private JButton logButton;
private JButton detailNodeButton;
private String[] selectionModelString = {"", "Gaussian RadioModel"
, "Rayleigh RadioModel"};
private String[] distPatternString = {"", "Rectangular Pattern"
, "Linear Pattern"
, "Random Pattern"};
public void init()
{
try
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndDisplayGUI();
}
});
}
catch(Exception e)
{
System.err.println("Unable to Create and Display GUI : " + e.getMessage());
e.printStackTrace();
}
}
private void createAndDisplayGUI()
{
JLabel noNodeLabel = new JLabel("Enter the No. of Nodes :", JLabel.LEFT);
noNodeField = new JTextField(5);
JLabel timeSimLabel = new JLabel("Enter time of Simulation :", JLabel.LEFT);
timeSimField = new JTextField(5);
JLabel iniEngLabel = new JLabel("Initial Energy Of Each Sensor Node :", JLabel.LEFT);
iniEngField = new JTextField("10^-4 Joules");
JLabel ampConsLabel = new JLabel("Amplifier Constant :", JLabel.LEFT);
ampConsField = new JTextField("10^-12 Joules");
JLabel engReqLabel = new JLabel("Energy Required To Activate Transmitter/Reciever :", JLabel.LEFT);
engReqField = new JTextField("50 ^ -9 Joules");
JLabel selModeLabel = new JLabel("Select Radio Model :", JLabel.LEFT);
selModeCombo = new JComboBox(selectionModelString);
JLabel distPattLabel = new JLabel("Distribution Pattern :", JLabel.LEFT);
distPattCombo = new JComboBox(selectionModelString);
runSimButton = new JButton("Run Simulation");
logButton = new JButton("View Log");
detailNodeButton = new JButton("Details of Node");
JComponent contentPane = (JComponent) getContentPane();
JPanel topPanel = new JPanel();
topPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
topPanel.setLayout(new GridLayout(0, 2));
topPanel.add(noNodeLabel);
topPanel.add(noNodeField);
topPanel.add(timeSimLabel);
topPanel.add(timeSimField);
topPanel.add(iniEngLabel);
topPanel.add(iniEngField);
topPanel.add(ampConsLabel);
topPanel.add(ampConsField);
topPanel.add(engReqLabel);
topPanel.add(engReqField);
topPanel.add(selModeLabel);
topPanel.add(selModeCombo);
topPanel.add(distPattLabel);
topPanel.add(distPattCombo);
JPanel buttonPanel = new JPanel();
buttonPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
//buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
buttonPanel.setLayout(new GridLayout(0, 1, 5, 5));
buttonPanel.add(runSimButton);
buttonPanel.add(logButton);
buttonPanel.add(detailNodeButton);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout(5, 5));
mainPanel.add(topPanel, BorderLayout.CENTER);
mainPanel.add(buttonPanel, BorderLayout.LINE_END);
contentPane.add(mainPanel, BorderLayout.PAGE_START);
setSize(1000, 600);
}
}
Here is the output :
Upvotes: 4
Reputation: 12538
Your current code looks pretty much like you are using some sort of GUI builder tool to generate the final layout.
For maximum control, build the component and add them to the panel. If you are looking to use GridBagLayout as shown in your code then follow the sequence construct as shown below
JPanel p1 = new JPanel(new GridBagLayout());
GridBagConstraints cs = new GridBagConstraints();
cs.fill = GridBagConstraints.HORIZONTAL;
lblUsername = new JLabel("Name: ");
cs.gridx = 0;
cs.gridy = 0;
cs.gridwidth = 1;
p1.add(lblUsername, cs);
txtUsername = new JTextField("", 20);
cs.gridx = 1;
cs.gridy = 0;
cs.gridwidth = 2;
p1.add(txtUsername, cs);
...
add(cp);
Or alternatively use BorderLayout or FlowLayout to hold the components on your panel:
JPanel p1 = new JPanel(new BorderLayout());
p1.add(lblUsername, BorderLaout.EAST);
...
Upvotes: 3