vendredi 22 mai 2015

Swing Registrierte Knöpfe mit Bildern werden nicht angezeigt

Hey ihr,

ich habe das Problem, dass die Knöpfe mit den Bildern nicht geladen werden. Die Dateipfade sind richtig, da es mit nur einem Bild schon mal klappte, jetzt, nachdem ich zwei weitere hinzugefügt habe (insgesamt 4 neue Knöpfe) und sie registrierte, funktionierte das Programm nicht mehr, da es keine Knöpfe lädt.

Ich habe drei verschiedene Klassen:

Hauptklasse "Main.java":
Java Code:

  1. package com.mathis.energiezimmer;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.ItemEvent;
  7. import java.awt.event.ItemListener;
  8.  
  9. import javax.swing.*;
  10.  
  11. @SuppressWarnings("serial")
  12. public class Main extends JFrame implements ActionListener, ItemListener {
  13.  
  14. public static JFrame frame;
  15. public static JPanel panel;
  16. public static Container cp;
  17.  
  18. public Main() {
  19.  
  20. Container cp = getContentPane();
  21. cp.setLayout(null);
  22. cp.setVisible(true);
  23. Knöpfe.buttons();
  24. cp.add(Knöpfe.heizung);
  25. cp.add(Knöpfe.box1);
  26. cp.add(Knöpfe.box2);
  27. cp.add(Knöpfe.box3);
  28. cp.add(Knöpfe.box4);
  29.  
  30. frame = new JFrame();
  31. frame.setLayout(new FlowLayout());
  32. frame.pack();
  33. frame.setVisible(true);
  34. frame.setExtendedState(Frame.MAXIMIZED_BOTH);
  35. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  36. frame.setTitle("Energiezimmer");
  37. frame.setResizable(false);
  38. frame.getContentPane().setBackground(Color.WHITE);
  39.  
  40. Knöpfe.heizung.addActionListener(this);
  41.  
  42. }
  43.  
  44. public static void main(String[] args) {
  45.  
  46. new Main();
  47. Hintergrund.background();
  48. frame.repaint();
  49.  
  50. }
  51.  
  52. public static Checkbox eins, zwei, drei, vier, fünf;
  53.  
  54. @Override
  55. public void actionPerformed(ActionEvent e) {
  56.  
  57. if(e.getSource() == Knöpfe.heizung) {
  58.  
  59. JDialog heizung = new JDialog();
  60.  
  61. eins = new Checkbox("Heizung durchgegend auf 5", cbg, false);
  62. zwei = new Checkbox("Heizung im Sommer auf 4", cbg, false);
  63. drei = new Checkbox("Heizung, wenn sie gebraucht wird, auf 2", cbg, false);
  64. vier = new Checkbox("Heizung immer auf 0", cbg, false);
  65. fünf = new Checkbox("Heizung im Winter auf 5, sonst nur auf 3", cbg, true);
  66.  
  67. eins.addItemListener(this);
  68. zwei.addItemListener(this);
  69. drei.addItemListener(this);
  70. vier.addItemListener(this);
  71. fünf.addItemListener(this);
  72.  
  73. heizung.add(eins);
  74. heizung.add(zwei);
  75. heizung.add(drei);
  76. heizung.add(vier);
  77. heizung.add(fünf);
  78.  
  79. heizung.setLayout(new FlowLayout());
  80. heizung.pack();
  81. heizung.setVisible(true);
  82. heizung.setSize(975, 65);
  83. heizung.setLocationRelativeTo(getParent());
  84. heizung.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  85. heizung.setTitle("Heizung");
  86. heizung.setResizable(false);
  87. heizung.getContentPane().setBackground(Color.LIGHT_GRAY);
  88.  
  89. heizung.setLayout(null);
  90.  
  91. }
  92.  
  93. }
  94.  
  95. @Override
  96. public void itemStateChanged(ItemEvent e) {
  97.  
  98. if(e.getSource() == eins | e.getSource() == zwei | e.getSource() == vier | e.getSource() == fünf) {
  99.  
  100.  
  101.  
  102. }
  103.  
  104. }
  105.  
  106. }


Nebenklasse "Knöpfe.java"
Java Code:

  1. package com.mathis.energiezimmer;
  2.  
  3. import java.awt.*;
  4.  
  5. import javax.swing.*;
  6.  
  7. public class Knöpfe {
  8.  
  9. public static JButton heizung = new JButton();
  10. public static JButton fenster1 = new JButton();
  11. public static JButton fenster2 = new JButton();
  12. public static JButton lampe = new JButton();
  13. public static JButton wasserhahn = new JButton();
  14. public static JButton box1 = new JButton();
  15. public static JButton box2 = new JButton();
  16. public static JButton box3 = new JButton();
  17. public static JButton box4 = new JButton();
  18.  
  19. public static void buttons() {
  20.  
  21. Icon heizungIcon = new ImageIcon(System.getProperty("user.dir")+"/icon/heizung.jpg");
  22. Icon rotIcon = new ImageIcon(System.getProperty("user.dir")+"/farben/rot.jpg");
  23. Icon grünIcon = new ImageIcon(System.getProperty("user.dir")+"/farben/grün.jpg");
  24.  
  25. heizung.setBackground(Color.white);
  26. heizung.setBorder(null);
  27.  
  28. box1.setBackground(Color.white);
  29. box1.setBorder(null);
  30.  
  31. box2.setBackground(Color.white);
  32. box2.setBorder(null);
  33.  
  34. box3.setBackground(Color.white);
  35. box3.setBorder(null);
  36.  
  37. box4.setBackground(Color.white);
  38. box4.setBorder(null);
  39.  
  40. heizung.setIcon(heizungIcon);
  41. box1.setIcon(rotIcon);
  42. box2.setIcon(rotIcon);
  43. box3.setIcon(rotIcon);
  44. box4.setIcon(rotIcon);
  45.  
  46. Knöpfe.heizung.setBounds(50, 375, 400, 250);
  47. Knöpfe.box1.setBounds(50, 50, 50, 50);
  48. Knöpfe.box2.setBounds(50, 105, 50, 50);
  49. Knöpfe.box3.setBounds(50, 160, 50, 50);
  50. Knöpfe.box4.setBounds(50, 215, 50, 50);
  51.  
  52. }
  53.  
  54. }


Nebenklasse "Hintergrund.java":
Java Code:

  1. package com.mathis.energiezimmer;
  2.  
  3. import javax.swing.*;
  4.  
  5. public class Hintergrund {
  6.  
  7. public static void background() {
  8.  
  9. JLabel lblNewLabel = new JLabel("");
  10. lblNewLabel.setIcon(new ImageIcon(System.getProperty("user.dir")+"/background/bg.jpg"));
  11. lblNewLabel.setSize(Main.frame.getWidth(), Main.frame.getHeight());
  12. Main.frame.getContentPane().add(lblNewLabel);
  13. Main.frame.repaint();
  14.  
  15. }
  16.  
  17. }


Schon mal Danke im Vorraus, entweder irgendwas ist grundlegend falsch oder ich bin einfach zu blind ^^

Gruß Maam


Swing Registrierte Knöpfe mit Bildern werden nicht angezeigt

0 commentaires:

Enregistrer un commentaire