java-forum.org - Java programmieren aus Leidenschaft

Zurück   java-forum.org - Java programmieren aus Leidenschaft > Java - Programmierung > AWT, Swing, JavaFX & SWT

AWT, Swing, JavaFX & SWT Themen zur Programmierung von Benutzer-Oberflächen (GUI, Applet) sowie zur Grafikprogrammierung (2D / 3D)

Thema geschlossen    
Themen-Optionen Thema durchsuchen Ansicht
Alt 15.09.2009, 11:41   #1 (permalink)
Benutzer
int
 
Registriert seit: 02.06.2009
Fachbeiträge: 63
Abgegebene Danke: 0
Erhielt 0 Danke für 0 Beiträge
Standard GridbagLayout

Hallo,

ich hab noch nicht so viel Erfahrung mit Java GUI Programmierung. Mache von mir aus gerade ein Projekt wobei ich das sehr viel brauch. Also ich hab ein Panel in dem eine Toolbar und mehrere Textfelder sind. In meiner Klasse ShowViewer waren zum Test 2 Textfelder. Da ich bis zu 20-25 Textfelder brauch habe ich mir überlegt ein Gridbaglayout zu nehmen, da ich die Größe besser zuordnen kann. Und auch die Toolbar besser reinpasst.

Leider habe ich noch nicht so viel mit Gridbaglayouts gearbeitet. Anhand einem Beispiels habe ich mir das Gridbaglayout so gebaut wie ich möchte, nur ich hab keine Ahnung wie ich es mir umbauen kann)! denn in meinem Fall soll das kein FRame sein sondern ein Panel. Es soll so sein wie im ShowViewer nur das Gridbaglayout soll rein.

Klasse mit GridbagLayout

Java Code: Quelltext in neuem Fenster öffnen
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
import java.awt.*;
import javax.swing.*;
 
public class GridBagLayoutDemo
 
{
 
    
    
    static void addComponent( Container cont,
                            GridBagLayout gbl,
                            Component c,
                            int x, int y,
                            int width, int height,
                            double weightx, double weighty )
  {
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.BOTH;
    gbc.gridx = x; gbc.gridy = y;
    gbc.gridwidth = width; gbc.gridheight = height;
    gbc.weightx = weightx; gbc.weighty = weighty;
    gbl.setConstraints( c, gbc );
    cont.add( c );
  }
  
  public static void main( String[] args )
  {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    Container c = f.getContentPane();
    GridBagLayout gbl = new GridBagLayout();
    c.setLayout( gbl );
    //                                      x  y  w  h  wx   wy
    addComponent( c, gbl, new JTextField("1"), 0, 0, 1, 1, 0.5, 1.0 );
    addComponent( c, gbl, new JTextField("2"), 2, 0, 1, 1, 0.5  , 1.0 );
    addComponent( c, gbl, new JTextField("3"), 0, 1, 1, 1, 0.5  , 1.0  );
    addComponent( c, gbl, new JTextField("4"), 1, 1, 2, 1, 0.5  , 1.0);
    addComponent( c, gbl, new JTextField("5"), 0, 3, 2, 1, 0.5  , 1.0 );
    addComponent( c, gbl, new JTextField("6"), 1, 3, 2, 1,0.5  , 1.0  );
    addComponent( c, gbl, new JTextField("7"), 0, 5, 1, 2, 0.5  , 1.0);
    addComponent( c, gbl, new JTextField("8"), 2, 5, 1, 2, 0.5  , 1.0);
    addComponent( c, gbl, new JTextField("9"), 0, 7, 1, 2, 0.5  , 1.0);
    addComponent( c, gbl, new JTextField("10"), 2, 7, 1, 2, 0.5  , 1.0);
    addComponent( c, gbl, new JTextField("11"), 0, 9, 1, 2, 0.5  , 1.0);
    addComponent( c, gbl, new JTextField("12"), 2, 9, 1, 2, 0.5  , 1.0);
    addComponent( c, gbl, new JTextField("13"), 0, 11, 1, 2, 0.5  , 1.0);
    addComponent( c, gbl, new JTextField("14"), 2, 11, 1, 2, 0.5  , 1.0);
    addComponent( c, gbl, new JTextField("15"), 0, 13, 1, 2, 0.5  , 1.0);
    addComponent( c, gbl, new JTextField("16"), 2, 13, 1, 2, 0.5  , 1.0);
    addComponent( c, gbl, new JTextField("17"), 0, 15, 1, 2, 0.5  , 1.0);
    addComponent( c, gbl, new JTextField("18"), 2, 15, 1, 2, 0.5  , 1.0);
    addComponent( c, gbl, new JTextField("19"), 0, 18, 1, 2, 0.5  , 1.0);
    addComponent( c, gbl, new JTextField("20"), 2, 18, 1, 2, 0.5  , 1.0);
    addComponent( c, gbl, new JTextField("21"), 0, 21, 1, 2, 0.5  , 1.0);
    addComponent( c, gbl, new JTextField("22"), 2, 21, 1, 2, 0.5  , 1.0);
    addComponent( c, gbl, new JButton("23"), 0, 24, 3, 1, 0.5  , 1.0);
    
    
    f.setSize( 500, 400 );
    f.setVisible( true );
  }
}

