gui布局

/**
* JPanel 的作用是可以定义layout
* @author Administrator
*
*/
public class Gui extends JFrame implements ActionListener{

@Override
public void actionPerformed(ActionEvent arg0) {


}

public static void main(String[] a){
Gui gui = new Gui();
gui.setVisible(true);
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

private final String[] grid= {"网0","格1","布2","局3","管理4","器5"};

private JButton gridButton[] = new JButton[grid.length];

private final String[] flow = {"流","式","布","局","管","理","器"};

private JButton flowButton[] = new JButton[flow.length];

private final String[] border = {"边界","布局","管","理","器"};
private JButton borderButton[] = new JButton[border.length];

JButton bt = new JButton("卡片切换");
JButton bt1 = new JButton("网格包布局");

public Gui(){
super();
init();
setBackground(Color.YELLOW);
setTitle("布局管理器综合实例");
setLocation(500,300);
//setResizable(false);
pack(); //使得计算器中各组件大小合适
}

private void init(){
JPanel gridPanel = new JPanel();//网格布局面板

gridPanel.setLayout(new GridLayout(2,3,3,3));

for(int i=0;i gridButton[i] = new JButton(grid[i]);
gridPanel.add(gridButton[i]);
gridButton[i].setForeground(Color.BLUE);
}

JPanel flowPanel = new JPanel();
flowPanel.setLayout(new FlowLayout());
for(int i=0;i flowButton[i] = new JButton(flow[i]);
flowPanel.add(flowButton[i]);
flowButton[i].setForeground(Color.RED);
}

JPanel borderPanel = new JPanel();
borderPanel.setLayout(new BorderLayout());
for(int i=0; i borderButton[i] = new JButton(border[i]);
borderPanel.setForeground(Color.CYAN);
}

borderPanel.add("North",borderButton[0]);
borderPanel.add("East",borderButton[1]);
borderPanel.add("South",borderButton[2]);
borderPanel.add("West",borderButton[3]);
borderPanel.add("Center",borderButton[4]);

//==================cardPanel================================================
JPanel cardPanel = new JPanel();
cardPanel.setLayout(new BorderLayout());


/*JButton bt = new JButton("卡片切换");
cardPanel.add("North",bt);*/

JPanel cp = new JPanel(); //这个panel会把button包起来
bt.addActionListener(this);
cp.add(bt);

JPanel pl = new JPanel();
pl.add(new JButton("button 1"));
pl.add(new JButton("button 2"));
pl.add(new JButton("button 3"));

JPanel p2 = new JPanel();
p2.add(new TextField("TextField",20));

JPanel cards = new JPanel();
CardLayout layout = new CardLayout();
cards.setLayout(layout);

cards.add("Panel with buttons ",pl);
cards.add("Panel with TextField",p2);
cardPanel.add("Center",cards);

JPanel gridBagPanel = new JPanel(); //网格包
gridBagPanel.setLayout(new GridBagLayout());
GridBagConstraints c= new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
gridBagPanel.add(new JButton("网1"),c);
gridBagPanel.add(new JButton("格2"),c);
gridBagPanel.add(new JButton("包3"),c);
c.gridwidth = GridBagConstraints.REMAINDER;
gridBagPanel.add(new JButton("布4"),c);
c.weightx = 0.0;
gridBagPanel.add(new JButton("局5"),c);
c.gridwidth = GridBagConstraints.RELATIVE;
gridBagPanel.add(new JButton("管6"),c);
c.gridwidth = GridBagConstraints.RELATIVE;
gridBagPanel.add(new JButton("理7"),c);
c.gridheight = 2;
c.gridwidth=1;
c.weighty=1.0;
gridBagPanel.add(new JButton("器8"),c);
c.weighty=0.0;
c.gridwidth = GridBagConstraints.REMAINDER;
c.gridheight=1;
gridBagPanel.add(new JButton("器9"),c);
gridBagPanel.add(new JButton("器0"),c);

getContentPane().setLayout(new BorderLayout());
getContentPane().add("North",flowPanel);
getContentPane().add("West",gridBagPanel);
getContentPane().add("Center",borderPanel);
getContentPane().add("East",cardPanel);
getContentPane().add("South",gridPanel);

}
}

你可能感兴趣的:(java_gui,C,C++,C#)