ERLEDIGT
JA
JA
ANTWORTEN
12
12
ZUGRIFFE
725
725
EMPFEHLEN
-
27.01.06 10:15 #1
Hallo Leute,
nachdem ich nun schon Verschiedenes probiert habe, weiß ich leider nicht mehr weiter.
Mein Layout soll sich der Fenstergröße proportional anpassen, daher ein Layout-Manager.
Obere Zeile 1. Viertel Beschreibung, Rest: Eingabefeld, genauso auch die zweite Zeile, das klappt auch. Der schwierige Teil ist jetzt: Im ersten Viertel soll oben "Text", darunter "Zeichenanzahl" und darunter ein Eingabefeld sein. Der restliche Platz soll komplett mit einem Textarea gefüllt sein (siehe auch Quellcode). Darunter kommt noch ein Button, das klappt auch. Leider bekomme ich den Teil mit dem Textarea nicht hin. Was mache ich falsch?
Code :1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
public void init() { GridBagLayout gb = new GridBagLayout(); GridBagConstraints gbc = new GridBagConstraints(); gbc.fill = GridBagConstraints.BOTH; gbc.weightx = 1.0; gbc.weighty = 1.0; gbc.gridwidth = 1; this.add(new Label("int:"), gb, gbc); gbc.weightx = 3.0; gbc.weighty = 1.0; gbc.gridwidth = GridBagConstraints.REMAINDER; this.tfInt = new TextField(); this.add(tfInt, gb, gbc); gbc.weightx = 1.0; gbc.weighty = 1.0; gbc.gridwidth = 1; this.add(new Label("long:"), gb, gbc); gbc.weightx = 3.0; gbc.weighty = 1.0; gbc.gridwidth = GridBagConstraints.REMAINDER; this.tfLong = new TextField(); this.add(tfLong, gb, gbc); gbc.weightx = 1.0; gbc.weighty = 1.0; gbc.gridwidth = 1; this.add(new Label("Text:"), gb, gbc); gbc.weightx = 3.0; gbc.weighty = 3.0; gbc.gridheight = 3; gbc.gridwidth = GridBagConstraints.REMAINDER; this.taText = new TextArea(); this.add(taText, gb, gbc); gbc.weightx = 1.0; gbc.weighty = 1.0; gbc.gridwidth = GridBagConstraints.REMAINDER; gbc.gridheight = 1; this.add(new Label("Zeichenanzahl:"), gb, gbc); gbc.weightx = 1.0; gbc.weighty = 1.0; gbc.gridwidth = GridBagConstraints.REMAINDER; this.tfTextLength = new TextField(); this.add(tfTextLength, gb, gbc); gbc.weightx = 4.0; gbc.weighty = 1.0; gbc.gridwidth = GridBagConstraints.REMAINDER; this.bRandom = new Button("Generieren"); bRandom.addActionListener(this); this.add(bRandom, gb, gbc); this.setLayout(gb); } private void add(Component c, GridBagLayout gb, GridBagConstraints gbc) { gb.setConstraints(c, gbc); this.add(c); }CU schnuffie
Fragliche Fragen stellende Fragensteller sind für verantwortungslose Antworten antwortender verantwortlicher Antworter selbst verantwortlich.
-
Hallo Schnuffie,
ich habe deinen Code etwas verunstaltet und dein Layout so verändert wie ich es von deiner Beschreibung verstanden habe.
Code :1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
import java.awt.*; import javax.swing.JApplet; public class Test extends JApplet { TextField tfInt, tfLong, tfTextLength; TextArea taText; Button bRandom; public void init() { setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.fill = GridBagConstraints.BOTH; gbc.weightx = 1; gbc.weighty = 1; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.gridx = 0; gbc.gridy = 0; add(new Label("int:"), gbc); gbc.weightx = 3; gbc.gridx = 1; gbc.gridwidth = 3; tfInt = new TextField(); add(tfInt, gbc); gbc.weightx = 1; gbc.gridwidth = 1; gbc.gridx = 0; gbc.gridy = 1; add(new Label("long:"), gbc); gbc.weightx = 3; gbc.gridx = 1; gbc.gridwidth = 4; tfLong = new TextField(); add(tfLong, gbc); gbc.weightx = 1; gbc.gridwidth = 1; gbc.gridx = 0; gbc.gridy = 2; add(new Label("Text:"), gbc); gbc.gridy = 3; add(new Label("Zeichenanzahl:"), gbc); gbc.gridy = 4; tfTextLength = new TextField(); add(tfTextLength, gbc); gbc.weightx = 3; gbc.gridheight = 3; gbc.gridwidth = 3; gbc.gridx = 1; gbc.gridy = 2; taText = new TextArea(); add(taText, gbc); gbc.weightx = 4; gbc.gridwidth = 4; gbc.gridx = 0; gbc.gridy = 5; bRandom = new Button("Generieren"); add(bRandom, gbc); } }
Wenn es doch anders aussehen sollte, könntest du ja vielleicht eine kleine Skizze uploaden?
Vg Erdal
-
Das wäre in der Tat hilfreich.
Zitat von flashray
Ein kleiner Tipp noch zum GBL:
Es ist oftmals sinnvoll, bei vielen Komponenten mehrere Panel wie Zeilen zu verwenden und auf diesen einige Komponenten zu platzieren. Und hin und wieder erfüllen Dummy-Panel und Dummy-Label ihren Zweck, um andere Komponenten in Ecken oder Seiten auszurichten.
-
30.01.06 12:54 #4
- Registriert seit
- Jun 2002
- Ort
- Saarbrücken (Saarland)
- Beiträge
- 9.886
- Blog-Einträge
- 29
Hallo!
Also ich wuerde nicht empfehlen unnoetig viele JPanels zu verwenden... es gibt ja Gott sei dank neben GridBagLayout noch viele andere LayoutManager... wie beispielsweise das huebsche SpringLayout
Gruss TomJava rocks!
How to become a good Java Programmer?
Does IT in Java and .Net
The only valid measurement of code quality: WTFs / minute
Blog
Xing
Twitter
-
Warum nicht?
Zitat von Thomas Darimont
Die Beispiele im Sun Tutorial sehen aber etwas spartanisch aus. Ist das genau so mächtig wie das GBL?
Zitat von Thomas Darimont
-
31.01.06 16:54 #6
Danke Euch allen.
Mit den x-/y-Angaben funktioniert's jetzt.
Code :1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
import java.applet.Applet; import java.awt.*; import java.util.Random; public class RandomApplet extends Applet implements ActionListener { private TextField tfInt; private TextField tfLong; private TextArea taText; private TextField tfTextLength; private Checkbox cbOnlySpace; private Checkbox cbWithNumeric; private Button bRandom; private Random r; public void init() { GridBagLayout gb = new GridBagLayout(); GridBagConstraints gbc = new GridBagConstraints(); gbc.fill = GridBagConstraints.HORIZONTAL; gbc.ipadx = 5; gbc.ipady = 1; gbc.weightx = 1.0; gbc.weighty = 1.0; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.gridx = 0; gbc.gridy = 0; this.add(new Label("int:"), gb, gbc); gbc.weightx = 3.0; gbc.weighty = 1.0; gbc.gridwidth = GridBagConstraints.REMAINDER; gbc.gridheight = 1; gbc.gridx = 1; gbc.gridy = 0; this.tfInt = new TextField(60); this.add(tfInt, gb, gbc); gbc.weightx = 1.0; gbc.weighty = 1.0; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.gridx = 0; gbc.gridy = 1; this.add(new Label("long:"), gb, gbc); gbc.weightx = 3.0; gbc.weighty = 1.0; gbc.gridwidth = GridBagConstraints.REMAINDER; gbc.gridheight = 1; gbc.gridx = 1; gbc.gridy = 1; this.tfLong = new TextField(60); this.add(tfLong, gb, gbc); gbc.weightx = 1.0; gbc.weighty = 1.0; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.gridx = 0; gbc.gridy = 2; this.add(new Label("Text:"), gb, gbc); this.tfTextLength = new TextField(6); Panel p = new Panel(new FlowLayout(FlowLayout.LEFT, 0, 0)); p.add(tfTextLength); p.add(new Label("Zeichen")); gbc.weightx = 1.0; gbc.weighty = 1.0; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.gridx = 0; gbc.gridy = 3; this.add(p, gb, gbc); gbc.weightx = 1.0; gbc.weighty = 1.0; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.gridx = 0; gbc.gridy = 4; this.cbWithNumeric = new Checkbox("mit Ziffern"); this.add(cbWithNumeric, gb, gbc); gbc.weightx = 1.0; gbc.weighty = 1.0; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.gridx = 0; gbc.gridy = 5; this.cbOnlySpace = new Checkbox("ohne Leerzeichen"); this.add(cbOnlySpace, gb, gbc); gbc.weightx = 3.0; gbc.weighty = 1.0; gbc.gridwidth = GridBagConstraints.REMAINDER; gbc.gridheight = 4; gbc.gridx = 1; gbc.gridy = 2; this.taText = new TextArea("", 6, 60, TextArea.SCROLLBARS_VERTICAL_ONLY); this.add(taText, gb, gbc); gbc.fill = GridBagConstraints.NONE; gbc.weightx = 4.0; gbc.weighty = 1.0; gbc.gridwidth = GridBagConstraints.REMAINDER; gbc.gridheight = GridBagConstraints.REMAINDER; gbc.gridx = 0; gbc.gridy = 6; this.bRandom = new Button("Generieren"); bRandom.addActionListener(this); this.add(bRandom, gb, gbc); this.setLayout(gb); } public void start() { this.r = new Random(); } private void add(Component c, GridBagLayout gb, GridBagConstraints gbc) { gb.setConstraints(c, gbc); this.add(c); } public void actionPerformed(ActionEvent ae) { tfInt.setText(String.valueOf(Math.abs(r.nextInt()))); tfLong.setText(String.valueOf(Math.abs(r.nextLong()))); try { int max = Integer.parseInt(tfTextLength.getText()); StringBuffer sb = new StringBuffer(); while (sb.length() < max) { sb.append((char)Math.abs(r.nextInt(Byte.MAX_VALUE))); } taText.setText(sb.toString()); } catch (Exception e) { taText.setText(""); } } }CU schnuffie
Fragliche Fragen stellende Fragensteller sind für verantwortungslose Antworten antwortender verantwortlicher Antworter selbst verantwortlich.
-
Moin,
interessant. So weit ich weiß, liegen weight-Werte zwischen 0 und 1. Was passiert, wenn Du
gbc.weightx = 3.0;
änderst auf
gbc.weightx = 1.0;
?
-
Hallo Snape,
Vom SunTutorial: (Siehe ab "Larger numbers...")
"Generally weights are specified with 0.0 and 1.0 as the extremes: the numbers in between are used as necessary. Larger numbers indicate that the component's row or column should get more space. For each column, the weight is related to the highest weightx specified for a component within that column, with each multicolumn component's weight being split somehow between the columns the component is in. Similarly, each row's weight is related to the highest weighty specified for a component within that row. Extra space tends to go toward the rightmost column and bottom row."
Vg Erdal
-
Beispielhaft:
Fall 1:
Spalte 1
gbc.weightx = 0.0;
Spalte 2
gbc.weightx = 1.0;
Spalte 2 bekommt den restlichen Platz vollständig zugewiesen
Fall 2:
Spalte 1
gbc.weightx = 1.0;
Spalte 2
gbc.weightx = 1.0;
Spalte 1 und 2 bekommen den gleichen Anteil vom Rest zugeteilt
Fall 3:
Spalte 1
gbc.weightx = 1.0;
Spalte 2
gbc.weightx = 3.0;
Spalte 2 bekommt verhältnismäßig 3 mal soviel wie Spalte 1 vom restlichen Platz.
Vg Erdal
-
01.02.06 13:39 #10
- Registriert seit
- Jun 2002
- Ort
- Saarbrücken (Saarland)
- Beiträge
- 9.886
- Blog-Einträge
- 29
Hallo,
Weil man zum einen dadurch den Quellcode extremst unleserlich macht, die Swing Anwendung unnoetig aufblaeht. Um zu verstehen was ich meine muss man sich einfach nur die obigen Beispiele des GridBagLayouts ansehen...Zitat von Thomas Darimont
Hallo!
Also ich wuerde nicht empfehlen unnoetig viele JPanels zu verwenden...
...
Warum nicht?
Zitat:
Zitat von Thomas Darimont
es gibt ja Gott sei dank neben GridBagLayout noch viele andere LayoutManager... wie beispielsweise das huebsche SpringLayout
Gruss Tom
Ja, IMHO kann man mit dem SpringLayout genausoviel machen wie mit dem GridBagLayout.Die Beispiele im Sun Tutorial sehen aber etwas spartanisch aus. Ist das genau so mächtig wie das GBL?
Gruss TomJava rocks!
How to become a good Java Programmer?
Does IT in Java and .Net
The only valid measurement of code quality: WTFs / minute
Blog
Xing
Twitter
-
Ich hätte in letztem Fall 0.1 und 0.3 genommen. So ist es wohl auch eigentlich gedacht. Ich wusste nur nicht, ob Werte über 1.0 einfach nur auch als 1.0 betrachtet werden und 1.0 die Obergrenze ist.
-
>Weil man zum einen dadurch den Quellcode extremst unleserlich macht, die Swing Anwendung unnoetig aufblaeht. Um zu verstehen was ich meine muss man sich einfach nur die obigen Beispiele des GridBagLayouts ansehen...
Man kann auch eigene Panel bauen und auf diese im Frame nur noch zugreifen.
Das ist angenehme und sinnvolle OOP - natürlich nur, wenn auf der GUI mehr als 3 Buttons, 5 Textfelder und 5 Label angezeigt werden sollen...
>Ja, IMHO kann man mit dem SpringLayout genausoviel machen wie mit dem GridBagLayout.
>Gruss Tom
Ich kenne niemanden, der damit arbeitet. Ich weiss auch keinen Grund, weshalb ich das GBL an die Seite legen sollte, um mich ins SpringLayout einzuarbeiten. Kann es irgendetwas mehr als das GBL?
-
06.02.06 09:45 #13
@Snape: SpringLayout kenne ich auch nicht.
Ich habe mir bei größeren Oberflächen angewöhnt, einzelne Abschnitte zu bilden, also sowas, wie z.B. ein AddressPanel, das dann eben zwei Textfelder für Name, eins für Anschrift und eins für die PLZ incl. Feldbeschriftungen mit eigenem Layoutmanager besitzt. Damit habe ich zum Schluß immer eine überschaubare Anzahl Panels, die ich sehr übersichtlich in der Frame-Klasse "verbauen" kann. Natürlich sind das dann mehr Panels "als nötig", jedoch dient das einer viel größeren Übersichtlichkeit. Was Applets angeht, weiche ich allerdings manchmal davon ab, wenn es möglich und sinnvoll ist, mit einer Klasse auszukommen (Minianwendungen).
CU schnuffie
Fragliche Fragen stellende Fragensteller sind für verantwortungslose Antworten antwortender verantwortlicher Antworter selbst verantwortlich.
Ähnliche Themen
-
Probleme mit GridBagLayout
Von HeinerPyt im Forum Swing, Java2D/3D, SWT, JFaceAntworten: 1Letzter Beitrag: 03.12.10, 10:16 -
Probleme mit GridBagLayout
Von Futzel im Forum Swing, Java2D/3D, SWT, JFaceAntworten: 1Letzter Beitrag: 27.04.10, 16:25 -
GridBagLayout probleme
Von Tithilion im Forum Swing, Java2D/3D, SWT, JFaceAntworten: 2Letzter Beitrag: 22.08.08, 07:59 -
GridBagLayout
Von MScalli im Forum JavaAntworten: 3Letzter Beitrag: 15.02.08, 10:15 -
Probleme mit GridBagLayout und JScrollPane
Von Romsl im Forum Swing, Java2D/3D, SWT, JFaceAntworten: 5Letzter Beitrag: 02.08.05, 23:18





Zitieren

Login





