使用各自的字段Swing排列标签 [英] Arrange the Label with its respective field Swing

查看:101
本文介绍了使用各自的字段Swing排列标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将所有字段与相应的标签对齐,

I need to align all the fields with the respective labels,

这是我的代码:

     public class Progress extends JPanel implements ActionListener {

 public JLabel ClientIP; JTextField ip;
 JLabel ClientPassword; JTextField pass;
 JLabel Videoname; JTextField vname;
 JLabel perccomplete; JTextField percent;
 JLabel PacketsSent; JTextField pacsent;
 JLabel Connectiontype; JTextField conntype;
 JLabel noofvideossent; JTextField videosend;
 JButton disconnect; JButton refresh;
 JButton ok;

 public Progress() {
 ClientIP = new JLabel("Client's IP:");
 ClientPassword = new JLabel("Clients Password:");
 Videoname = new JLabel("Video being Transfered:");
 perccomplete = new JLabel("% of transfer Complete:");
 PacketsSent = new JLabel("No of Packets sent:");
 Connectiontype = new JLabel("Connection Type:");
 noofvideossent = new JLabel("No of Videos Sent:");
 String Ipad,Ipass,Iselvid;
 if(ClientIPAddr==null || ClientIPAddr.equals("")){
     Ipad="Not Connected";
 }else Ipad=ClientIPAddr.toString();
 if(vFilePassword ==null || vFilePassword.equals("")){
     Ipass="No Password";
 }else Ipass=vFilePassword;
 if(selected_video==null || selected_video.equals("")){
     Iselvid="Not Selected";
 }else Iselvid=selected_video;

 ip = new JTextField(Ipad);
 ip.setColumns(20);
 pass = new JTextField(Ipass);
 pass.setColumns(20);
 vname = new JTextField(Iselvid);
 vname.setColumns(20);
 percent = new JTextField("10%");
 percent.setColumns(20);
 pacsent =new JTextField(String.valueOf(RTSPSeqNb));
 pacsent.setColumns(20);
 String c;
 if(clientConnected==true)
     c="TCP";
 else c="not Connected";
 conntype = new JTextField(c);
 conntype.setColumns(20);
 videosend = new JTextField(String.valueOf(videocount));
 videosend.setColumns(20);

     //Tell accessibility tools about label/textfield pairs.
 ClientIP.setLabelFor(ip);
 ClientPassword.setLabelFor(pass);
 Videoname.setLabelFor(vname);
 perccomplete.setLabelFor(percent);
 PacketsSent.setLabelFor(pacsent);
 Connectiontype.setLabelFor(conntype);
 noofvideossent.setLabelFor(videosend);

 //Lay out the labels in a panel.
    JPanel labelPane = new JPanel(new GridLayout(0,1));
    labelPane.add(ClientIP);
    labelPane.add(ClientPassword);
    labelPane.add(Videoname);
    labelPane.add(perccomplete);
    labelPane.add(PacketsSent);
    labelPane.add(Connectiontype);
    labelPane.add(noofvideossent);

     //Layout the text fields in a panel.
    JPanel fieldPane = new JPanel(new GridLayout(0,1));
    fieldPane.add(ip);
    fieldPane.add(pass);
    fieldPane.add(vname);
    fieldPane.add(percent);
    fieldPane.add(pacsent);
    fieldPane.add(conntype);
    fieldPane.add(videosend);
    //Put the panels in this panel, labels on left,
    //text fields on right.
    //setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));


    JPanel buttonPane = new JPanel(new GridLayout(0,1));
    disconnect = new JButton("Disconnect Client");
    disconnect.setActionCommand("Disconnect");
    disconnect.addActionListener(this);
    refresh = new JButton("Refresh Details");
    refresh.setActionCommand("refresh");
    refresh.addActionListener(this);
    ok = new JButton("OK");
    ok.setActionCommand("ok");
    ok.addActionListener(this);
    buttonPane.add(refresh);
    buttonPane.add(disconnect);
    buttonPane.add(ok);
    add(labelPane, BorderLayout.CENTER);
    add(fieldPane, BorderLayout.LINE_END);
    add(buttonPane, BorderLayout.AFTER_LAST_LINE);
 }


  private void createAndShowGUI() {
    //Create and set up the window.
    frame = new JFrame("Connected Client's Details");
    //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    //Add contents to the window.
    frame.add(new Progress());
    //Display the window.
    frame.pack();
    frame.setVisible(true);
}

我尝试通过引用它但它无法正常工作,有什么建议吗?

I tried going it by referencing it but it was not working, any suggestions ?

推荐答案

有几种选择:

BoxLayout 此处

JTable ,讨论了这里

这篇关于使用各自的字段Swing排列标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