代码实现
public class Application_1 { public static void main(String args[]){ ComponentInWindow win= new ComponentInWindow(); win.setBounds(100,100,500,200); win.setTitle("常用组件"); } }
import javax.swing.*; import java.awt.*; public class ComponentInWindow extends JFrame { JTextField text; JButton button; JCheckBox checkBox1,checkBox2,checkBox3; JRadioButton radio1,radio2; ButtonGroup group; JComboBox comBox; JTextArea area; public ComponentInWindow(){ init(); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } void init(){ setLayout(new FlowLayout()); add(new JLabel("文本框")); text =new JTextField(10); add(text); add(new JLabel("按钮:")); button =new JButton("确认"); button.setBackground(Color.cyan); add(button); add(new JLabel("选择框:")); checkBox1 =new JCheckBox("音乐"); checkBox2 =new JCheckBox("文学"); checkBox3 =new JCheckBox("游泳"); add(checkBox1); add(checkBox2); add(checkBox3); add(new JLabel("单选按钮")); group =new ButtonGroup(); radio1 =new JRadioButton("理科"); radio2 =new JRadioButton("文科"); group.add(radio1); group.add(radio2); add(radio1); add(radio2); add(new JLabel("下拉列表")); comBox =new JComboBox<String>(); comBox.addItem("第一季度"); comBox.addItem("第二季度"); comBox.addItem("第三季度"); comBox.addItem("第四季度"); add(comBox); add(new JLabel("文本区")); area =new JTextArea(6,12); add(new JScrollPane(area)); } }