2012-04-24 107 views
2

enter image description hereenter image description here我想對齊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); 
} 
+2

1)該代碼甚至沒有編譯2)什麼是當前的對齊以及期望的結果是什麼(截圖可以使事情更清晰)3)爲什麼使用AWT組件而不是Swing組件4)請使用self - 爲您的變量描述名稱,以便您的代碼變得可讀。 acttran是'TextField'這個事實並不簡單(對所有其他變量都是一樣的) – Robin 2012-04-24 12:59:47

+1

請提供一個關於你想要什麼以及在哪裏的視覺描述?除了'JApplet'什麼都沒有涉及'Swing' :( – 2012-04-24 13:02:23

+0

請檢查一下,我的小程序如上圖所示,其中的元素沒有正確對齊如何使用java中的Layouts對齊它們我試着用上面提到的代碼但是沒有得到它,我還應該給舊的applet代碼嗎? – 2012-04-24 13:11:35

回答

4

試試這個,這是對你有好處:

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); 
    } 
} 

這裏是輸出:

JAPPLET

+0

非常感謝。我想要這個。再次感謝您的回覆和關注。 – 2012-04-24 14:22:15

+0

你是最受歡迎的,很高興這個例子確實幫了你,保持微笑:-) – 2012-04-24 15:17:04

+0

非常感謝你。還有一個請求:由於topPanel中的每一行(JLabel和JTextField)都非常接近。我試着在每行之間使用setBorder()方法爲每個標籤和TextField添加頂部和底部填充,如您所示。但我沒有得到。是否有其他方法可以實現它? – 2012-04-24 15:25:28

3

當你說這

JPanel cp = new JPanel(); 

這意味着你將自動獲得一個FlowLayout。你可能想要一個different one

編輯:好的,我能看到你給cp一個GroupLayout,但我不明白,你pack()cp是或致電setVisible()窗口。

編輯:但這並不重要的小程序。

3

您目前的代碼看起來很像您使用某種GUI生成器工具生成最終佈局。

爲了實現最大程度的控制,請構建組件並將它們添加到面板。如果您正在尋找使用GridBagLayout中所示的代碼,然後按照順序構建如下圖所示

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); 

或者選擇使用BorderLayout的或FlowLayout中握住你的面板上的組件:

JPanel p1 = new JPanel(new BorderLayout()); 
p1.add(lblUsername, BorderLaout.EAST); 
...