我给你的代码不光有元,还有方,椭圆等等
你复制下来运行下就行了。要是喜欢就仔细研究下
package kh;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JRadioButton;
import java.awt.Font;
import java.awt.Insets;
import javax.swing.JLabel;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JPasswordField;
import javax.swing.JFormattedTextField;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JSlider;
import javax.swing.JProgressBar;
import javax.swing.JTable;
import java.awt.*;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JPopupMenu;
import javax.swing.JMenuItem;
/**
*
Title:
*
*
Description:
*
*
Copyright: Copyright (c) 2007
*
*
Company:
*
* @author not attributable
* @version 1.0
*/
public class Frame1 extends JFrame {
JPanel contentPane;
JButton jButton1 = new JButton();
JRadioButton jRadioButton1 = new JRadioButton();
JRadioButton jRadioButton2 = new JRadioButton();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
ButtonGroup buttonGroup1 = new ButtonGroup();
JCheckBox jCheckBox1 = new JCheckBox();
JCheckBox jCheckBox2 = new JCheckBox();
JCheckBox jCheckBox3 = new JCheckBox();
JLabel jLabel3 = new JLabel();
JTextField jTextField1 = new JTextField();
JLabel jLabel4 = new JLabel();
JTextArea jTextArea1 = new JTextArea();
JPasswordField jPasswordField1 = new JPasswordField();
JLabel jLabel5 = new JLabel();
JComboBox jComboBox1 = new JComboBox();
JLabel jLabel6 = new JLabel();
JList jList1 = new JList();
JLabel jLabel7 = new JLabel();
String[] s={"《神曲》","《十日谈》","《堂.吉柯德》","《雪国》","《枕草子》","《围城》","《我们仨》"};
JScrollPane jScrollPane1 = new JScrollPane();
JScrollPane jScrollPane2 = new JScrollPane();
JSlider jSlider1 = new JSlider();
JLabel jLabel8 = new JLabel();
JProgressBar jProgressBar1 = new JProgressBar();
JLabel jLabel9 = new JLabel();
JMenuBar jMenuBar1 = new JMenuBar();
JMenu jMenu1 = new JMenu();
JMenuItem jMenuItem1 = new JMenuItem();
JMenuItem jMenuItem2 = new JMenuItem();
JMenuItem jMenuItem3 = new JMenuItem();
JMenuItem jMenuItem4 = new JMenuItem();
public Frame1() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
this.setJMenuBar(jMenuBar1); //在容器对象的层板加进去菜单条
setSize(new Dimension(500, 530));
setTitle("组件展示");
jButton1.setBounds(new Rectangle(136, 22, 120, 34));
jButton1.setFont(new java.awt.Font("宋体", Font.PLAIN, 12));
jButton1.setMargin(new Insets(2, 2, 2, 2));
jButton1.setText("按钮:JButton");
jRadioButton1.setFont(new java.awt.Font("宋体", Font.PLAIN, 12));
jRadioButton1.setSelected(true);
jRadioButton1.setText("男");
jRadioButton1.setBounds(new Rectangle(297, 16, 41, 32));
jRadioButton2.setFont(new java.awt.Font("宋体", Font.PLAIN, 12));
jRadioButton2.setText("女");
jRadioButton2.setBounds(new Rectangle(345, 16, 63, 33));
jLabel1.setFont(new java.awt.Font("宋体", Font.PLAIN, 12));
jLabel1.setText("标签:JLabel");
jLabel1.setBounds(new Rectangle(18, 23, 106, 28));
jLabel2.setFont(new java.awt.Font("宋体", Font.PLAIN, 12));
jLabel2.setText("单选按钮:JRadioButton");
jLabel2.setBounds(new Rectangle(281, 36, 137, 41));
jCheckBox1.setFont(new java.awt.Font("宋体", Font.PLAIN, 12));
jCheckBox1.setText("音乐");
jCheckBox1.setBounds(new Rectangle(17, 89, 53, 27));
jCheckBox2.setFont(new java.awt.Font("宋体", Font.PLAIN, 12));
jCheckBox2.setText("美术");
jCheckBox2.setBounds(new Rectangle(67, 89, 53, 26));
jCheckBox3.setFont(new java.awt.Font("宋体", Font.PLAIN, 12));
jCheckBox3.setText("体育");
jCheckBox3.setBounds(new Rectangle(118, 92, 53, 21));
jLabel3.setFont(new java.awt.Font("宋体", Font.PLAIN, 12));
jLabel3.setText("复选框:JCheckBox");
jLabel3.setBounds(new Rectangle(22, 118, 117, 21));
jLabel4.setFont(new java.awt.Font("宋体", Font.PLAIN, 12));
jLabel4.setText("文本框:JTextField");
jLabel4.setBounds(new Rectangle(264, 120, 128, 23));
jTextArea1.setText("文本域:JTextArea");
jLabel5.setFont(new java.awt.Font("宋体", Font.PLAIN, 12));
jLabel5.setText("密码框:JPasswordField");
jLabel5.setBounds(new Rectangle(12, 203, 135, 31));
jLabel6.setFont(new java.awt.Font("宋体", Font.PLAIN, 12));
jLabel6.setText("下拉列表框:JComboBox");
jLabel6.setBounds(new Rectangle(22, 304, 136, 27));
jComboBox1.addItem("网络06");
jComboBox1.addItem("维护06");
jComboBox1.setFont(new java.awt.Font("宋体", Font.PLAIN, 12));
jComboBox1.setBounds(new Rectangle(29, 261, 112, 29));
jLabel7.setFont(new java.awt.Font("宋体", Font.PLAIN, 12));
jLabel7.setText("列表框:JList");
jLabel7.setBounds(new Rectangle(264, 330, 103, 31));
jList1=new JList(s);
jList1.setFont(new java.awt.Font("宋体", Font.PLAIN, 12));
jLabel8.setFont(new java.awt.Font("宋体", Font.PLAIN, 12));
jLabel8.setText("标尺:JSlider");
jLabel8.setBounds(new Rectangle(38, 444, 87, 25));
jLabel9.setFont(new java.awt.Font("宋体", Font.PLAIN, 12));
jLabel9.setText("进度指示器:JProgressBar");
jLabel9.setBounds(new Rectangle(242, 430, 151, 25));
jSlider1.setMajorTickSpacing(20);
jSlider1.setMaximum(100);
jSlider1.setMinorTickSpacing(5);
jSlider1.setPaintLabels(true);
jSlider1.setPaintTicks(true);
jSlider1.setBounds(new Rectangle(30, 378, 126, 48));
jProgressBar1.setStringPainted(true);
jProgressBar1.setBounds(new Rectangle(250, 381, 117, 25));
jProgressBar1.setModel(jSlider1.getModel());
jScrollPane2.setBounds(new Rectangle(265, 246, 115, 74));
jScrollPane1.setBounds(new Rectangle(260, 153, 118, 73));
jPasswordField1.setBounds(new Rectangle(16, 164, 113, 27));
jTextField1.setBounds(new Rectangle(261, 92, 122, 24));
jMenu1.setText("文件");
jMenuItem1.setText("新建");
jMenuItem2.setText("打开");
jMenuItem3.setText("保存");
jMenuItem4.setText("退出");
buttonGroup1.add(jRadioButton1);
buttonGroup1.add(jRadioButton2);
jScrollPane2.getViewport().add(jList1);
jScrollPane1.getViewport().add(jTextArea1);
contentPane.add(jSlider1, null);
contentPane.add(jLabel8, null);
contentPane.add(jComboBox1, null);
contentPane.add(jLabel6, null);
contentPane.add(jPasswordField1, null);
contentPane.add(jLabel5, null);
contentPane.add(jCheckBox2, null);
contentPane.add(jCheckBox1, null);
contentPane.add(jCheckBox3, null);
contentPane.add(jLabel3, null);
contentPane.add(jLabel4, null);
contentPane.add(jTextField1, null);
contentPane.add(jLabel1, null);
contentPane.add(jButton1, null);
contentPane.add(jRadioButton1, null);
contentPane.add(jRadioButton2, null);
contentPane.add(jLabel2, null);
contentPane.add(jScrollPane1, null);
contentPane.add(jScrollPane2, null);
contentPane.add(jLabel7, null);
contentPane.add(jProgressBar1, null);
contentPane.add(jLabel9, null);
jMenuBar1.add(jMenu1);
jMenu1.add(jMenuItem1);
jMenu1.add(jMenuItem2);
jMenu1.add(jMenuItem3);
jMenu1.addSeparator();
jMenu1.add(jMenuItem4);
}
}