Klasse mit ShowViewer (Klasse ist nur ein Panel, da sie in eine andere Gui übernommen wird.)

Java Code: Quelltext in neuem Fenster öffnen
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
 
public class ShowViewer extends JPanel implements ActionListener{
    
    WohnungVerwalter wohnung = new WohnungVerwalter(); 
    
    String iconsDirectory = "icons/";
    
    int counter; 
 
        
      private JTextField textField1;
      private JTextField textField2;
    
    public ShowViewer(){
        super(); 
        
       
        this.setLayout(new BorderLayout());
        
        JToolBar toolBar = new JToolBar();
        addButtons(toolBar);
        addTextFelder();
        toolBar.setFloatable(false);
        toolBar.setRollover(true);
        
 
        
        this.add(toolBar, BorderLayout.NORTH);
       
        this.add(textField1, BorderLayout.CENTER);
        this.add(textField2, BorderLayout.SOUTH);
        
        
        
        
        
    }   
    
    public void addTextFelder (){
        textField1 = new JTextField();
        textField2 = new JTextField();
        
    }
    
    public void addButtons(JToolBar toolBar) {
        JButton button;
        button = createButton("links", "Zurück" );
        toolBar.add(button);
        button = createButton( "rechts", "Vorwärts" );
        toolBar.add(button);
        button = createButton("blume", "Bearbeiten");
        toolBar.add(button);
        button = createButton("delet", "Löschen");
        toolBar.add(button);
        
 
    }
 
    public JButton createButton(String actionCommand, String toolTipText) {
        String imgLocation = iconsDirectory + actionCommand + ".jpg";
        JButton button = new JButton();
        button.setActionCommand(actionCommand);
        button.setToolTipText(toolTipText);
        button.addActionListener(this);
        button.setIcon(new ImageIcon(imgLocation, actionCommand));
        
        return button;
            
    }
    
    
    
    
       public void showNext() {
            
            counter++;
            
            if (counter>=wohnung.list.size()) counter = 0;
            textField1.setText(wohnung.list.get(counter).getName());
            textField2.setText(wohnung.list.get(counter).getMieterName());
           
        
    }
        public void deleteCurrent(){
            
            String d;
            String s; 
            
            d = (wohnung.list.get(counter).getID());
            //vb.verbinden();
            
            s = "DELETE FROM `mkb`.`vermieter` WHERE `vermieter`.`ID` =" + d;
             
            
            
            
        }
      public void showLast() {
            
            counter--;
            
            if (counter<= 1) //counter = 0;
            textField1.setText(wohnung.list.get(counter).getName());
            textField2.setText(wohnung.list.get(counter).getMieterName());
            
 
        
        
    }
    
    public static void main (String[] args)
    {
        // man kann natürlich seine eigene JFrame-Klasse schreiben und diese hier
        // verwendent
        final JFrame myFrame = new JFrame("Fenstertitel");
        // Layouting des Frames: der ShowViewer ist Hauptkomponente und kommt in die Mitte
        myFrame.add( new ShowViewer(), BorderLayout.CENTER );
 
        // pack() setzt die Größe des Frames anhand des Inhaltes (daher PreferredSize von ShowViewer setzen
        myFrame.pack(); //oder setSize(800, 600);
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        // Anzeigen des Frames
        SwingUtilities.invokeLater(new Runnable()
        
        {
            public void run()
            {
                myFrame.setVisible(true);
            }
        });
    }
 
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().compareTo("rechts") == 0)
            this.showNext();
 
        
    }}

Vielen Dank für die Hilfe
maxth ist offline  
Bei Google nach dem markiertem Wort suchen Bei Wikipedia nach dem markiertem Wort suchen Im Forum nach dem markiertem Wort suchen
Alt 15.09.2009, 12:06   #2 (permalink)
Stammbenutzer
Megabyte
 
