2

在此处输入图像描述在此处输入图像描述我正在尝试对齐 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); 
 }
4

3 回答 3

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

这是输出:

杰普莱

于 2012-04-24T14:04:17.293 回答
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);
...
于 2012-04-24T13:41:18.963 回答
3

当你这么说

JPanel cp = new JPanel();

这意味着您将FlowLayout自动获得一个。你可能想要一个不同的

编辑:好的,我可以看到您在哪里提供cpa GroupLayout,但我看不到您所在pack()的窗口所在的位置cp或 call setVisible()

编辑:但这对于小程序无关紧要。

于 2012-04-24T13:17:36.010 回答