所以我们应该为我们的航空公司制作预订屏幕。在用户输入他/她的信息并点击保留后,我的代码需要在单独的框架上显示她的信息。我已经设置了框架,我只需要将JTextField中的信息传输到JLabel上的框架。我听说我需要把它变成一个变量,所以我可以在多个地方使用它。我是Java的新手,所以请不要使用我可能不熟悉的单词。谢谢,这是我的代码。我有3节课。
头等舱:
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TicketMan {
public static void main(String[] args) {
JFrame frame = new JFrame ("Airport");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
TicketManBot panel = new TicketManBot();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true); } }
第二类:(字段所在的类。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
public class TicketManBot extends JPanel {
private JLabel name;
private JLabel dest;
private JLabel depc;
private JLabel dept;
private JLabel seat;
private JButton b;
Random rand = new Random();
private JTextField namef, destf, depcf, deptf;
public TicketManBot()
{
name = new JLabel ("Enter passenger's name:");
dest = new JLabel ("Destination: ");
depc = new JLabel ("Departure city: ");
dept = new JLabel ("Departure time: ");
seat = new JLabel ("Seat Number: " + rand.nextInt(100));
b = new JButton ("Make Reservation");
b.addActionListener (new results());
// the fields I need to move
namef = new JTextField (10);
destf = new JTextField (10);
depcf = new JTextField (10);
deptf = new JTextField (10);
add (name);
add (namef);
add (dest);
add (destf);
add (depc);
add (depcf);
add (dept);
add (deptf);
add (seat);
add (b);
setPreferredSize (new Dimension(200, 300));
setBackground (Color.gray);
}
private class results implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
int resultLabel;
String text = namef.getText();
JFrame results = new JFrame("Thank you for flying with JJ Air");
results.setVisible(true);
results.setSize (200, 300);
// I need to move the fields up there in front of these VVVVVV
JLabel nr = new JLabel ("Name of the passenger:");
JLabel dr = new JLabel ("Destination:");
JLabel depr = new JLabel ("Departure city:");
JLabel dpr = new JLabel ("Departure time:");
JLabel sr = new JLabel ("Seat number:");
JLabel t = new JLabel ("Thank you for flying with us");
JLabel label = new JLabel ("");
JPanel panel = new JPanel ();
results.add(panel);
panel.add (nr);
panel.add (dr);
panel.add (depr);
panel.add (dpr);
panel.add (sr);
panel.add (t);
}
}
}
第三类:
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
public class results extends JPanel {
}
答案 0 :(得分:2)
jLabel.setText(jTextField.getText());
其中jLabel是您的标签,jTextField是您的文本字段。
例如:
JLabel nr = new JLabel ("Name of the passenger:"+Namef.getText());
顺便说一句,您应该阅读Java编码风格指南,这将有助于使您的代码对其他人更具可读性。
答案 1 :(得分:0)
Jlabel是您要将文本设置为的标签。 jtextField是您想从
获取信息的tetxfieldjLabel.setText(jTextField.getText());
您还可以做的是将所有textFields中的所有信息收集到Array中,然后使用它来显示所需jLabel中的数组。或者甚至更好地使用Array在漂亮的表格中显示数据:)