2017-12-06 11 views
2

Dies ist das beabsichtigte Design meiner JFrame:(GridBag Layout) Grafik-Komponente in ein anderes Raster Clipping, nicht sicher, warum

enter image description here

Es ist leicht genug, sieht zu implementieren, hat einige Knöpfe, Textfelder, Lables und eine Auswahlliste.

Es hat auch das gelbe Feld in der oberen linken Ecke eine "Grafik" genannt. Ich weiß nicht, wie man es benutzt.

Um etwas Ähnliches wie oben zu machen, wollte ich ein Gridbag Layout verwenden.

Dies ist, was ich in der Lage war, bevor sie in Probleme laufen zu produzieren:

enter image description here

Das Label red:, die Textbox und die -/+ Tasten alle auf 1 col sind (gridy = 1;).

Als ich meine Grafik in Zeile 0 Spalte 0 (mit gridwidth = 4) hinzugefügt habe, erwartete ich, dass es über diese Elemente gehen wird (weil 0 kleiner als 1 ist). Leider ist das nicht das, was ich bekommen habe, und es ist frustrierend, weil ich nicht weiß, warum es so ist.

Mein Ergebnis:

enter image description here

Wie ich bereits sagte, ich habe nicht so viel Erfahrung mit JFrames. Ich würde wirklich jemand Erfahrung und Hilfe bei der Reparatur dieses und eine Erklärung warum zu schätzen wissen.

Hier ist meine Quellcode:

(1/2):

package colorSamplerApp; 

import java.awt.*; 
import javax.swing.*; 



public class WindowApplication extends JFrame { 
    final static boolean shouldFill = true; 
    final static boolean shouldWeightX = true; 
    final static boolean RIGHT_TO_LEFT = false; 

    protected static JLabel redLabel; 
    protected static JLabel greenLabel; 
    protected static JLabel blueLabel; 

    protected static JTextField redTextField; 
    protected static JTextField greenTextField; 
    protected static JTextField blueTextField; 

    protected static JButton redButtonM; 
    protected static JButton greenButtonM; 
    protected static JButton blueButtonM; 

    protected static JButton redButtonP; 
    protected static JButton greenButtonP; 
    protected static JButton blueButtonP; 

    protected static JButton saveButton; 
    protected static JButton resetButton; 

    protected static JList listColors; 

    protected static DrawingTester drawTest; 



    public static void addComponentsToPane(Container pane) 
    { 
     if (RIGHT_TO_LEFT) { 
      pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); 
     } 


     pane.setLayout(new GridBagLayout()); 
     GridBagConstraints c = new GridBagConstraints(); 
     if (shouldFill) { 
     //natural height, maximum width 
     c.fill = GridBagConstraints.HORIZONTAL; 
     } 


     /** 
     * Setup the RGB Labels 
     * 
     * R <0,1> 
     * G <0,2> 
     * B <0,3> 
     * 
     */ 

     c.insets = new Insets(0,10,0,5); 

