📄 second.java
字号:
package testsystem;
import java.awt.Color;
import java.awt.Container;
import java.awt.Image;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
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.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.Border;
import java.awt.Rectangle;
import javax.swing.JToggleButton;
import javax.swing.JList;
public class second extends JFrame{
public second(){
//获得窗口的容器
Container con = this.getContentPane();
//设置容器的布局管理器,当为null表示绝对布局
con.setLayout(null);
//文本框对象
JTextField jtxt = new JTextField();
//设置当前组件的大小和位置 ,
//其中前两个参数代表位置,后两个参数代表大小
jtxt.setBounds(80,20,100,20);
//将组件加入容器
con.add(jtxt);
//标签组件
JLabel jl = new JLabel();
//设置标签显示文本
jl.setText("姓名");
jl.setBounds(10,20,60,20);
con.add(jl);
//产生边框对象
Border border = BorderFactory.createLineBorder(Color.blue);
//设置标签组件的边框
jl.setBorder(border);
//图片标签
JLabel jlImage = new JLabel();
//图片对象
ImageIcon imgIcon = new ImageIcon("1.jpg");
//获得当前图片对象对应的图形对象
Image img = imgIcon.getImage();
//得到缩放之后的图形对象,100,100表示压缩图像的宽和高
img = img.getScaledInstance(100, 100, 0);
//设置标签的图片
jlImage.setIcon(new ImageIcon(img));
jlImage.setBounds(10,70,100,100);
con.add(jlImage);
//按钮
JButton jb = new JButton("添加");
jb.setBounds(10,180,80,20);
con.add(jb);
//文本域
JTextArea jta = new JTextArea();
jta.setBounds(10,210,100,100);
con.add(jta);
//复选框
JCheckBox jcb = new JCheckBox("体育");
jcb.setText("体育");
jcb.setBounds(120,210,100,20);
con.add(jcb);
//单选钮
JRadioButton jrb1 = new JRadioButton("男");
jrb1.setBounds(120,240,80,20);
con.add(jrb1);
JRadioButton jrb2 = new JRadioButton("女");
jrb2.setBounds(200,240,60,20);
con.add(jrb2);
JRadioButton jrb3 = new JRadioButton("李宇春");
jrb3.setBounds(270,240,100,20);
con.add(jrb3);
//按钮组
ButtonGroup bg = new ButtonGroup();
bg.add(jrb1);
bg.add(jrb2);
bg.add(jrb3);
//容器组件
JPanel jp = new JPanel();
jp.setBounds(120,270,200,200);
con.add(jp);
jp.setBorder(border);
jp.setLayout(null);
JLabel jlImg = new JLabel(new ImageIcon("1.jpg"));
//滚动面板
JScrollPane jsp = new JScrollPane(jlImg);
jsp.setBounds(330,270,200,200);
con.add(jsp);
//下拉框
JComboBox jcbox = new JComboBox();
//添加下拉框的选项
jcbox.addItem("高中");
jcbox.addItem("大专");
jcbox.addItem("本科");
jcbox.setBounds(10,10,100,20);
jp.add(jcbox);
this.setSize(600,600);
this.setTitle("测试");
this.setVisible(true);
//设置容器的背景色,改变窗口的显示背景
//con.setBackground(Color.red);
//设置窗体位置
//this.setLocation(100, 100);
//设置窗体居中,必须放在setVisible之下
this.setLocationRelativeTo(null);
//设置窗口大小不可调整
this.setResizable(false);
//设置窗口关闭时,程序结束
this.setDefaultCloseOperation(3);
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
new second();
}
private void jbInit() throws Exception {
jTextField1.setText("jTextField1");
jTextField1.setBounds(new Rectangle(216, 113, 221, 91));
this.getContentPane().add(jTextField1);
jList1.setBounds(new Rectangle(48, 94, 56, 20));
this.getContentPane().add(jToggleButton1);
this.getContentPane().add(jList1);
jToggleButton1.setText("jToggleButton1");
jToggleButton1.setBounds(new Rectangle(287, 44, 97, 25));
}
JTextField jTextField1 = new JTextField();
JToggleButton jToggleButton1 = new JToggleButton();
JList jList1 = new JList();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -