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
Klasse mit ShowViewer (Klasse ist nur ein Panel, da sie in eine andere Gui übernommen wird.)
Vielen Dank für die Hilfe
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:
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:
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