2

ここに画像の説明を入力してくださいここに画像の説明を入力してください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); 
 }
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

あなたがこれを言うとき

JPanel cp = new JPanel();

それはあなたがFlowLayout自動的に取得しようとしていることを意味します。おそらく別のものが必要です。

編集:わかりました。あなたがどこにを与えcpているかはわかりますが、ウィンドウがどこにあるか、またはGroupLayoutどこにあるかはわかりません。pack()cpsetVisible()

編集:しかし、それはアプレットには関係ありません。

于 2012-04-24T13:17:36.010 に答える
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 に答える