     c.gridwidth = 1; 
     redLabel = new JLabel("Red:"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 0; 
     c.gridy = 1; 
     pane.add(redLabel, c); 

     greenLabel = new JLabel("Green:"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 0; 
     c.gridy = 2; 
     pane.add(greenLabel, c); 

     blueLabel = new JLabel("Blue:"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 0; 
     c.gridy = 3; 
     pane.add(blueLabel, c); 

     /** 
     * Setup the RGB Text Fields 
     * 
     * R<1,1> 
     * G<1,2> 
     * B<1,3> 
     * 
     */ 


     c.insets.set(0, 0, 0, 5); 

     redTextField = new JTextField("255"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 1; 
     c.gridy = 1; 
     pane.add(redTextField, c); 

     greenTextField = new JTextField("0"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 1; 
     c.gridy = 2; 
     pane.add(greenTextField, c); 

     blueTextField = new JTextField("0"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 1; 
     c.gridy = 3; 
     pane.add(blueTextField, c); 

     /** 
     * Setup the RGB (-) Button Fields 
     * 
     * R<2,1> 
     * G<2,2> 
     * B<2,3> 
     * 
     */ 

     c.insets.set(5,5,0,10); 

     redButtonM = new JButton("-"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 2; 
     c.gridy = 1; 
     pane.add(redButtonM, c); 

     greenButtonM = new JButton("-"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 2; 
     c.gridy = 2; 
     pane.add(greenButtonM, c); 

     blueButtonM = new JButton("-"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 2; 
     c.gridy = 3; 
     pane.add(blueButtonM, c); 

     /** 
     * Setup the RGB (+) Button Fields 
     * 
     * R<3,1> 
     * G<3,2> 
     * B<3,3> 
     * 
     */ 

     c.insets.set(5,0,0,10); 

     redButtonP = new JButton("+"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 3; 
     c.gridy = 1; 
     pane.add(redButtonP, c); 

     greenButtonP = new JButton("+"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 3; 
     c.gridy = 2; 
     pane.add(greenButtonP, c); 

     blueButtonP = new JButton("+"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 3; 
     c.gridy = 3; 
     pane.add(blueButtonP, c); 

     /** 
     * Setup the Save/Reset Buttons 
     * 
     * save <0,4> 
     * reset<1,4> 
     * 
     */ 

     c.insets.set(10,10,10,5); 

     saveButton = new JButton("Save"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.ipady = 10;  //make this component tall 
     c.ipadx = 10; 
     //c.weightx = 0.0; 
     c.gridwidth = 2; 
     c.gridx = 0; 
     c.gridy = 4; 
     pane.add(saveButton, c); 


     c.insets.set(10,5,10,10); 

     resetButton = new JButton("Reset"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.ipady = 10;  
     c.ipadx = 10; 
     //c.weighty = 1.0; //request any extra vertical space 
     //c.anchor = GridBagConstraints.PAGE_END; //bottom of space 
     c.gridx = 2;  //aligned with button 2 
     c.gridwidth = 2; //2 columns wide 
     c.gridy = 4;  //third row 
     pane.add(resetButton, c); 

     /** 
     * Setup the Color Selection List 
     * 
     * <4,0> 
     * 
     */ 

     c.insets.set(10,0,10,10); 

     listColors = new JList(); 
     /** 
      File reading shenanigans to fill up the list 
     */ 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.ipady = 150;  
     c.ipadx = 100; 
     //c.weighty = 1.0; //request any extra vertical space 
     //c.anchor = GridBagConstraints.PAGE_END; //bottom of space 
     c.gridx = 4;  //aligned with button 2 
     c.gridheight = 5; //2 columns wide 
     c.gridy = 1;  //third row 
     pane.add(listColors, c); 

     /** 
     * Setup the Graphics 
     * 
     * <0,0> 
     * 
     */ 

     drawTest = new DrawingTester(); 
     c.insets.set(0,0,0,0); 
     c.gridx = 0;  //top leftw 
     c.gridy = 0;  //bottom 
     c.gridwidth = 0 ; 
     pane.add(drawTest,c); 
    } 


    public static void main (String argv[]) 
    { 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGUI("Color Sampler"); 
      } 
     }); 
    } 

    public static void createAndShowGUI(String title) 
    { 
     // Create and setup the window 
     JFrame frame = new JFrame (title); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     // Setup the content pane 
     addComponentsToPane(frame.getContentPane()); 

     // Display the window 
     frame.pack(); 
     frame.setVisible(true); 

    } 


} 

(2/2)

package colorSamplerApp; 

import java.awt.*; 
import javax.swing.*; 

import javax.swing.JComponent; 

public class DrawingTester extends JComponent { 

    public void paintComponent(Graphics g){ 
     super.paintComponent(g); 
     g.setColor(Color.red); 
     g.fillRect(0,0, 170, 70); 
    } 
} 
+0

Anstatt ein Feld für jede JComponent zu erstellen, würde ich sehr empfehlen, stattdessen Arrays zu verwenden und auch das Entwurfsmuster "Factory Pattern" bezüglich der Erstellung solcher Komponenten zu betrachten. Immer wenn Sie den gleichen Code 3-5 mal hintereinander tippen, überdenken Sie, was Sie tun. Manchmal kann man es nicht vermeiden, aber in 99% der Fälle könnte man es besser machen. –

Antwort

3

Das Problem, das Sie mit Blick auf bezieht sich die Wiederverwendung gleich GridBagConstraints Objekt. Ziemlich häufig und ist einer der Schmerzen im Umgang mit diesem Layout.

Zunächst habe ich alle Anrufe, die die insets geändert haben, auskommentiert.

Dann habe ich sichergestellt, dass die gridx und richtig eingestellt wurden. Denken Sie auch an gridheight und gridwidth als die Anzahl der Zellen im Raster, die Ihre Komponente nehmen wird, denken Sie nicht in Bezug auf die tatsächlichen width und height der internen Komponente, die Sie in der Zelle platzieren.

In DrawingTester habe ich auch dafür gesorgt, dass wir die ganze Sache gemalt haben.

Überprüfen Sie den Code und das Ergebnis aus:

import java.awt.*; 
import javax.swing.*; 

public class WindowApplication extends JFrame { 
    final static boolean shouldFill = true; 
    final static boolean shouldWeightX = true; 
    final static boolean RIGHT_TO_LEFT = false; 

    protected static JLabel redLabel; 
    protected static JLabel greenLabel; 
    protected static JLabel blueLabel; 

    protected static JTextField redTextField; 
    protected static JTextField greenTextField; 
    protected static JTextField blueTextField; 

    protected static JButton redButtonM; 
    protected static JButton greenButtonM; 
    protected static JButton blueButtonM; 

    protected static JButton redButtonP; 
    protected static JButton greenButtonP; 
    protected static JButton blueButtonP; 

    protected static JButton saveButton; 
    protected static JButton resetButton; 

    protected static JList listColors; 

    protected static DrawingTester drawTest; 



    public static void addComponentsToPane(Container pane) 
    { 
     if (RIGHT_TO_LEFT) { 
      pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); 
     } 


     pane.setLayout(new GridBagLayout()); 
     GridBagConstraints c = new GridBagConstraints(); 
     if (shouldFill) { 
     //natural height, maximum width 
     c.fill = GridBagConstraints.HORIZONTAL; 
     } 


     /** 
     * Setup the RGB Labels 
     * 
     * R <0,1> 
     * G <0,2> 
     * B <0,3> 
     * 
     */ 

//  c.insets = new Insets(0,10,0,5); 

     c.gridwidth = 1; 
     redLabel = new JLabel("Red:"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 0; 
     c.gridy = 1; 
     pane.add(redLabel, c); 

     greenLabel = new JLabel("Green:"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 0; 
     c.gridy = 2; 
     pane.add(greenLabel, c); 

     blueLabel = new JLabel("Blue:"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 0; 
     c.gridy = 3; 
     pane.add(blueLabel, c); 

     /** 
     * Setup the RGB Text Fields 
     * 
     * R<1,1> 
     * G<1,2> 
     * B<1,3> 
     * 
     */ 


//  c.insets.set(0, 0, 0, 5); 

     redTextField = new JTextField("255"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 1; 
     c.gridy = 1; 
     pane.add(redTextField, c); 

     greenTextField = new JTextField("0"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 1; 
     c.gridy = 2; 
     pane.add(greenTextField, c); 

     blueTextField = new JTextField("0"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 1; 
     c.gridy = 3; 
     pane.add(blueTextField, c); 

     /** 
     * Setup the RGB (-) Button Fields 
     * 
     * R<2,1> 
     * G<2,2> 
     * B<2,3> 
     * 
     */ 

//  c.insets.set(5,5,0,10); 

     redButtonM = new JButton("-"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 2; 
     c.gridy = 1; 
     pane.add(redButtonM, c); 

     greenButtonM = new JButton("-"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 2; 
     c.gridy = 2; 
     pane.add(greenButtonM, c); 

     blueButtonM = new JButton("-"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 2; 
     c.gridy = 3; 
     pane.add(blueButtonM, c); 

     /** 
     * Setup the RGB (+) Button Fields 
     * 
     * R<3,1> 
     * G<3,2> 
     * B<3,3> 
     * 
     */ 

//  c.insets.set(5,0,0,10); 

     redButtonP = new JButton("+"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 3; 
     c.gridy = 1; 
     pane.add(redButtonP, c); 

     greenButtonP = new JButton("+"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 3; 
     c.gridy = 2; 
     pane.add(greenButtonP, c); 

     blueButtonP = new JButton("+"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 3; 
     c.gridy = 3; 
     pane.add(blueButtonP, c); 

     /** 
     * Setup the Save/Reset Buttons 
     * 
     * save <0,4> 
     * reset<1,4> 
     * 
     */ 

     saveButton = new JButton("Save"); 
     c.insets.set(10,10,10,5); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.ipady = 10;  //make this component tall 
     c.ipadx = 10; 
     //c.weightx = 0.0; 
     c.gridwidth = 2; 
     c.gridx = 0; 
     c.gridy = 4; 
     pane.add(saveButton, c); 


     resetButton = new JButton("Reset"); 
     c.insets.set(10,5,10,10); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.ipady = 10;  
     c.ipadx = 10; 
     //c.weighty = 1.0; //request any extra vertical space 
     //c.anchor = GridBagConstraints.PAGE_END; //bottom of space 
     c.gridx = 2;  //aligned with button 2 
     c.gridwidth = 2; //2 columns wide 
     c.gridy = 4;  //third row 
     pane.add(resetButton, c); 

     /** 
     * Setup the Color Selection List 
     * 
     * <4,0> 
     * 
     */ 

     c.insets.set(5,5,5,5); 

     listColors = new JList(new Object[] {"Test 1", "Test 2", "Test 3"}); 
     /** 
      File reading shenanigans to fill up the list 
     */ 
     c.fill = GridBagConstraints.BOTH; 
     c.gridy = 0;  
     c.gridx = 4;  
     c.gridheight = 5; 
     c.gridwidth = 1; 

     pane.add(listColors, c); 

     /** 
     * Setup the Graphics 
     * 
     * <0,0> 
     * 
     */ 

     drawTest = new DrawingTester(); 
     c.insets = new Insets(5,5,5,5); 
     c.fill = GridBagConstraints.BOTH; 
     c.gridx = 0;  //top leftw 
     c.gridy = 0;  //bottom 
     c.gridheight = 1; 
     c.gridwidth = 4; 
     pane.add(drawTest,c); 
    } 


    public static void main (String argv[]) 
    { 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGUI("Color Sampler"); 
      } 
     }); 
    } 

    public static void createAndShowGUI(String title) 
    { 
     // Create and setup the window 
     JFrame frame = new JFrame (title); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     // Setup the content pane 
     addComponentsToPane(frame.getContentPane()); 

     // Display the window 
     frame.pack(); 
     frame.setVisible(true); 

    } 


} 

class DrawingTester extends JComponent { 

    public void paintComponent(Graphics g){ 
     super.paintComponent(g); 
     g.setColor(Color.red); 
     g.fillRect(0,0, g.getClipBounds().width, g.getClipBounds().height); 
    } 
} 

Schauen Sie sich das Ergebnis:

enter image description here

Von diesem Punkt an Sie es verändern fortsetzen können sollten an Ihre Bedürfnisse anzupassen, wie alle insets zurück zu bekommen und die height und width der Komponenten zu ändern. Denken Sie daran, dass Sie die GridBagConstraints wiederverwenden, also müssen Sie vorsichtig sein. Wenn Sie sich nicht sicher sind, instanziieren Sie einfach einen neuen und testen Sie Ihren Code.