javaapplet中的元素对齐

javaapplet中的元素对齐,java,swing,applet,alignment,layout-manager,Java,Swing,Applet,Alignment,Layout Manager,我试图在Java小程序中对齐多个元素,但我无法做到这一点。请帮帮我。这是我的密码: public class User extends JApplet implements ActionListener,ItemListener { public int sentp,recievedp,corruptedp; public String stetus; public double energi,nodeid,en;

我试图在Java小程序中对齐多个元素,但我无法做到这一点。请帮帮我。这是我的密码:

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); 
 }
当你这么说的时候

JPanel cp = new JPanel();
这意味着您将自动获得一个
FlowLayout
。你可能想要一个

编辑:好的,我可以看到你在哪里给
cp
a
GroupLayout
,但是我看不到你
pack()
cp所在的窗口或调用
setVisible()


编辑:但这对小程序并不重要。

您当前的代码看起来很像是在使用某种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);
...

试试这个,这对你有好处吗:

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);
    }
}
以下是输出:


1)该代码甚至没有编译2)当前对齐方式是什么,预期结果是什么(屏幕截图肯定会让事情变得更清楚)3)为什么使用AWT组件而不是Swing组件4)请为变量使用自描述名称,以便代码变得可读。事实上,
acttran
是一个
TextField
并不简单(对于所有其他变量都是一样的)。请提供一个直观的描述,说明您到底想要什么以及在哪里?除了
JApplet
之外,与
Swing
没有任何关系:(请检查它。我的小程序如上图所示。其中的元素没有正确对齐。如何使用java布局对齐它们?我尝试了上述代码,但没有得到它。我是否也要给出旧的小程序代码?在屏幕截图中,它们对齐(水平),但显然不是您想要的对齐方式。请描述您想要实现的对齐方式(即使是MS Paint模型也会澄清此问题)。另外,请查看可能包含您选择的路线示例的want@Robin:我想将它们对齐,如下图所示。您想要我编写的代码,以便您指出要进行的修改吗?非常感谢。我想要这一个。再次感谢您的回复和关注。非常欢迎您,很高兴这次考试mple确实帮助过你,保持微笑:-)非常感谢你。请再说一遍:因为topPanel中的每一行(JLabel和JTextField)都非常接近。我尝试为每个标签和文本字段使用setBorder()方法在行之间添加顶部和底部填充,如图所示。但是我没有得到。有没有其他方法来实现它?只需更改这一行
topPanel.setLayout(新的GridLayout(0,2))到这个
topPanel.setLayout(新的GridLayout(0,2,15,15))
或提供除
15
之外的任何内容,这些内容可以满足您的需求,看起来:-)。我希望这就是您所指的:-)@Doug:
内容窗格
基本上是一个
容器
,作为顶级容器上的
窗格之一出现,可以在中找到更多信息
getContentPane()
只返回顶级容器的
Container
对象,即
JFrame/JWindow/JDialog
。将各种组件添加到
JPanel
中,然后将此
JPanel
设置为顶级容器的内容窗格是一种很好的做法,而不是像我在本例中所做的那样。我只是修改了一点OP的代码