2017-04-15 26 views
-1

此代码使用卡布局,因为我想更改窗口显示而不是具有多个窗口(或框架)。因此,我想要多个面板,这似乎工作。GridBag布局充当流布局,在卡布局中。我究竟做错了什么?

但是在面板中,我想使用网格布局定位组件。但它不工作!它充当流程布局。任何人都可以帮助我克服这个障碍吗?请。

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

是你想达到什么样的整体图形界面的美观? –

+0

我最好想创建一个内容部分,它根据按下的按钮来改变显示。在每个窗口上,标题保持不变。 – notAllThere15

+0

然后,主GUI使用BorderLayout,标题保存在BorderLayout.PAGE_START位置,使用CardLayout的JPanel保存在BorderLayout.CENTER位置。但是这并没有回答你的GridBagLayout JPanel试图实现的功能。 –

回答

1

你看起来不必要的过度复杂的事情。您创建一个JPanel所谓的contentPane并给它一个CardLayout,一切都很好,但后来由于某种原因,你传递的contentPane的JPanel进入MyPanel构造,

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

,然后在构造函数中分配组件来此同contentPane JPanel - 但为什么?您指定的GridBagLayout到MyPanel this实例,但随后的网格包的方式在其中添加组件到myPanel1变量时,此JPanel有JPanel的默认的FlowLayout:

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);  
    } 
} 

最好设置myPanel使用GridBagLayout的:

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

不是this

再次看来你是过于复杂了什么应该是一个简单的努力。

例如:

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(); 
    } 
}