Registriert seit: 01.05.2008
Fachbeiträge: 2.009
Abgegebene Danke: 8
Erhielt 123 Danke für 122 Beiträge
ich mach das meistens so, das ich eine eigene Klasse für das Frame habe, und wiederum extra klassen für den inhalt des frames.
Das Frame hat bei immer ein BorderLayout mit Menü und Toolbar(North) und die einzige Komponente im Center ist die Klasse "MainPanel" sozusagen, welche irgendeine Gestalt haben kann, in deinem Fall ist es ein Panel mit GridBagLayout und diversen Textfeldern. So trenne ich schonmal Frame-spezifische Elemente vom eigentlichen Inhalt, der kann ja variieren und ich brauch die Toolbar oder Statusbar, sofern man eine hat nicht im Layout des Panels unterbringen, da es meiner Ansicht nach nix mit dem Panel zu tun hat (rein von der Sicht, welche Elemente eben zum Fenster gehörig sind und welche nicht). So weiss ich auch gleich wo ich schauen muss, und muss auch nicht erst grossartig suchen

Also:
Frame mit Borderlayout - Menü - Toolbar(North) - MainPanel (Center) - Statusbar (South wenn nötig)

MainPanel mit irgendnem Inhalt und Layout unabhängig von den ganzen Menüelementen.

Somit kannst du das Layout-Demo als MainPanel (extends JPanel) nutzen und bis auf die Textfelder kannst deine ShowViewer als MainFrame deklarieren (extends JFrame)
__________________
Wer aufhört, besser zu werden, hat aufgehört, gut zu sein. (Philip Rosenthal)
diggaa1984 ist offline  
Bei Google nach dem markiertem Wort suchen Bei Wikipedia nach dem markiertem Wort suchen Im Forum nach dem markiertem Wort suchen
Alt 15.09.2009, 12:39   #3 (permalink)
Benutzer
int
Themenstarter
 
Registriert seit: 02.06.2009
Fachbeiträge: 63
Abgegebene Danke: 0
Erhielt 0 Danke für 0 Beiträge
Vielen Dank für deine Antwort.

Ich habe noch eine Klasse die Main_Gui die als Frame deklariert ist. Und es funktioniert auch so wie du es mir gerade beschrieben hast. Gut zu hören das ich auf dem richtigen Weg bin = ).

Mein Problem hierbei ist lediglich das ich große Probleme habe, das Gridbaglayout in meine ShowView Klasse zubringen damit ich diese verwenden kann. PRoblem ist also: aus dem Gridbaglayout ein Panel zu machen und eine Toolbar und meine restliche elemente aus dem ShowViewer einzufügen.
maxth ist offline  
Bei Google nach dem markiertem Wort suchen Bei Wikipedia nach dem markiertem Wort suchen Im Forum nach dem markiertem Wort suchen
Alt 15.09.2009, 12:53   #4 (permalink)
Stammbenutzer
Megabyte
 
Registriert seit: 01.05.2008
Fachbeiträge: 2.009
Abgegebene Danke: 8
Erhielt 123 Danke für 122 Beiträge
Zitat: maxth
Beitrag anzeigen
PRoblem ist also: aus dem Gridbaglayout ein Panel zu machen und eine Toolbar und meine restliche elemente aus dem ShowViewer einzufügen.
hm teil 1 sollte nich so schwer sein, teil 2 sollte gar nicht sein .. die toolbar soll ja in die andere Frame-klasse rein, somit brauchst die net mit GridbagLayout platzieren.

Java Code: Quelltext in neuem Fenster öffnen
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
import java.awt.*;
import javax.swing.*;
 
public class MainPanel extends JPanel {
 
