我想對齊Java Applet中的幾個元素,我無法做到這一點。請幫幫我。這裏是我的代碼:Java Applet中的元素對齊
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);
}
1)該代碼甚至沒有編譯2)什麼是當前的對齊以及期望的結果是什麼(截圖可以使事情更清晰)3)爲什麼使用AWT組件而不是Swing組件4)請使用self - 爲您的變量描述名稱,以便您的代碼變得可讀。 acttran是'TextField'這個事實並不簡單(對所有其他變量都是一樣的) – Robin 2012-04-24 12:59:47
請提供一個關於你想要什麼以及在哪裏的視覺描述?除了'JApplet'什麼都沒有涉及'Swing' :( – 2012-04-24 13:02:23
請檢查一下,我的小程序如上圖所示,其中的元素沒有正確對齊如何使用java中的Layouts對齊它們我試着用上面提到的代碼但是沒有得到它,我還應該給舊的applet代碼嗎? – 2012-04-24 13:11:35