samedi 9 mai 2015

Event Handling Aus einer anderen Klasse heraus einen Text des JLabels ändern.

Hallo zusammen,
ich arbeite weiter fleißig an meinen Java Projekt zu welchen ich schon eine andere Frage gestellt habe ( http://ift.tt/1Rnf4SS ).
Nun habe ich das Übergeben der Daten aus dem Dialogfeld gemeistert und will die Eingabe der Daten im Hauptfenster Anzeigen. Leider funktioniert der Weg wie ich ihn mir gedacht habe nicht und ich hab auch mal wieder keine Ahnung. Ich erstelle eine Methode in der Hauptklasse jedoch kann ich sie nicht aus der Unterklasse aufrufen.

Ich hänge mal komplett beide Klassen an. Es geht um die Methode updateGUI und den Aufruf eben jener Methode in der neben Klasse.

Danke im vor raus.


Java Code:

  1. package mainprog;
  2. import formulare.Logindata_dialog;
  3. import java.awt.EventQueue;
  4. import javax.swing.JFrame;
  5. import javax.swing.JPanel;
  6. import javax.swing.border.EmptyBorder;
  7. import javax.swing.JLabel;
  8. import javax.swing.SwingConstants;
  9. import java.awt.Font;
  10. import javax.swing.JButton;
  11. import java.awt.Color;
  12. import java.awt.event.ActionListener;
  13. import java.awt.event.ActionEvent;
  14.  
  15. public class WvS_ProjectDB extends JFrame {
  16. private static final long serialVersionUID = 6049042076699458303L;
  17. private JPanel contentPane;
  18. private static String DbIpAdress;
  19. private static String DbPort;
  20. private static String DbName;
  21. private static String DbUser;
  22. private static String DbUserPasswd;
  23. private JLabel lbl_DBname;
  24. private JLabel lbl_PWD;
  25. Logindata_dialog dialog = new Logindata_dialog();
  26.  
  27.  
  28.  
  29.  
  30. public static void main(String[] args) {
  31. EventQueue.invokeLater(new Runnable() {
  32. public void run() {
  33. try {
  34. WvS_ProjectDB frame = new WvS_ProjectDB();
  35. frame.setVisible(true);
  36. } catch (Exception e) {
  37. e.printStackTrace();
  38. }
  39. }
  40. });
  41. }
  42. public WvS_ProjectDB() {
  43. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  44. setBounds(100, 100, 852, 629);
  45. contentPane = new JPanel();
  46. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  47. setContentPane(contentPane);
  48. contentPane.setLayout(null);
  49.  
  50. JLabel lbl_Titel = new JLabel("WvS Techniker Projekt Datenbank");
  51. lbl_Titel.setHorizontalAlignment(SwingConstants.CENTER);
  52. lbl_Titel.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 30));
  53. lbl_Titel.setBounds(15, 16, 800, 81);
  54. contentPane.add(lbl_Titel);
  55.  
  56. lbl_DBname = new JLabel("Keine Datenbank vorgew\u00E4hlt!");
  57. lbl_DBname.setFont(new Font("Tahoma", Font.BOLD, 16));
  58. lbl_DBname.setForeground(Color.RED);
  59. lbl_DBname.setBounds(15, 148, 272, 20);
  60. contentPane.add(lbl_DBname);
  61.  
  62. lbl_PWD = new JLabel("Kein Password eingegeben!");
  63. lbl_PWD.setFont(new Font("Tahoma", Font.BOLD, 16));
  64. lbl_PWD.setForeground(Color.RED);
  65. lbl_PWD.setBounds(15, 187, 244, 20);
  66. contentPane.add(lbl_PWD);
  67.  
  68. JButton btn_login = new JButton("Logindaten eingeben");
  69. btn_login.addActionListener(new ActionListener() {
  70. public void actionPerformed(ActionEvent arg0) {
  71. dialog.setVisible(true);
  72. }
  73. });
  74. btn_login.setBounds(25, 225, 233, 29);
  75. contentPane.add(btn_login);
  76. }
  77. public static String getDbIpAdress() {
  78. return DbIpAdress;
  79. }
  80. public static void setDbIpAdress(String dbIpAdress) {
  81. DbIpAdress = dbIpAdress;
  82. }
  83. public static String getDbPort() {
  84. return DbPort;
  85. }
  86. public static void setDbPort(String dbPort) {
  87. DbPort = dbPort;
  88. }
  89. public static String getDbName() {
  90. return DbName;
  91. }
  92. public static void setDbName(String dbName) {
  93. DbName = dbName;
  94. }
  95. public static String getDbUser() {
  96. return DbUser;
  97. }
  98. public static void setDbUser(String dbUser) {
  99. DbUser = dbUser;
  100. }
  101. public static String getDbUserPasswd() {
  102. return DbUserPasswd;
  103. }
  104. public static void setDbUserPasswd(String dbUserPasswd) {
  105. DbUserPasswd = dbUserPasswd;
  106. }
  107. public void updateGUI() {
  108. lbl_DBname = new JLabel(DbName);
  109. lbl_DBname.setForeground(Color.BLACK);
  110. lbl_PWD = new JLabel("Password vorgewählt");
  111. lbl_PWD.setForeground(Color.BLACK);
  112. }
  113. }


