setSize-Funktion für JFrame?

  • setSize-Funktion für JFrame?

    Hiho!
    Frage: Wie kann ich denn von einem in Swing programmierten JFrame die Größe ändern?? Also ich hab schon irgendwie mit setSize(int a,int b) gearbeitet... aber bekomms einfach net so hin, dass es funzt...
    Wie muss man denn die Methode richtig implementieren?
    meine Klasse heisst wie folgt:

    Quellcode

    1. JFrame ButtonBox = new JFrame("ButtonBox");
    2. JPanel Hintergrund = new JPanel(new GridLayout(3,1));
    3. JButton Gelb, Rot, Blau;
    4. public void Buttons(){
    5. Gelb = new JButton("faerbe Buttons gelb");
    6. Gelb.setBackground(Color.yellow);
    7. Gelb.addActionListener(this);
    8. Rot = new JButton("faerbe Buttons rot");
    9. Rot.setBackground(Color.yellow);
    10. Rot.addActionListener(this);
    11. Blau = new JButton("faerbe Buttons blau");
    12. Blau.setBackground(Color.yellow);
    13. Blau.addActionListener(this);
    14. Hintergrund.add(Gelb);
    15. Hintergrund.add(Rot);
    16. Hintergrund.add(Blau);
    17. ButtonBox.getContentPane().add(Hintergrund);
    18. ButtonBox.pack();
    19. ButtonBox.setVisible(true);
    20. }
    21. public static void main(String[] args) {
    22. ButtonBox MeinSwing = new ButtonBox();
    23. MeinSwing.Buttons();
    24. }
    25. public void actionPerformed(ActionEvent e){
    26. Object button = e.getSource();
    27. if (button == Gelb) {
    28. this.Gelb.setBackground(Color.yellow);
    29. this.Rot.setBackground(Color.yellow);
    30. this.Blau.setBackground(Color.yellow);
    31. this.Hintergrund.setBackground(Color.yellow);
    32. }
    33. if (button == Rot) {
    34. this.Gelb.setBackground(Color.red);
    35. this.Rot.setBackground(Color.red);
    36. this.Blau.setBackground(Color.red);
    37. this.Hintergrund.setBackground(Color.red);
    38. }
    39. if (button == Blau) {
    40. this.Gelb.setBackground(Color.blue);
    41. this.Rot.setBackground(Color.blue);
    42. this.Blau.setBackground(Color.blue);
    43. this.Hintergrund.setBackground(Color.blue);
    44. }
    45. }
    46. }
    Alles anzeigen


    Ich bin echt ratlos... hab schon allen möglichen scheiss probiert, aber ich komm einfach net drauf. =) Danke schonmal für Hilfe!
    \\.-'´´ Alw4ys k33p on burn!ng, 'cAu$e if you are buRnT out y0ur l!fe sURreNd3Rs deAth! ``'-.//
  • Naja... ohne pack(); macht er gar nix, dann kommt nur die Kopfzeile mit "ButtonBox" drin. Das Problem ist eigentlich nur eine kosmetische Angelegenheit.
    Das Proggi funzt vollkommen so, wie es soll. Aber:
    Von meiner ButtonBox sieht man leider nur das "Butto..."-[]X in der Kopfzeile. Also der Rest von ButtonBox wird von den Minimize/Maximize/Close-Symbolen verdeckt.
    Wenn ich jetzt z.B. mit setSize(300,300) das ganze Ding insgesamt größer machen könnte, dann würde man den kompletten Namen der Box sehen.
    Doch leider hab ichs einfach nicht hinbekommen, die Methode setSize() entsprechend einzubinden, so dass es auch tatsächlich die Größe anpasst (meist bekam ich irgendwelche Fehlermeldungen).
    Außerdem würde ich gerne in diesem Zusammenhang auch das ganze Dings gerne relativ mittig im Bildschirm starten lassen... gibts da sowas wie setVerticalPos(width) und setHorizontalPos(heigth) oder so?? Wenn ja, wie baut man das ein?
    =)
    thx nochmal!
    Danny
    \\.-'´´ Alw4ys k33p on burn!ng, 'cAu$e if you are buRnT out y0ur l!fe sURreNd3Rs deAth! ``'-.//
  • haha und nochmal oberlol!!
    ButtonBox.setLocation(500, 300);
    Das funzt auf einmal... Ich weiss auch net, warum's heut mittag nicht ging.
    Also vielen Danke nochmal, alle Fragen geklärt!! 8) :D
    n1 Support hier... respekt!
    Codest du beruflich @ Donut?
    \\.-'´´ Alw4ys k33p on burn!ng, 'cAu$e if you are buRnT out y0ur l!fe sURreNd3Rs deAth! ``'-.//