【GUI编程】图形用户界面之Swing

目录

Swing

JFrame窗体

JDialog弹窗

Icon标签

文本域JScroll面板

图片按钮、单选项、多选框

下拉框、列表框

文本框、密码框、文本域


Swing

JFrame窗体

 package gui;
 ​
 import javax.swing.*;
 import java.awt.*;
 ​
 public class JFrameDemo extends JFrame{
 ​
     //初始化
     public void init(){
         //顶级窗口
         JFrame jframe = new JFrame("这是一个JFrame窗口");
         jframe.setVisible(true);
         jframe.setBounds(600,300,600,500);
         jframe.setBackground(Color.BLUE);
 ​
         //设置文字
         JLabel jLabel = new JLabel("欢迎来到Java世界!",SwingConstants.CENTER);
         jframe.add(jLabel);
 ​
         //容器实例化
         Container container = this.getContentPane();
         container.setBackground(Color.BLUE);
 ​
         //关闭事件
         jframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
     }
 ​
     public static void main(String[] args) {
         //建立一个窗口
         new JFrameDemo().init();
     }
 }

JDialog弹窗

演示:弹窗

 package gui;
 ​
 import javax.swing.*;
 import java.awt.*;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 ​
 public class DialogDemo extends JFrame {
 ​
     public DialogDemo(){
         this.setVisible(true);
         this.setBounds(600,300,600,500);
 ​
         //JFrame 放东西,需要容器
         Container contentPane = this.getContentPane();
         //绝对布局
         contentPane.setLayout(null);
 ​
         //按钮
         JButton jButton = new JButton("点击弹出一个对话框");
         jButton.setBounds(180,150,200,50);
 ​
         //点击这个按钮的时候,弹出一个弹窗
         jButton.addActionListener(new ActionListener() {//监听器
             @Override
             public void actionPerformed(ActionEvent e) {
                 //弹窗
                 new MyDialogDemo();
             }
         });
 ​
         contentPane.add(jButton);
     }
 ​
     public static void main(String[] args) {
         new DialogDemo();
     }
 }
 ​
 //弹窗的窗口
 class MyDialogDemo extends JDialog{
     public MyDialogDemo(){
         this.setVisible(true);
         this.setBounds(600,300,100,70);
 ​
 ​
         Container contentPane = this.getContentPane();
 ​
         Component java = contentPane.add(new Label("java"));
     }
 }

Icon标签

Icon标签

 package gui;
 ​
 import javax.swing.*;
 import java.awt.*;
 ​
 //图标,需要实现类,Frame集成
 public class IconDemo extends JFrame implements Icon {
 ​
 ​
     private int width;
     private int height;
 ​
     public IconDemo(){}//无参构造
 ​
     public IconDemo(int width,int height){//有参构造
         this.width = width;
         this.height = height;
     }
 ​
     public void init(){
         IconDemo iconDemo = new IconDemo(15, 15);
         //图标放在标签上,也可以放在按钮上!
         JLabel jLabel = new JLabel("Icon", iconDemo, SwingConstants.CENTER);
 ​
         Container contentPane = getContentPane();
         contentPane.add(jLabel);
 ​
         setVisible(true);
 ​
     }
 ​
     public static void main(String[] args) {
         new IconDemo().init();
     }
 ​
     @Override
     public void paintIcon(Component c, Graphics g, int x, int y) {
         g.fillOval(x,y,width,height);
     }
 ​
     @Override
     public int getIconWidth() {
         return width;
     }
 ​
     @Override
     public int getIconHeight() {
         return height;
     }
 }

文本域JScroll面板

