2017-04-15 5 views
-1

Dieser Code verwendet das Kartenlayout, da ich die Fensteranzeige ändern möchte, anstatt mehrere Fenster (oder Rahmen) zu haben. Deshalb möchte ich mehrere Panels, die scheinbar funktionieren.GridBag Layout, das als Flow-Layout innerhalb eines Kartenlayouts fungiert. Was mache ich falsch?

Innerhalb der Panels möchte ich jedoch das Gridbag-Layout für die Positionierung von Komponenten verwenden. Aber es funktioniert nicht! Es fungiert als Flow-Layout. Kann mir jemand helfen, diese Hürde zu überwinden? Bitte.

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

public class another 
{ 
private JPanel contentPane; 
private MyPanel panel1; 
private MyPanel2 panel2; 

private void displayGUI() 
{ 
    JFrame frame = new JFrame("Card Layout Example"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    JPanel contentPane = new JPanel(); 


    contentPane.setLayout(new CardLayout()); 
    panel1 = new MyPanel(contentPane); 
    panel2 = new MyPanel2(); 
    contentPane.add(panel1, "Panel 1"); 
    contentPane.add(panel2, "Panel 2"); 
    frame.setContentPane(contentPane); 
    frame.pack(); 
    frame.setLocationByPlatform(true); 
    frame.setVisible(true); 
} 

public static void main(String... args) 
{ 
    SwingUtilities.invokeLater(new Runnable() 
    { 
     public void run() 
     { 
      new another().displayGUI(); 
     } 
    }); 
} 
} 

class MyPanel extends JPanel { 

private JTextField How; 
private JLabel jcomp2; 
private JLabel jcomp3; 
private JButton jcomp4; 
private JPanel contentPane; 
private JPanel myPanel1; 

public MyPanel(JPanel panel) 
{ 

    contentPane = panel; 
    //construct components 
    How = new JTextField (1); 
    jcomp2 = new JLabel ("Label2"); 
    jcomp3 = new JLabel ("Label3"); 
    jcomp4 = new JButton ("openNewWindow"); 
    myPanel1 = new JPanel(); 

    //adjust size and set layout 
    setPreferredSize (new Dimension (600, 600)); 
    setLayout (new GridBagLayout()); 

    //set component bounds (only needed by Absolute Positioning) 
    /* 
    How.setBounds (245, 50, 60, 25); 
    jcomp2.setBounds (35, 30, 185, 50); 
    jcomp3.setBounds (250, 30, 60, 20); 
    jcomp4.setLocation(0, 0); 
    jcomp4.setSize(315, 25); 
    */ 




    insert(jcomp2, 0, 0, 1, 1); 
    insert(jcomp3, 0, 1, 1, 1); 
    insert(jcomp4, 1, 0, 1, 1); 

    jcomp4.addActionListener(new ActionListener() 
    { 
     public void actionPerformed(ActionEvent e) 
     { 
      CardLayout cardLayout = (CardLayout) contentPane.getLayout(); 
      cardLayout.next(contentPane); 
     } 
    }); 

    //add components 
    //add (How); 
    add (jcomp2); 
    add (jcomp3); 
    add (jcomp4);    
} 


public void insert(Component c, int gridX, int gridY, int gridW, int gridH) 
{ 

    GridBagConstraints constraint = new GridBagConstraints(); 

    constraint.gridx = gridX; 
    constraint.gridy = gridY; 
    constraint.gridwidth = gridW; 
    constraint.gridheight = gridH; 

    constraint.anchor = GridBagConstraints.LINE_START; 
    myPanel1.add(c, constraint); 

} 



} 

class MyPanel2 extends JPanel { 
private JButton jcomp1; 
private JButton jcomp2; 
private JButton jcomp3; 
private JTextField jcomp4; 

public MyPanel2() { 
    //construct components 
    jcomp1 = new JButton ("test1"); 
    jcomp2 = new JButton ("test2"); 
    jcomp3 = new JButton ("test3"); 
    jcomp4 = new JTextField (5); 

    //adjust size and set layout 
    setPreferredSize (new Dimension (395, 156)); 
    setLayout (null); 

    //set component bounds (only needed by Absolute Positioning) 
    jcomp1.setBounds (20, 45, 100, 25); 
    jcomp2.setBounds (135, 60, 100, 25); 
    jcomp3.setBounds (260, 35, 100, 25); 
    jcomp4.setBounds (105, 115, 100, 25); 

    //add components 
    add (jcomp1); 
    add (jcomp2); 
    add (jcomp3); 
    add (jcomp4);  
} 
} 
+0

Welche Gesamt GUI Aussehen versuchen Sie zu erreichen? –

+0

Ich möchte idealerweise einen Inhaltsbereich erstellen, der die Anzeige abhängig davon ändert, welche Taste gedrückt wird. Auf jedem Fenster bleibt eine Überschrift gleich. – notAllThere15

+0

Dann verwendet die Haupt-GUI BorderLayout, der Titel wird in der Position "BorderLayout.PAGE_START" gehalten und der JPanel, der CardLayout verwendet, wird in der Position "BorderLayout.CENTER" gehalten. Aber das beantwortet nicht, was Ihr GridBagLayout JPanel versucht zu erreichen. –

Antwort

1

Sie scheinen die Dinge unnötig zu komplizieren. Sie erstellen ein JPanel contentPane genannt und geben ihm ein Cardlayout, alles schön und gut, aber dann aus irgendeinem Grund passieren, dass contentPane JPanel in den MyPanel Konstruktor

JPanel contentPane = new JPanel(); 
contentPane.setLayout(new CardLayout()); 
panel1 = new MyPanel(contentPane); 
panel2 = new MyPanel2(); 

und dann innerhalb der Konstruktor Sie Komponenten zuweisen zu demselben contentPane JPanel - aber warum? Sie weisen GridBagLayout zum MyPanel this Instanz, aber dann Komponenten innerhalb es in einem GridBag Weg zum myPanel1 Variable hinzufügen, wenn diese JPanel die JPanel Standardflow hat:

class MyPanel extends JPanel { 

