我需要将所有字段与各自的标签对齐,
这是我的代码:
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);
}
我尝试通过引用它来实现它,但它不起作用,有什么建议吗?