演示:JPanel

 package gui;
 ​
 import javax.swing.*;
 import java.awt.*;
 ​
 public class JPanelDemo extends JFrame {
     public JPanelDemo(){
         Container container = getContentPane();
         container.setLayout(new GridLayout(2,1,10,10));//后面参数的意思是间距
 ​
         JPanel jPanel1 = new JPanel(new GridLayout(1,3));
         JPanel jPanel2 = new JPanel(new GridLayout(1,2));
         JPanel jPanel3 = new JPanel(new GridLayout(2,1));
         JPanel jPanel4 = new JPanel(new GridLayout(2,3));
 ​
         jPanel1.add(new JButton("1"));
         jPanel1.add(new JButton("1"));
         jPanel1.add(new JButton("1"));
 ​
         jPanel2.add(new JButton("2"));
         jPanel2.add(new JButton("2"));
 ​
         jPanel3.add(new JButton("3"));
         jPanel3.add(new JButton("3"));
 ​
         jPanel4.add(new JButton("4"));
         jPanel4.add(new JButton("4"));
         jPanel4.add(new JButton("4"));
         jPanel4.add(new JButton("4"));
         jPanel4.add(new JButton("4"));
         jPanel4.add(new JButton("4"));
 ​
         container.add(jPanel1);
         container.add(jPanel2);
         container.add(jPanel3);
         container.add(jPanel4);
 ​
         setVisible(true);
         setSize(500,500);
 ​
     }
 ​
     public static void main(String[] args) {
         new JPanelDemo();
     }
 }

演示:JScrollPanel

 package gui;
 ​
 import javax.swing.*;
 import java.awt.*;
 ​
 public class JScrollDemo extends JFrame {
     public JScrollDemo(){
         Container container = getContentPane();
 ​
         //文本域
         JTextArea textArea = new JTextArea(20,50);
         textArea.setText("欢迎来到Java世界");
         container.add(textArea);
 ​
         //面板
         JScrollPane scrollPane = new JScrollPane(textArea);
         container.add(scrollPane);
 ​
         setVisible(true);
         setBounds(600,300,600,300);
     }
 ​
     public static void main(String[] args) {
         new JScrollDemo();
     }
 }

图片按钮、单选项、多选框

演示:图片按钮

 package gui;
 ​
 import javax.swing.*;
 import java.awt.*;
 import java.net.URL;
 ​
 public class JButtonDemo extends JFrame {
     public JButtonDemo() {
         Container container = getContentPane();
 ​
         //讲一个图片变为图标
         URL url = JButtonDemo.class.getResource("tour.jpg");
         ImageIcon icon = new ImageIcon(url);
 ​
         //把这个图标放在按钮上
         JButton button = new JButton();
         button.setIcon(icon);
         button.setToolTipText("图片按钮");
 ​
         //add
         container.add(button);
 ​
         setVisible(true);
         setBounds(600,300,600,500);
     }
 ​
     public static void main(String[] args) {
         new JButtonDemo();
     }
 }

演示:单选框

 
package gui;
 ​
 import javax.swing.*;
 import java.awt.*;
 import java.net.URL;
 ​
 public class JButtonDemo extends JFrame {
     public JButtonDemo() {
         Container container = getContentPane();
 ​
         //讲一个图片变为图标
         URL url = JButtonDemo.class.getResource("tour.jpg");
         ImageIcon icon = new ImageIcon(url);
 ​
         //单选框
         JRadioButton radioButton1 = new JRadioButton("JRadioButton01");
         JRadioButton radioButton2 = new JRadioButton("JRadioButton02");
         JRadioButton radioButton3 = new JRadioButton("JRadioButton03");
 ​
         //由于单选框只能选择一个,分组,一个组只能选择一个
         ButtonGroup buttonGroup = new ButtonGroup();
         buttonGroup.add(radioButton1);
         buttonGroup.add(radioButton2);
         buttonGroup.add(radioButton3);
 ​
         //add
         container.add(radioButton1,BorderLayout.CENTER);
         container.add(radioButton2,BorderLayout.NORTH);
         container.add(radioButton3,BorderLayout.SOUTH);
 ​
         setVisible(true);
         setBounds(600,300,600,500);
         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
     }
 ​
     public static void main(String[] args) {
         new JButtonDemo();
     }
 }

