我有三个不同的GUI,看起来不同,并在类中存储变量。单击特定按钮时,我不想更改为下一帧。另外,我不能保留变量中的值,例如:在textField中键入内容将文本作为字符串存储在变量中,以便下一个GUI可以使用它。我不确定如何做到这一点因此我在这里问。
第一类gui,应该在按钮点击时更改为下一个类gui。
public class SearchTripList extends JPanel {
private JList list;
private DefaultListModel clientListElements;
// private static String[] colornames = { "black", "blue", "red", "white" };
// private static Color[] colors = { Color.BLACK, Color.BLUE, Color.RED,
// Color.WHITE };
private JTextField tfDest = new JTextField();
private int listRader = 0;
private ResultSet myRs;
private JPanel pnlCenter = new JPanel(new BorderLayout());
private JPanel pnlSouth = new JPanel(new GridLayout(3, 1));
private JPanel pnlNorth = new JPanel(new GridLayout(1, 4));
private String dest;
private JLabel lvlVeckaAvgang = new JLabel("Vecka avgång");
private JLabel lblStad = new JLabel("Destination");
private JLabel lblTidAvgang = new JLabel("Tid avgång");
private JLabel lblAvgangFran = new JLabel ("Avgång från");
private String m = " ";
JButton btnSearch = new JButton("sök");
JButton btnDone = new JButton("Färdigställ");
private Controller controller;
public SearchTripList() throws SQLException {
setLayout(new BorderLayout());
clientListElements = new DefaultListModel();
list = new JList(clientListElements);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setVisibleRowCount(listRader);
pnlCenter.add(new JScrollPane(list));
add(pnlCenter, BorderLayout.CENTER);
add(pnlNorth, BorderLayout.NORTH);
add(pnlSouth, BorderLayout.SOUTH);
pnlNorth.add(lvlVeckaAvgang);
pnlNorth.add(lblStad);
pnlNorth.add(lblTidAvgang);
pnlNorth.add(lblAvgangFran);
// pnlCenter.add(new JScrollPane(list));
// list.addListSelectionListener(new ListSelectionListener() {
// public void valueChanged(ListSelectionEvent event) {
// // setBackground(colors[list.getSelectedIndex()]);
//
// }
// });
pnlSouth.add(tfDest);
pnlSouth.add(btnSearch);
pnlSouth.add(btnDone);
createListeners();
}
public void createListeners() {
Listener list = new Listener();
btnSearch.addActionListener(list);
btnDone.addActionListener(list);
}
private class Listener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnSearch) {
dest = tfDest.getText();
try {
connect();
writeData();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}else if (e.getSource()== btnDone){
// controller.kvitto(); }
}
}
public void writeData() throws SQLException {
String temp = null;
while (myRs.next()) {
listRader++;
temp = myRs.getString("veckaAvgang") + m + myRs.getString("destination")+m+ myRs.getString("avgangTid")+m+myRs.getString("avgangFran");
clientListElements.addElement(temp);
}
}
public void connect() {
try {
// 1 Get a connection to database
java.sql.PreparedStatement myStmt = null;
Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/x", "a", "b");
String sql = "SELECT * FROM Tur WHERE destination = ? ";
// 2. Create a statement
myStmt = myConn.prepareStatement(sql);
myStmt.setString(1, dest);
// 3. Execute SQL query
myRs = myStmt.executeQuery();
System.out.println("Connect");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] a) throws SQLException {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new SearchTripList());
frame.pack();
frame.setSize(400, 400);
frame.setVisible(true);
}
}
第一堂课时,应该出现的第二堂课。
public class PersonInfo extends JPanel {
private JLabel lblForNamn = new JLabel("För namn");
private JLabel lblEfterNamn = new JLabel("Efter namn");
private JLabel lblAdress = new JLabel("Address");
private JLabel lblPersonNr = new JLabel("Person nummer (10 siffror)");
private JLabel lblTelefonNr = new JLabel("Telefon nummer");
private JTextField tfForNamn = new JTextField();
private JTextField tfEfterNamn = new JTextField();
private JTextField tfAdress = new JTextField();
private JTextField tfPersonNr = new JTextField();
private JTextField tfTelefonNr = new JTextField();
private JButton btnVidare = new JButton("Vidare");
private JButton btnAvbryt = new JButton("Avbryt");
private JFrame frame;
private String forNamn;
private String efterNamn;
private String adress;
private String prNr;
private long telefonNr = 0;
private Controller controller;
public PersonInfo() {
setPreferredSize(new Dimension(500, 600));
setLayout(null);
lblForNamn.setLocation(55, 15);
lblForNamn.setSize(80, 50);
add(lblForNamn);
tfForNamn.setLocation(50, 50);
tfForNamn.setSize(400, 50);
add(tfForNamn);
lblEfterNamn.setLocation(55, 100);
lblEfterNamn.setSize(80, 50);
add(lblEfterNamn);
tfEfterNamn.setLocation(50, 135);
tfEfterNamn.setSize(400, 50);
add(tfEfterNamn);
lblAdress.setLocation(55, 185);
lblAdress.setSize(80, 50);
add(lblAdress);
tfAdress.setLocation(50, 220);
tfAdress.setSize(400, 50);
add(tfAdress);
lblPersonNr.setLocation(55, 270);
lblPersonNr.setSize(200, 50);
add(lblPersonNr);
tfPersonNr.setLocation(50, 305);
tfPersonNr.setSize(400, 50);
add(tfPersonNr);
lblTelefonNr.setLocation(55, 355);
lblTelefonNr.setSize(150, 50);
add(lblTelefonNr);
tfTelefonNr.setLocation(50, 390);
tfTelefonNr.setSize(400, 50);
add(tfTelefonNr);
btnVidare.setLocation(275, 500);
btnVidare.setSize(200, 50);
add(btnVidare);
btnAvbryt.setLocation(25, 500);
btnAvbryt.setSize(200, 50);
add(btnAvbryt);
createListeners();
this.controller = controller;
}
public void insertData() {
try {
// 1 Get a connection to database
Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/x", "a", "b");
// 2. Create a statement
Statement myStmt = myConn.createStatement();
// 3. Execute SQL query
String sq1 = "insert into Resenar" + " (personNr, teleNr, adress, forNamn, efterNamn)" + "values(" + prNr
+ ", " + telefonNr + ", '" + adress + "', '" + forNamn + "', '" + efterNamn + "')";
myStmt.executeUpdate(sq1);
System.out.println("Insert complete");
// Namn som primärnyckel fungerade inte så bra då det fanns fler
// namn.
} catch (Exception e) {
e.printStackTrace();
}
}
public void createListeners() {
Listener list = new Listener();
btnVidare.addActionListener(list);
}
private class Listener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnVidare) {
forNamn = tfForNamn.getText();
efterNamn = tfEfterNamn.getText();
adress = tfAdress.getText();
prNr = tfPersonNr.getText();
telefonNr = Integer.parseInt(tfTelefonNr.getText());
insertData();
}else if(e.getSource()==btnAvbryt){
System.exit(0);
}
}
}
public static void main(String[] a) {
PersonInfo pi = new PersonInfo();
JFrame frame = new JFrame("Person information");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new PersonInfo());
frame.pack();
frame.setResizable(false);
frame.setVisible(true);
}
public String getForNamn() {
// TODO Auto-generated method stub
return forNamn;
}
答案 0 :(得分:0)
PersonInfo
在第一个类的构造函数中引用您在main中创建的{{Infobox course
| course_code = PH31011
| course_name = Quantum Mechanics I
| department = [[Physics]]
| credits = 4
| ltp = {{Data bars
| columns = 6 | Jan| Feb| Mar| Apr| May| Jun
| blue|Berries| 65| 38| 72| 47| 20| 30
}}
}}
{{Data bars
| columns = 6 | Jan| Feb| Mar| Apr| May| Jun
| blue|Berries| 65| 38| 72| 47| 20| 30
}}
,然后将其传递给动作侦听器,当您创建新面板form
时,可以将其添加到主框架。
已更新!