    public MainPanel() {
        super();
        setup();
    }
   
 
    private void addComponent( Container cont,
                            GridBagLayout gbl,
                            Component c,
                            int x, int y,
                            int width, int height,
                            double weightx, double weighty ) {
 
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.BOTH;
    gbc.gridx = x; gbc.gridy = y;
    gbc.gridwidth = width; gbc.gridheight = height;
    gbc.weightx = weightx; gbc.weighty = weighty;
    gbl.setConstraints( c, gbc );
    cont.add( c );
  }
  
 
  private void setup() {
    GridBagLayout gbl = new GridBagLayout();
    this.setLayout(gbl);
 
    //                                      x  y  w  h  wx   wy
    addComponent( this, gbl, new JTextField("1"), 0, 0, 1, 1, 0.5, 1.0 );
    addComponent( this, gbl, new JTextField("2"), 2, 0, 1, 1, 0.5  , 1.0 );
    addComponent( this, gbl, new JTextField("3"), 0, 1, 1, 1, 0.5  , 1.0  );
    addComponent( this, gbl, new JTextField("4"), 1, 1, 2, 1, 0.5  , 1.0);
    addComponent( this, gbl, new JTextField("5"), 0, 3, 2, 1, 0.5  , 1.0 );
    addComponent( this, gbl, new JTextField("6"), 1, 3, 2, 1,0.5  , 1.0  );
    addComponent( this, gbl, new JTextField("7"), 0, 5, 1, 2, 0.5  , 1.0);
    addComponent( this, gbl, new JTextField("8"), 2, 5, 1, 2, 0.5  , 1.0);
    addComponent( this, gbl, new JTextField("9"), 0, 7, 1, 2, 0.5  , 1.0);
    addComponent( this, gbl, new JTextField("10"), 2, 7, 1, 2, 0.5  , 1.0);
    addComponent( this, gbl, new JTextField("11"), 0, 9, 1, 2, 0.5  , 1.0);
    addComponent( this, gbl, new JTextField("12"), 2, 9, 1, 2, 0.5  , 1.0);
    addComponent( this, gbl, new JTextField("13"), 0, 11, 1, 2, 0.5  , 1.0);
    addComponent( this, gbl, new JTextField("14"), 2, 11, 1, 2, 0.5  , 1.0);
    addComponent( this, gbl, new JTextField("15"), 0, 13, 1, 2, 0.5  , 1.0);
    addComponent( this, gbl, new JTextField("16"), 2, 13, 1, 2, 0.5  , 1.0);
    addComponent( this, gbl, new JTextField("17"), 0, 15, 1, 2, 0.5  , 1.0);
    addComponent( this, gbl, new JTextField("18"), 2, 15, 1, 2, 0.5  , 1.0);
    addComponent( this, gbl, new JTextField("19"), 0, 18, 1, 2, 0.5  , 1.0);
    addComponent( this, gbl, new JTextField("20"), 2, 18, 1, 2, 0.5  , 1.0);
    addComponent( this, gbl, new JTextField("21"), 0, 21, 1, 2, 0.5  , 1.0);
    addComponent( this, gbl, new JTextField("22"), 2, 21, 1, 2, 0.5  , 1.0);
    addComponent( this, gbl, new JButton("23"), 0, 24, 3, 1, 0.5  , 1.0);
  }
}

so würde ich denken ist dein panel und nun brauchst ja nur noch die elemente wie Toolbar und menü aus der showviewer-klasse in deine mainFrame-Klasse schieben. Die showViewer würde dann sozusagen wegfallen

MainFrame wäre ca. sowas
Java Code: Quelltext in neuem Fenster öffnen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class MainFrame extends JFrame {
 
    public MainFrame() {
        super("AnyViewer");
        setup();
    }
 
    private void setup() {
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        setLayout(new BorderLayout());
 
        //methoden wuerden dann komponenten erstellen, wie du es bisher auch machst
        this.setJMenuBar(getJMenuBar());
        this.add(getToolbar(), BorderLayout.NORTH);
        this.add(new MainPanel(), BorderLayout.CENTER);
 
        pack();
    }
}
__________________
Wer aufhört, besser zu werden, hat aufgehört, gut zu sein. (Philip Rosenthal)

Geändert von diggaa1984 (15.09.2009 um 13:01 Uhr)
diggaa1984 ist offline  
Bei Google nach dem markiertem Wort suchen Bei Wikipedia nach dem markiertem Wort suchen Im Forum nach dem markiertem Wort suchen
Thema geschlossen    

Themen-Optionen Thema durchsuchen
Thema durchsuchen:

Erweiterte Suche
Ansicht

Ähnliche Themen
Thema Autor Forum Antworten Letzter Beitrag
Einfache Frage zu GridBagLayout ernst AWT, Swing, JavaFX & SWT 30 01.04.2009 20:23
Swing Gridbaglayout? steve77 Java Basics - Anfänger-Themen 1 15.12.2008 19:41
Swing Gridbaglayout? steve77 Java Basics - Anfänger-Themen 0 15.12.2008 19:13
Gridbaglayout, wie geht das? Kamikaze1464 Java Basics - Anfänger-Themen 2 26.06.2007 00:12


Lesezeichen

Forumregeln
Es ist Ihnen erlaubt, neue Themen zu verfassen.
Es ist Ihnen erlaubt, auf Beiträge zu antworten.
Es ist Ihnen nicht erlaubt, Anhänge hochzuladen.
Es ist Ihnen nicht erlaubt, Ihre Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are aus
Pingbacks are aus
Refbacks are aus


Alle Zeitangaben in WEZ +1. Es ist jetzt 00:26 Uhr.


Powered by vBulletin® Version 3.8.6 (Deutsch)
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.2
Thanks for Smilies by smilies.4-user.de