演示:多选框

 package gui;
 ​
 import javax.swing.*;
 import java.awt.*;
 import java.net.URL;
 ​
 public class JButtonDemo extends JFrame {
     public JButtonDemo() {
         Container container = getContentPane();
 ​
         //讲一个图片变为图标
         URL url = JButtonDemo.class.getResource("tour.jpg");
         ImageIcon icon = new ImageIcon(url);
 ​
         //多选框
         JCheckBox jCheckBox1 = new JCheckBox("jCheckBox1");
         JCheckBox jCheckBox2 = new JCheckBox("jCheckBox2");
 ​
         container.add(jCheckBox1,BorderLayout.NORTH);
         container.add(jCheckBox2,BorderLayout.SOUTH);
 ​
 ​
         setVisible(true);
         setBounds(600,300,600,500);
         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
     }
 ​
     public static void main(String[] args) {
         new JButtonDemo();
     }
 }

下拉框、列表框

演示:下拉框

 
package gui;
 ​
 import javax.swing.*;
 import java.awt.*;
 ​
 public class TestComboboxDemo extends JFrame {
 ​
     public TestComboboxDemo() {
 ​
         Container container = getContentPane();
 ​
         JComboBox status = new JComboBox();
         status.addItem (null);
         status.addItem ("正在热映");
         status.addItem ("已下架");
         status.addItem ("即将上映");
 ​
         container.add (status);
 ​
 ​
         setVisible(true);
         setBounds(600,300,500,400);
         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
     }
 ​
     public static void main(String[] args) {
 ​
     }
 }

演示:列表框

 
package gui;
 ​
 import javax.swing.*;
 import java.awt.*;
 ​
 public class TestComboboxDemo extends JFrame {
 ​
     public TestComboboxDemo() {
 ​
         Container container = getContentPane();
 ​
         //生成列表的内容
         String[] contents = {"1","2","3"};
         //列表中需要放入内容
         JList jList = new JList(contents);
 ​
         container.add(jList);
         setVisible(true);
         setBounds(600,300,500,400);
         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
     }
 ​
     public static void main(String[] args) {
         new TestComboboxDemo ();
     }
 }

文本框、密码框、文本域

演示:文本框

 package gui;
 ​
 import javax.swing.*;
 import java.awt.*;
 ​
 public class TestTextDemo extends JFrame {
     public TestTextDemo() {
 ​
         Container container = getContentPane();
 ​
         JTextField jTextField1 = new JTextField ("hello");
         JTextField jTextField2 = new JTextField ("world",20);
 ​
         container.add (jTextField1,BorderLayout.NORTH);
         container.add (jTextField2,BorderLayout.CENTER);
 ​
         setVisible(true);
         setBounds(600,300,500,400);
         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
     }
 ​
     public static void main(String[] args) {
         new TestTextDemo();
     }
 }

演示:密码框

 
package gui;
 ​
 import javax.swing.*;
 import java.awt.*;
 ​
 public class TestTextDemo extends JFrame {
     public TestTextDemo() {
 ​
         Container container = getContentPane();
 ​
         JPasswordField jPasswordField = new JPasswordField ();//默认为****
         jPasswordField.setEchoChar('*');
         container.add (jPasswordField);
 ​
         setVisible(true);
         setBounds(600,300,500,400);
         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
     }
 ​
     public static void main(String[] args) {
         new TestTextDemo();
     }
 }

演示:文本域

 
package gui;
 ​
 import javax.swing.*;
 import java.awt.*;
 ​
 public class TestTextDemo extends JFrame {
     public TestTextDemo() {
 ​
         Container container = getContentPane();
 ​
         JTextArea textArea = new JTextArea (20, 50);
         textArea.setText ("欢迎来到Java世界!");
 ​
         //Scroll
         JScrollPane scrollPane = new JScrollPane(textArea);
         container.add (scrollPane);
 ​
         setVisible(true);
         setBounds(600,300,500,400);
         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
     }
 ​
     public static void main(String[] args) {
         new TestTextDemo();
     }
 }

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值