Java Code:

  1. package formulare;
  2. import java.awt.BorderLayout;
  3. import java.awt.FlowLayout;
  4. import javax.swing.JButton;
  5. import javax.swing.JDialog;
  6. import javax.swing.JPanel;
  7. import javax.swing.border.EmptyBorder;
  8. import javax.swing.JPasswordField;
  9. import javax.swing.JTextField;
  10. import javax.swing.JLabel;
  11. import javax.swing.SwingConstants;
  12. import java.awt.event.ActionListener;
  13. import java.awt.event.ActionEvent;
  14. import mainprog.WvS_ProjectDB;
  15. public class Logindata_dialog extends JDialog {
  16.  
  17. private static final long serialVersionUID = -8298102570486583361L;
  18. private final JPanel contentPanel = new JPanel();
  19. private JTextField text_dbHost;
  20. private JTextField text_dbPort;
  21. private JTextField text_dbDatabank;
  22. private JTextField text_dbUser;
  23. private JPasswordField pwd_dbPassword;
  24.  
  25. public static void main(String[] args) {
  26. try {
  27. Logindata_dialog dialog = new Logindata_dialog();
  28. dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  29. dialog.setVisible(true);
  30. } catch (Exception e) {
  31. e.printStackTrace();
  32. }
  33. }
  34.  
  35. public Logindata_dialog() {
  36. setBounds(100, 100, 450, 300);
  37. getContentPane().setLayout(new BorderLayout());
  38. contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
  39. getContentPane().add(contentPanel, BorderLayout.CENTER);
  40. contentPanel.setLayout(null);
  41.  
  42. JLabel lblHost = new JLabel("Host");
  43. lblHost.setBounds(15, 16, 69, 20);
  44. contentPanel.add(lblHost);
  45.  
  46. JLabel lblPort = new JLabel("Port");
  47. lblPort.setBounds(15, 52, 69, 20);
  48. contentPanel.add(lblPort);
  49.  
  50. JLabel lblDatenbank = new JLabel("Datenbank");
  51. lblDatenbank.setBounds(15, 88, 123, 20);
  52. contentPanel.add(lblDatenbank);
  53.  
  54. JLabel lblUser = new JLabel("User");
  55. lblUser.setBounds(15, 124, 69, 20);
  56. contentPanel.add(lblUser);
  57.  
  58. JLabel lblPassword = new JLabel("Password");
  59. lblPassword.setBounds(15, 160, 69, 20);
  60. contentPanel.add(lblPassword);
  61.  
  62. text_dbHost = new JTextField();
  63. text_dbHost.setHorizontalAlignment(SwingConstants.RIGHT);
  64. text_dbHost.setText("localhost");
  65. text_dbHost.setBounds(125, 13, 288, 26);
  66. contentPanel.add(text_dbHost);
  67. text_dbHost.setColumns(10);
  68.  
  69. text_dbPort = new JTextField();
  70. text_dbPort.setHorizontalAlignment(SwingConstants.RIGHT);
  71. text_dbPort.setText("3306");
  72. text_dbPort.setBounds(125, 49, 288, 26);
  73. contentPanel.add(text_dbPort);
  74. text_dbPort.setColumns(10);
  75.  
  76. text_dbDatabank = new JTextField();
  77. text_dbDatabank.setHorizontalAlignment(SwingConstants.RIGHT);
  78. text_dbDatabank.setText("wvs_db");
  79. text_dbDatabank.setBounds(125, 85, 288, 26);
  80. contentPanel.add(text_dbDatabank);
  81. text_dbDatabank.setColumns(10);
  82.  
  83. text_dbUser = new JTextField();
  84. text_dbUser.setHorizontalAlignment(SwingConstants.RIGHT);
  85. text_dbUser.setText("root");
  86. text_dbUser.setBounds(125, 121, 288, 26);
  87. contentPanel.add(text_dbUser);
  88. text_dbUser.setColumns(10);
  89.  
  90. pwd_dbPassword = new JPasswordField();
  91. pwd_dbPassword.setHorizontalAlignment(SwingConstants.RIGHT);
  92. pwd_dbPassword.setBounds(125, 157, 288, 26);
  93. contentPanel.add(pwd_dbPassword);
  94. {
  95. JPanel buttonPane = new JPanel();
  96. buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
  97. getContentPane().add(buttonPane, BorderLayout.SOUTH);
  98. {
  99. JButton okButton = new JButton("OK");
  100. okButton.addActionListener(new ActionListener() {
  101. public void actionPerformed(ActionEvent arg0) {
  102. WvS_ProjectDB.setDbIpAdress(text_dbDatabank.getText());
  103. WvS_ProjectDB.setDbPort(text_dbPort.getText());
  104. WvS_ProjectDB.setDbName(text_dbDatabank.getText());
  105. WvS_ProjectDB.setDbUser(text_dbUser.getText());
  106. char[] zeichen = pwd_dbPassword.getPassword();
  107. String pwd_abfrage = new String(zeichen);
  108. WvS_ProjectDB.setDbUserPasswd(pwd_abfrage);
  109. Logindata_dialog.this.dispose();
  110. }
  111. });
  112. okButton.setActionCommand("OK");
  113. buttonPane.add(okButton);
  114. getRootPane().setDefaultButton(okButton);
  115. }
  116. {
  117. JButton cancelButton = new JButton("Cancel");
  118. cancelButton.addActionListener(new ActionListener() {
  119. public void actionPerformed(ActionEvent arg0) {
  120. Logindata_dialog.this.dispose();
  121. WvS_ProjectDB.updateGUI();
  122. }
  123. });
  124. cancelButton.setActionCommand("Cancel");
  125. buttonPane.add(cancelButton);
  126. }
  127. }
  128. }
  129. }


Event Handling Aus einer anderen Klasse heraus einen Text des JLabels ändern.

0 commentaires:

Enregistrer un commentaire