如何将GridBagLayout和CardLayout结合使用?
我试图将这些按钮添加到面板中,但是它破坏了GridBagLayout,下面是文档中的内容;
button = new JButton("Button 1");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
pane.add(button, c);
button = new JButton("Button 2");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 0;
pane.add(button, c);我想做的是:
JPanel m = new JPanel();
button = new JButton("Button 1");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
m.add(button, c);
button = new JButton("Button 2");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 0;
m.add(button, c);
windows = new JPanel(new CardLayout());
windows.add(m, "test");
pane.add(windows, c);但是,它会恢复,就好像它在使用流布局。
我错过了什么?任何帮助都是非常感谢的。
完整
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public final class GUI extends JPanel implements ActionListener {
// buttons
private JButton newButton, findButton;
JPanel panels;
public void addComponentToPane (Container pane) {
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
JPanel mainMenu = new JPanel();
// add
mainMenu.add(newButton = new JButton("Add"), c);
newButton.setActionCommand("new");
newButton.addActionListener(this);
// find
mainMenu.add(findButton = new JButton("Find"), c);
findButton.setActionCommand("find");
findButton.addActionListener(this);
panels = new JPanel(new CardLayout());
panels.add(mainMenu, "mainWindow");
pane.add(panels, c);
}
public void actionPerformed(ActionEvent e) {
CardLayout allpanels = (CardLayout)(panels.getLayout());
switch (e.getActionCommand()) {
case "new":
allpanels.show(panels, "add");
break;
case "find":
System.out.println("find");
break;
}
}
private static void createGUI () {
JFrame frame = new JFrame("Check");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GUI contentPane = new GUI();
contentPane.addComponentToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
createGUI();
}
}发布于 2014-01-19 07:56:54
您的代码非常混乱,但主要问题是:
我已经重构了您的代码,并修复了上面提到的这些问题。如果你能提供更多关于你想要达到的目标的细节,我们也许能给出一个更好的答案。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public final class GUI extends JPanel implements ActionListener {
// buttons
private JButton newButton, findButton;
JPanel panels;
public void addComponentToPane(Container pane) {
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
createPanels();
pane.add(panels, c);
}
private void createPanels() {
JPanel mainMenu = createMainMenu();
panels = new JPanel(new CardLayout());
panels.add(mainMenu, "mainWindow");
panels.add(new JLabel("Add card"), "add");
panels.add(new JLabel("Find card"), "find");
}
private JPanel createMainMenu() {
GridBagConstraints c = new GridBagConstraints();
c.gridx = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.HORIZONTAL;
JPanel mainMenu = new JPanel(new GridBagLayout());
// add
mainMenu.add(newButton = new JButton("Add"), c);
newButton.setActionCommand("new");
newButton.addActionListener(this);
// find
mainMenu.add(findButton = new JButton("Find"), c);
findButton.setActionCommand("find");
findButton.addActionListener(this);
return mainMenu;
}
public void actionPerformed(ActionEvent e) {
CardLayout allpanels = (CardLayout) (panels.getLayout());
switch (e.getActionCommand()) {
case "new":
allpanels.show(panels, "add");
break;
case "find":
allpanels.show(panels, "find");
break;
}
}
private static void createGUI() {
JFrame frame = new JFrame("Check");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GUI contentPane = new GUI();
contentPane.addComponentToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
createGUI();
}
}https://stackoverflow.com/questions/21201103
复制相似问题