美文网首页
GUI Swing 组件常用方法

GUI Swing 组件常用方法

作者: TryCatch菌 | 来源:发表于2018-10-01 00:10 被阅读0次

GUI Swing 组件常用方法
在学习JAVA的过程中,会涉及到一些简单的页面设计等操作,以下就为一些常用的JFrame类中的一些组件的调用方法,顺带可以体会继承的概念extends

GUI
图形化用户界面,在JAVA中用Awt和Swing来构建JAVA的风格界面组件

1.Awt:awt是使用操作系统平台本身提供组件风格来构建JAVA的GUI组件,所以在跨平台时,显示风格不一致
2.Swing:弥补了Awt类包的不足,有自己的显示风格,这样在跨平台时,显示风格会保持一致

GUI显示特点,先进先出,最先加入窗体的组件在最上层,最后加入的组件在最下层

这段代码简单的演示了如何设置JFrame窗体

import javax.swing.JFrame;

public class SwingTest {

      public static void main(String[] args) {
            // 导入swing类的JFrame包(框架)
            JFrame j=new JFrame();
            //设置窗体大小
            j.setSize(500,400);
            //设置窗体可见
            j.setVisible(true);
            //设置窗体相对屏幕居中
            j.setLocationRelativeTo(null);
            //关闭窗口,程序结束,3表示第三个按钮
            j.setDefaultCloseOperation(3);
            //设置窗体标题
            j.setTitle("我的窗体");
      }

}

这段代码演示如果添加Label,文本框,按钮组件,下拉组件,单选框,多选框等常用的组件

import java.awt.Image;

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.JRadioButton;
import javax.swing.JTextField;

public class SwingExtends extends JFrame {

      public static void main(String[] args) {
            // TODO Auto-generated method stub
            SwingExtends s = new SwingExtends();

      }

      public SwingExtends() {
            this.setTitle("我的窗体");
            // 设置窗体的布局器,null布局含义是,使用组件的大小和未在定位组件
            this.setLayout(null);

            // 标签组件
            JLabel j1 = new JLabel("姓名");

            // 设置标签的位置X和Y值和大小(长宽)
            j1.setBounds(50, 50, 80, 20);
            // 将标签加入窗体
            this.add(j1);

            // 文本框组件
            JTextField jtxt = new JTextField();
            jtxt.setBounds(140, 50, 120, 20);
            this.add(jtxt);

            // 创建图片标签
            JLabel imgLabel = new JLabel();
            // 得到图片
            Image img = new ImageIcon("img/IMG.PNG").getImage();
            // 将图片缩放到200*100
            img = img.getScaledInstance(200, 100, 1);
            // 设置标签的图片
            imgLabel.setIcon(new ImageIcon(img));
            // 20 20表示偏移量为20,600,600表示图片大小
            imgLabel.setBounds(20, 20, 600, 400);
            this.add(imgLabel);

            // 设置标签的图片
            imgLabel.setIcon(new ImageIcon(img));
            // 按钮
            JButton button = new JButton("确定");
            button.setBounds(200, 350, 80, 20);
            this.add(button);

            // 下拉框
            JComboBox box = new JComboBox();
            box.addItem("小学");
            box.addItem("初中");
            box.addItem("高中");
            box.setBounds(400, 350, 120, 20);
            this.add(box);

            // 单选框
            JRadioButton radiol = new JRadioButton("男");
            radiol.setBounds(300, 50, 80, 20);
            this.add(radiol);
            JRadioButton radion2 = new JRadioButton("女");
            radion2.setBounds(400, 50, 80, 20);
            this.add(radion2);

            //复选框
            JCheckBox jc=new JCheckBox("记住密码");
            jc.setBounds(300,100,80,20);
            this.add(jc);

            // 设置窗体大小 长宽
            this.setSize(800, 800);
            // 设置窗体可见
            this.setVisible(true);
            // 关闭窗口,程序结束,3表示第三个按钮
            this.setDefaultCloseOperation(3);
            // 设置窗体相对屏幕居中
            this.setLocationRelativeTo(null);
            //窗体大小不能改变
            this.setResizable(false);
      }
}

如果需在JFame窗体内添加窗体,需导入小型容器JPanel

import java.awt.Color;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;

public class PanelFrame extends JFrame {

      public static void main(String[] args) {
            //初始化对象,构造器方法调用
            PanelFrame f=new PanelFrame();

      }
      public PanelFrame(){
            //初始化JFrame布局
            this.setLayout(null);

            //创建红色边框
            Border border=BorderFactory.createLineBorder(Color.red,10);
            //创建小型容器
            JPanel jp=new JPanel();
            jp.setBounds(30,30, 200, 200);
            jp.setBorder(border);
            this.add(jp);

            //初始化JPanel容器布局
            jp.setLayout(null);
            //在jp容器中添加文本框
            JTextField text=new JTextField();
            text.setBounds(20, 20, 50, 50);
            jp.add(text);

            //创建边框
            this.setSize(800,600);
            this.setDefaultCloseOperation(3);
            this.setLocationRelativeTo(null);
            this.setVisible(true);
      }
}

这段代码演示如何添加多选页面