    // .... 

    private JPanel contentPane; 
    private JPanel myPanel1; 

    public MyPanel(JPanel panel) { 
     contentPane = panel; 

     // .... 

     myPanel1 = new JPanel(); 

     setLayout (new GridBagLayout()); // you set *** this *** to GridBagLayout 

     insert(jcomp2, 0, 0, 1, 1); 
     insert(jcomp3, 0, 1, 1, 1); 
     insert(jcomp4, 1, 0, 1, 1); 

     add (jcomp2); // and then you add components to this without GridBagConstraints ? 
     add (jcomp3); 
     add (jcomp4);    
    } 


    public void insert(Component c, int gridX, int gridY, int gridW, int gridH) { 

     // .... 

     // but then add components in a GridBagLayout way into the myPanel1 JPanel??? 
     myPanel1.add(c, constraint);  
    } 
} 

besser einstellen MyPanel GridBagLayout verwenden:

Vielmehr
// adjust size and set layout 
    setPreferredSize(new Dimension(600, 600)); 
    // setLayout(new GridBagLayout()); 
    myPanel1.setLayout(new GridBagLayout()); 

als this

Wieder scheint es, dass Sie zu verkomplizieren, was sollte ein einfaches Unterfangen sein.

Zum Beispiel:

import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Insets; 
import java.awt.event.ActionEvent; 

import javax.swing.*; 

public class SimpleCardExample extends JPanel { 
    private static final int EB_GAP = 8; 
    private CardLayout cardLayout = new CardLayout(); 

    public SimpleCardExample() { 
     setBorder(BorderFactory.createEmptyBorder(EB_GAP, EB_GAP, EB_GAP, EB_GAP)); 
     setLayout(cardLayout); 
     add(new GridBagPanel(this), GridBagPanel.class.getSimpleName()); 
     add(new NextPanel(this), NextPanel.class.getSimpleName()); 
    } 

    public void nextCard() { 
     cardLayout.next(this); 
    } 

    private static void createAndShowGui() { 
     JFrame frame = new JFrame("SimpleCardExample"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(new SimpleCardExample()); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(() -> createAndShowGui()); 
    } 
} 

class GridBagPanel extends JPanel { 
    private static final int GAP = 6; 
    private static final Insets GBC_INSETS = new Insets(GAP, GAP, GAP, GAP); 

    public GridBagPanel(SimpleCardExample scExample) { 
     setLayout(new GridBagLayout()); 
     add(new JLabel("Title Goes Here", SwingConstants.CENTER), createGbc(0, 0, 2, 1)); 
     add(new JLabel("Name:"), createGbc(0, 1, 1, 1)); 
     add(new JTextField(15), createGbc(1, 1, 1, 1)); 
     add(new JLabel("Phone:"), createGbc(0, 2, 1, 1)); 
     add(new JTextField(15), createGbc(1, 2, 1, 1)); 
     add(new JLabel("Address:"), createGbc(0, 3, 1, 1)); 
     add(new JTextField(15), createGbc(1, 3, 1, 1)); 
     add(new JButton(new NextAction("Next", scExample)), createGbc(0, 4, 2, 1)); 
    } 

    private GridBagConstraints createGbc(int x, int y, int w, int h) { 
     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.gridx = x; 
     gbc.gridy = y; 
     gbc.gridwidth = w; 
     gbc.gridheight = h; 
     gbc.insets = GBC_INSETS; 
     gbc.fill = GridBagConstraints.HORIZONTAL; 
     gbc.anchor = x == 0 ? GridBagConstraints.WEST : GridBagConstraints.EAST; 
     gbc.weightx = 1.0; 
     gbc.weighty = 1.0; 
     return gbc; 
    } 
} 

class NextPanel extends JPanel { 
    public NextPanel(SimpleCardExample scExample) { 
     add(new JButton(new NextAction("Next", scExample))); 
    } 
} 

class NextAction extends AbstractAction { 
    private SimpleCardExample scExample; 

    public NextAction(String name, SimpleCardExample scExample) { 
     super(name); 
     this.scExample = scExample; 
     int mnemonic = (int) name.charAt(0); 
     putValue(MNEMONIC_KEY, mnemonic); 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     scExample.nextCard(); 
    } 
}