Auf Thema antworten

Soooooo,

Ich hab mich heute Mittag mal 2 Stunden hingesetzt und hab mit den Tipps von Suinos und der Hilfe von google etwas zusammen gewürfelt.

Das was ich mir da zusammen gewürfelt hab sieht irgendwie kompliziert und viel aus. Also ich bin mir bei der Sache nicht 100% sicher.


Ich würde mich sehr über Vorschläge und Verbesserungen freuen. :)


[JAVA=1]

import java.awt.*;

import javax.swing.*;



public class Test {

    final static boolean shouldFill = true;

    final static boolean shouldWeightX = true;

    final static boolean RIGHT_TO_LEFT = false;

   

   

    public static void addComponentsToPane(Container pane) {

        if (RIGHT_TO_LEFT) {

            pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

        }


        JLabel label;

        JTextField textfield;

        JButton button;

        pane.setLayout(new GridBagLayout());

        GridBagConstraints c = new GridBagConstraints();

        if (shouldFill) {

        c.fill = GridBagConstraints.HORIZONTAL;

        }


        label = new JLabel("Bild");

        if (shouldWeightX) {

        c.weightx = 0.5;

        }

        c.fill = GridBagConstraints.HORIZONTAL;

        c.ipady = 40;

        c.gridx = 2;

        c.gridy = 0;

        pane.add(label, c);

       

        label = new JLabel("");

        c.fill = GridBagConstraints.HORIZONTAL;

        c.weightx = 0.5;

        c.gridx = 0;

        c.gridy = 1;

        pane.add(label, c);

       

        label = new JLabel("Anzahl");

        c.fill = GridBagConstraints.CENTER;

        c.weightx = 0.5;

        c.gridx = 1;

        c.gridy = 1;

        pane.add(label, c);

       

        label = new JLabel("Produkt");

        c.fill = GridBagConstraints.CENTER;

        c.weightx = 0.5;

        c.gridx = 2;

        c.gridy = 1;

        pane.add(label, c);

       

        label = new JLabel("");

        c.fill = GridBagConstraints.HORIZONTAL;

        c.weightx = 0.5;

        c.gridx = 3;

        c.gridy = 1;

        pane.add(label, c);

       

        textfield = new JTextField("");

        c.fill = GridBagConstraints.HORIZONTAL;

        c.ipady = 10;     

        c.weightx = 0.5;

        c.gridwidth = 1;

        c.gridx = 1;

        c.gridy = 2;

        pane.add(textfield, c);

       

        textfield = new JTextField("");

        c.fill = GridBagConstraints.HORIZONTAL;

        c.ipady = 10;    

        c.weightx = 0.5;

        c.gridwidth = 1;

        c.gridx = 2;

        c.gridy = 2;

        pane.add(textfield, c);

       

        textfield = new JTextField("");

        c.fill = GridBagConstraints.HORIZONTAL;

        c.ipady = 10;     

        c.weightx = 0.5;

        c.gridwidth = 1;

        c.gridx = 1;

        c.gridy = 3;

        pane.add(textfield, c);

       

        textfield = new JTextField("");

        c.fill = GridBagConstraints.HORIZONTAL;

        c.ipady = 10;     

        c.weightx = 0.5;

        c.gridwidth = 1;

        c.gridx = 2;

        c.gridy = 3;

        pane.add(textfield, c);

       

        textfield = new JTextField("");

        c.fill = GridBagConstraints.HORIZONTAL;

        c.ipady = 10;     

        c.weightx = 0.5;

        c.gridwidth = 1;

        c.gridx = 1;

        c.gridy = 4;

        pane.add(textfield, c);

       

        textfield = new JTextField("");

        c.fill = GridBagConstraints.HORIZONTAL;

        c.ipady = 10;     

        c.weightx = 0.5;

        c.gridwidth = 1;

        c.gridx = 2;

        c.gridy = 4;

        pane.add(textfield, c);

       

        textfield = new JTextField("");

        c.fill = GridBagConstraints.HORIZONTAL;

        c.ipady = 10;     

        c.weightx = 0.5;

        c.gridwidth = 1;

        c.gridx = 1;

        c.gridy = 5;

        pane.add(textfield, c);

       

        textfield = new JTextField("");

        c.fill = GridBagConstraints.HORIZONTAL;

        c.ipady = 10;     

        c.weightx = 0.5;

        c.gridwidth = 1;

        c.gridx = 2;

        c.gridy = 5;

        pane.add(textfield, c);


        textfield = new JTextField("");

        c.fill = GridBagConstraints.HORIZONTAL;

        c.ipady = 10;     

        c.weightx = 0.5;

        c.gridwidth = 1;

        c.gridx = 1;

        c.gridy = 6;

        pane.add(textfield, c);

       

        textfield = new JTextField("");

        c.fill = GridBagConstraints.HORIZONTAL;

        c.ipady = 10;     

        c.weightx = 0.5;

        c.gridwidth = 1;

        c.gridx = 2;

        c.gridy = 6;

        pane.add(textfield, c);

       

        button = new JButton("Kaufen");

        c.fill = GridBagConstraints.HORIZONTAL;

        c.ipady = 10;     

        c.gridx = 2;      

        c.gridwidth = 1; 

        c.gridy = 7;      

        pane.add(button, c);

    }

   

    private static void createAndShowGUI() {

        JFrame frame = new JFrame("Tabelle");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        addComponentsToPane(frame.getContentPane());

        frame.pack();

        frame.setVisible(true);

    }


    public static void main(String[] args) {

        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            public void run() {

                createAndShowGUI();

            }

        });

    }

}

[/code]


und so sieht's aus:



Meine Frage:

  • Kann Ich das JTextField auf eine bestimmte größe festlegen?
  • Wie bekomm ich einen kleinen Abstand zwischen dem letzten JTextField und dem JButton?


Gruß Erdnuss

(Wie man sieht Java Anfänger, deswegen bitte Verständliche Antworten die auch ich verstehe :D)



Oben