this.setLayout(null);
            //设置窗体组件,选择页面
            JTabbedPane jta=new JTabbedPane();
            jta.setBounds(0, 0, 650, 350);
            jta.add("网络设置", new Inter());
            jta.add("登录设置", new Entry());
            this.add(jta);

下面我们用以上代码组件简单的演示如何建立一个简易的网络设置/登录选项界面,以QQ的设置界面为参照界面

JFrame 窗体代码

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;

public class QQtest extends JFrame {

      public static void main(String[] args) {
            QQtest q = new QQtest();

      }

      public QQtest() {
            //初始化窗口布局
            this.setLayout(null);
            //设置窗体组件,选择页面
            JTabbedPane jta=new JTabbedPane();
            jta.setBounds(0, 0, 650, 350);
            jta.add("网络设置", new Inter());
            jta.add("登录设置", new Entry());
            this.add(jta);

            //设置按钮
            Button bu1=new Button("确定",430,360,this);
            Button bu2=new Button("取消",530,360,this);

            //设置窗体尺寸
            this.setSize(650,450);
            this.setDefaultCloseOperation(3);
            this.setLocationRelativeTo(null);
            this.setVisible(true);
            this.setTitle("设置");

      }
}

登录设置界面

import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Entry extends JPanel {
      public Entry(){
            //初始化文本框组件
            this.setLayout(null);

            //文本框组件
            JLabel jt=new JLabel();
            jt.setText("登录窗口");
            jt.setBounds(30, 20, 130, 20);
            this.add(jt);

            RadioBut r1=new RadioBut("使用QQ2011登录窗口",50,50,this);
            RadioBut r2=new RadioBut("使用经典登录窗口",50,70,this);

            //插入图片
            JLabel jlimg=new JLabel();
            Image img=new ImageIcon("img/1.jpg").getImage();
            img=img.getScaledInstance(370, 210, 1);
            jlimg.setIcon(new ImageIcon(img));
            jlimg.setBounds(100, 100, 370, 210);
            this.add(jlimg);
      }
}

网络设置界面

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Inter extends JPanel {
      public Inter() {
            this.setLayout(null);
            //文本框组件
            Label l1 = new Label("网络设置", 20, 20, this);
            Label l2 = new Label("类型:", 100, 60, this);
            Label l3 = new Label("地址:", 250, 60, this);
            Label l4 = new Label("端口:", 400, 60, this);
            Label l5 = new Label("用户名:", 100, 130, this);
            Label l6 = new Label("密码:", 250, 130, this);
            Label l7 = new Label("域:", 400, 130, this);
            Label l8 = new Label("登录服务器", 20, 200, this);
            Label l9 = new Label("类型:", 100, 230, this);
            Label l10 = new Label("地址:", 250, 230, this);
            Label l11= new Label("端口:", 400, 230, this);

            //输入框组件
            Txt t1=new Txt(250, 90, this);
            Txt t2=new Txt(400, 90, this);
            Txt t3=new Txt(400, 90, this);
            Txt t4=new Txt(100, 160, this);
            Txt t5=new Txt(250, 160, this);
            Txt t6=new Txt(400, 160, this);
            Txt t7=new Txt(100, 260, this);
            Txt t8=new Txt(250, 260, this);
            Txt t9=new Txt(400, 260, this);

            //按钮组件
//          Button b1=new Button("测试",470,190,this);
            JButton b=new JButton();
            b.setText("测试");
            b.setBounds(470, 190, 60, 20);
            this.add(b);



            //做一个下拉框
            JComboBox jc=new JComboBox();
            jc.setBounds(100, 90, 130, 20);
            jc.addItem("不使用代理");
            jc.addItem("HTTP代理");
            jc.addItem("SOCKSS代理");
            jc.addItem("使用游览器设置");
            this.add(jc);
      }
}

创建4种按钮组件

import javax.swing.JLabel;
//标签组件
public class Label extends JLabel {
      public Label(String text,int x,int y,Inter ter){
            this.setLayout(null);
            this.setText(text);
            this.setBounds(x, y, 80, 30);
            ter.add(this);
      }
}

import javax.swing.JPanel;
import javax.swing.JTextField;
//文本框组件
public class Txt extends JTextField{
      public Txt(int x,int y,Inter ter){
            this.setLayout(null);
            this.setBounds(x, y, 130, 20);
            ter.add(this);
      }
}

import javax.swing.JButton;
import javax.swing.JPanel;
//按钮组件
public class Button extends JButton {
      public Button(String txt,int x,int y,QQtest test){
            this.setLayout(null);
            this.setText(txt);
            this.setBounds(x, y, 60, 20);
            test.add(this);
      }
}

import javax.swing.JRadioButton;
//单选框组件
public class RadioBut extends JRadioButton {
      public RadioBut(String text,int x,int y,Entry ry){
            this.setLayout(null);
            this.setText(text);
            this.setBounds(x, y, 200, 20);
            ry.add(this);

      }
}

以上代码是放在一个工程内,添加适当的图片即可完成简易的设置界面的操作,同过以上练习也可以体会extends继承的概念

相关文章

网友评论

      本文标题:GUI Swing 组件常用方法

      本文链接:https://www.haomeiwen.com/subject/wrsvoftx.html