GUI--JFrame学习01(基本控件)
[Java进阶] Swing两万字大总结一(超详细教程,这不得收藏一波)_swing 教程_程序喵正在路上的博客-CSDN博客
1.创建第一个JFrame窗体
package learn; import javax.swing.*; import java.awt.*; public class MyFrame01 extends JFrame { public void CreateJFrame(String title){ //实例化 JFrame frame = new JFrame(title); //获取容器 Container container = frame.getContentPane(); container.setBackground(Color.CYAN); JLabel jLabel = new JLabel("这是一个JFrame窗体"); //使标签上的文字居中 jLabel.setHorizontalAlignment(SwingConstants.CENTER); //将标签添加到容器中 container.add(jLabel); //窗体可视 frame.setVisible(true); //设置窗体的位置以及大小 frame.setBounds(400,300,400,300); //设置关闭方式 frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new MyFrame01().CreateJFrame("第一个JFrame窗体"); } }
2.JDialog窗体
package learn; import javax.swing.*; import java.awt.*; /** * MyFrame+MyDialog */ public class MyFrame02 extends JFrame { public MyFrame02(){ //创建一个容器 Container container = getContentPane(); container.setLayout(null); JLabel label = new JLabel("第二个JFrame窗体"); label.setHorizontalAlignment(SwingConstants.CENTER); container.add(label); JButton button = new JButton("点击出现对话框"); button.setBounds(10,10,150,20); //按钮添加点击事件 button.addActionListener(e->{ new MyDialog(MyFrame02.this).setVisible(true); }); container.add(button); container.setBackground(Color.GRAY); setSize(400,300); setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); setVisible(true); } public static void main(String[] args) { new MyFrame02(); } } class MyDialog extends JDialog { public MyDialog(MyFrame02 frame02){ //实例化JDialog类对象,指定父窗体、标题、类型 super(frame02, "JDialog窗体", true); Container contentPane = getContentPane(); contentPane.add(new JLabel("第一个对话框")); setBounds(120,120,150,100); } }
3.JLabel标签
package learn; import javax.swing.*; /** * JLabel */ public class MyFrame03 extends JFrame { JLabel label; public MyFrame03(){ setTitle("添加标签"); label=new JLabel("第一个JLabel",JLabel.CENTER); label.setIcon(new ImageIcon("D:\\javaidea\\work01\\other\\guicha7\\src\\learn\\imgs\\img1.png")); label.setHorizontalTextPosition(JLabel.CENTER); label.setVerticalTextPosition(JLabel.BOTTOM); //设置标签不可用 label.setEnabled(false); //当标签不可用的时候显示的图像 //注释这一行后显示的图像是不可用即是灰色的 // label.setDisabledIcon(new ImageIcon("D:\\javaidea\\work01\\other\\guicha7\\src\\learn\\imgs\\img2.png")); add(label); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setBounds(400,300,800,650); setVisible(true); } public static void main(String[] args) { new MyFrame03(); } }
4.普通按钮、单选按钮、复选框
package learn; import javax.imageio.ImageIO; import javax.swing.*; import java.io.File; import java.io.IOException; /** * JButton,JRadioButton,JCheckBox */ public class MyFrame04 extends JFrame { JPanel root; JButton msgBtn,imgBtn; JRadioButton radioButton1,radioButton2,radioButton3,radioButton4; JCheckBox checkBox1,checkBox2,checkBox3,checkBox4; public MyFrame04() throws IOException { //定义面板容器 root = new JPanel(); setContentPane(root); //设置面板是绝对布局 setLayout(null); //普通按钮以及添加图片的按钮 msgBtn = new JButton("普通按钮"); msgBtn.setBounds(54,68,100,40); root.add(msgBtn); ImageIcon icon = new ImageIcon(ImageIO.read(new File("D:\\javaidea\\work01\\other\\guicha7\\src\\learn\\imgs\\img1.png"))); imgBtn = new JButton(icon); imgBtn.setBounds(196,40,120,120); root.add(imgBtn); //单选按钮 radioButton1 = new JRadioButton("学习"); radioButton2 = new JRadioButton("玩游戏"); radioButton3 = new JRadioButton("运动"); radioButton4 = new JRadioButton("听音乐"); radioButton1.setBounds(45,200,73,23); radioButton2.setBounds(134,200,73,23); radioButton3.setBounds(45,250,73,23); radioButton4.setBounds(134,250,73,23); root.add(radioButton1); root.add(radioButton2); root.add(radioButton3); root.add(radioButton4); //定义单选按钮组控件 ButtonGroup group = new ButtonGroup(); group.add(radioButton1); group.add(radioButton2); group.add(radioButton3); group.add(radioButton4); //复选框 checkBox1 = new JCheckBox("C"); checkBox2 = new JCheckBox("C++"); checkBox3 = new JCheckBox("Java"); checkBox4 = new JCheckBox("Python"); checkBox1.setBounds(45,300,73,23); checkBox2.setBounds(134,300,73,23); checkBox3.setBounds(45,350,73,23); checkBox4.setBounds(134,350,73,23); root.add(checkBox1); root.add(checkBox2); root.add(checkBox3); root.add(checkBox4); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setBounds(400,300,400,450); setVisible(true); } public static void main(String[] args) throws IOException { new MyFrame04(); } }
5.文本框、密码框
package learn; import javax.swing.*; /** * JTextField、JPasswordField */ public class MyFrame05 extends JFrame { JPanel root; JLabel unameLabel,upwdLabel; JTextField unameText; JPasswordField upwd; JButton loginBtn,exitBtn; public MyFrame05(){ //定义面板容器 root =new JPanel(); setContentPane(root); setLayout(null);//设置绝对布局 unameLabel = new JLabel("用户名: "); unameLabel.setBounds(52,33,54,15); root.add(unameLabel); unameText = new JTextField(12); unameText.setBounds(116,30,139,21); root.add(unameText); upwdLabel = new JLabel("密 码: "); upwdLabel.setBounds(52,74,54,15); root.add(upwdLabel); //密码框 upwd = new JPasswordField(12); upwd.setBounds(116,71,139,21); //设置回显字符 upwd.setEchoChar('●'); root.add(upwd); loginBtn = new JButton("登录"); loginBtn.setBounds(64,116,69,23); root.add(loginBtn); exitBtn = new JButton("退出"); exitBtn.setBounds(174,116,69,23); root.add(exitBtn); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setBounds(400,300,340,256); setVisible(true); } public static void main(String[] args) { new MyFrame05(); } }
6.JTextArea、JComboBox
package learn; import javax.swing.*; /** * JTextArea、JComboBox */ public class MyFrame06 extends JFrame { JPanel root; JTextArea textArea; JComboBox comboBox; JLabel msgLabel; public MyFrame06(String title){ //定义面板容器 super(title); root = new JPanel(); setContentPane(root); setLayout(null); //TextArea textArea = new JTextArea(); //设置文本域自动换行 textArea.setLineWrap(true); textArea.setBounds(20,10,290,200); root.add(textArea); //ComboBox msgLabel = new JLabel("请选择部门: "); msgLabel.setBounds(31,220,130,15); root.add(msgLabel); String[] options={ "生产部","销售部","财务部","采购部","市场部","宣传部" }; comboBox = new JComboBox(options); comboBox.setBounds(130,220,100,21); root.add(comboBox); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setBounds(400,300,400,400); setVisible(true); } public static void main(String[] args) { new MyFrame06("JTextArea & JComboBox"); } }