Java复杂GUI布局举例与源码



【举例要求】要求完成下图中要求的布局,形式要求一致,并且要求美观。这里使用的布局较多,比较复杂,编写好的源码与大家分享一下。


Java复杂GUI布局举例与源码


package ComPlexPanel;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;

public class ComPlexPanel {
 JFrame frame = new JFrame("ComPlexPanelDemo Made By HY49");
 Container content = frame.getContentPane();
 public ComPlexPanel(){
  JPanel GrandParentPanel = new JPanel();
  
//  JPanel FatherPanel = new JPanel();
  JPanel MotherPanel = new JPanel(new GridLayout(4,1));
  
  JPanel BrotherPanel01 = new JPanel();
  JPanel BrotherPanel02 = new JPanel(new GridLayout(1,5));
  JPanel BrotherPanel03 = new JPanel();
  
  JPanel SonPanel01 = new JPanel();
  JTextArea area01 = new JTextArea(6,5);
  SonPanel01.add(area01);
  JPanel SonPanel02 = new JPanel(new GridLayout(3,1));
  JPanel SonPanel03 = new JPanel();
  JTextArea area02 = new JTextArea(6,5);
  SonPanel03.add(area02);
  JPanel SonPanel04 = new JPanel(new GridLayout(3,1));
  JPanel SonPanel05 = new JPanel();
  JTextArea area03 = new JTextArea(6,5);
  SonPanel05.add(area03);
  
  JButton OKButton = new JButton("OK");
  JButton CancleButton = new JButton("Cancle");
  JButton SetupButton = new JButton("SetUp");
  JButton HelpButton = new JButton("Help");
  MotherPanel.add(OKButton);
  MotherPanel.add(CancleButton);
  MotherPanel.add(SetupButton);
  MotherPanel.add(HelpButton);
  
  JLabel label01= new JLabel("Demo: ComPlexPanelDemo",JLabel.CENTER);
  BrotherPanel01.add(label01);
  JLabel label02 = new JLabel("Priority:  ");
  String[] Priority = {"High","Middle","Low"};
  JComboBox comboBox = new JComboBox(Priority);
  comboBox.setEditable(false);
  comboBox.setSelectedItem("High");
  JCheckBox checkBox = new JCheckBox("Print to File");
  BrotherPanel03.add(label02);
  BrotherPanel03.add(comboBox);
  BrotherPanel03.add(checkBox);
  
  JCheckBox checkBox01 = new JCheckBox("Image");
  JCheckBox checkBox02 = new JCheckBox("Text");
  JCheckBox checkBox03 = new JCheckBox("Code");
  SonPanel02.add(checkBox01);
  SonPanel02.add(checkBox02);
  SonPanel02.add(checkBox03);
  
  ButtonGroup buttonGroup = new ButtonGroup();
  JRadioButton radioButton01 = new JRadioButton("Selection");
  buttonGroup.add(radioButton01);
  JRadioButton radioButton02 = new JRadioButton("All");
  radioButton02.setSelected(true);
  buttonGroup.add(radioButton02);
  JRadioButton radioButton03 = new JRadioButton("Applet");
  buttonGroup.add(radioButton03);
  SonPanel04.add(radioButton01);
  SonPanel04.add(radioButton02);
  SonPanel04.add(radioButton03);
  SonPanel04.setSize(30, 50);
  
  BrotherPanel02.add(SonPanel01);
  BrotherPanel02.add(SonPanel02);
  BrotherPanel02.add(SonPanel03);
  BrotherPanel02.add(SonPanel04);
  BrotherPanel02.add(SonPanel05);
  
  
  
  GrandParentPanel.add(BrotherPanel01,BorderLayout.NORTH);
  GrandParentPanel.add(BrotherPanel02,BorderLayout.CENTER);
  GrandParentPanel.add(MotherPanel,BorderLayout.EAST);
  GrandParentPanel.add(BrotherPanel03,BorderLayout.SOUTH);
  content.add(GrandParentPanel);
  
  frame.setVisible(true);
  frame.setBounds(450, 200, 490, 260);
//  frame.pack();
  
 }
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  new ComPlexPanel();
 }

}

你可能感兴趣的:(Java复杂GUI布局举例与源码)