博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java中GUI编程总结—Swing(窗口、面板、弹窗、标签、按钮、列表、文本框)
阅读量:4072 次
发布时间:2019-05-25

本文共 9881 字,大约阅读时间需要 32 分钟。

在这里插入图片描述

3.Swing

3.1 窗口 面板

//标签居中class Myframe2 extends JFrame{
public void init(){
this.setBounds(10,10,200,300); this.setVisible(true); JLabel lable=new JLabel("欢迎来到狂神"); this.add(lable); //让文本标签居中,设置水平对齐 lable.setHorizontalAlignment(SwingConstants.CENTER); //获得一个容器 Container container=this.getContentPane(); container.setBackground(Color.yellow); }}
//初始化public class javaPractice {
//init();初始化 public void init(){
//顶级窗口 JFrame jf=new JFrame("这是一个JFrame窗口"); jf.setBounds(10,10,200,300); jf.setVisible(true); jf.setBackground(Color.blue); JLabel lable=new JLabel("欢迎来到狂神"); jf.add(lable); //关闭事件 jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) {
//建立一个窗口 new javaPractice().init(); }}

3.2 弹窗

public class javaPractice extends JFrame {
public javaPractice(){
this.setVisible(true); this.setBounds(100,100,700,500); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //JFrame 放东西 容器 Container container=this.getContentPane(); //绝对布局 container.setLayout(null); //按钮 JButton button=new JButton("点击弹出一个对话框"); button.setBounds(30,30,200,50); //点击这个按钮的时候,弹出一个窗口 button.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
//弹窗 new MyDialog(); } }); container.add(button); } public static void main(String[] args) {
new javaPractice(); }}class MyDialog extends JDialog{
public MyDialog() {
this.setVisible(true); this.setBounds(200,200,400,400); //默认就有this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); Container container=this.getContentPane(); container.setLayout(null); container.add(new Label("学习java")); }}

3.3 标签

label

new TLabel("xxx")

图标 icon

//图标、需要实现的类,Frame继承public class javaPractice extends JFrame implements Icon {
private int width; private int height; public javaPractice() {
//无参构造 } public javaPractice(int width,int height) {
this.width=width; this.height=height; } public void init(){
javaPractice iconDemo=new javaPractice(15,15); //图标放在标签上,也可以放在按钮上! JLabel label =new JLabel("icontest",iconDemo,SwingConstants.CENTER); Container container=getContentPane(); container.add(label); this.setVisible(true); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) {
new javaPractice().init(); } @Override public void paintIcon(Component c, Graphics g, int x, int y) {
g.fillOval(x,y,width,height); } @Override public int getIconWidth() {
return this.width; } @Override public int getIconHeight() {
return this.height; }}

图片 icon

public class javaPractice extends JFrame {
public javaPractice(){
//获取图片的地址 JLabel label=new JLabel("Imageicon"); URL url=javaPractice.class.getResource("tx.jpg"); ImageIcon imageIcon=new ImageIcon(url); label.setIcon(imageIcon); label.setHorizontalAlignment(SwingConstants.CENTER); Container container=getContentPane(); container.add(label); setVisible(true); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setBounds(100,100,200,200); } public static void main(String[] args) {
new javaPractice(); }}

3.4 面板

  • JPanel
public class javaPractice extends JFrame {
public javaPractice(){
Container container=this.getContentPane(); container.setLayout(new GridLayout(2,1,10,10)); JPanel panel1=new JPanel(new GridLayout(1,3)); panel1.add(new JButton("1")); panel1.add(new JButton("1")); panel1.add(new JButton("1")); container.add(panel1); this.setVisible(true); this.setSize(500,500); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) {
new javaPractice(); }}
  • JScroll
public class javaPractice extends JFrame {
public javaPractice(){
Container container=this.getContentPane(); //文本域 JTextArea textArea=new JTextArea(20,50); textArea.setText("欢迎学习狂神说java"); //Scroll面板 JScrollPane scrollPane=new JScrollPane(textArea); container.add(scrollPane); this.setVisible(true); this.setBounds(100,100,300,500); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) {
new javaPractice(); }}

在这里插入图片描述

3.5 按钮

  • 图片按钮
public class javaPractice extends JFrame {
public javaPractice(){
Container container=this.getContentPane(); //将一个图片变为图标 URL resource=javaPractice.class.getResource("tx.jpg"); Icon icon=new ImageIcon(resource); //把这个图标放在按钮上 JButton button=new JButton(); button.setIcon(icon); button.setToolTipText("图片按钮"); container.add(button); this.setVisible(true); this.setBounds(100,100,300,500); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) {
new javaPractice(); }}
  • 单选按钮
public class javaPractice extends JFrame {
public javaPractice(){
Container container=this.getContentPane(); //将一个图片变为图标 URL resource=javaPractice.class.getResource("tx.jpg"); Icon icon=new ImageIcon(resource); //单选框 JRadioButton radioButton1=new JRadioButton("button1"); JRadioButton radioButton2=new JRadioButton("button2"); JRadioButton radioButton3=new JRadioButton("button3"); //单选只能选择一个,所以一个组中只能选择一个 container.add(radioButton1,BorderLayout.CENTER); container.add(radioButton2,BorderLayout.NORTH); container.add(radioButton3,BorderLayout.SOUTH); this.setVisible(true); this.setBounds(100,100,300,500); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) {
new javaPractice(); }}
  • 复选按钮
public class javaPractice extends JFrame {
public javaPractice(){
Container container=this.getContentPane(); //多选框 JCheckBox checkBox01=new JCheckBox("checkBox01"); JCheckBox checkBox02=new JCheckBox("checkBox02"); container.add(checkBox01,BorderLayout.NORTH); container.add(checkBox02,BorderLayout.SOUTH); this.setVisible(true); this.setBounds(100,100,300,500); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) {
new javaPracti ce(); }}

3.6 列表

  • 下拉框
public class javaPractice extends JFrame {
public javaPractice(){
Container container=this.getContentPane(); JComboBox status=new JComboBox(); status.addItem(null); status.addItem("正在热映"); status.addItem("已下架"); status.addItem("即将上映"); container.add(status); this.setVisible(true); this.setBounds(100,100,300,500); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) {
new javaPractice(); }}
  • 列表框
    public class javaPractice extends JFrame {
    public javaPractice(){
    Container container=this.getContentPane(); //String[] content={"1","2","3"}; Vector contents=new Vector(); //列表中需要加入内容 JList jList=new JList(contents); contents.add("zhangsan"); contents.add("lisi"); contents.add("wangwu"); container.add(jList); this.setVisible(true); this.setBounds(100,100,300,500); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) {
    new javaPractice(); }}
  • 应用场景
    • 选择地区,或者一些单个选项
    • 列表,展示信息,一般是动态扩容

3.7 文本框

  • 文本框
public class javaPractice extends JFrame {
public javaPractice(){
Container container=this.getContentPane(); JTextField textField=new JTextField("hello"); JTextField textField0=new JTextField("word",20); container.add(textField,BorderLayout.SOUTH); container.add(textField0,BorderLayout.NORTH); this.setVisible(true); this.setBounds(100,100,300,500); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) {
new javaPractice(); }}
  • 密码框
public class javaPractice extends JFrame {
public javaPractice(){
Container container=this.getContentPane(); //画板 JPasswordField passwordField=new JPasswordField(); passwordField.setEchoChar('*'); container.add(passwordField); this.setVisible(true); this.setBounds(100,100,300,500); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) {
new javaPractice(); }}
  • 文本域
public class javaPractice extends JFrame {
public javaPractice(){
Container container=this.getContentPane(); //文本域 JTextArea textArea=new JTextArea(20,50); textArea.setText("欢迎学习狂神说java"); //Scroll面板 JScrollPane scrollPane=new JScrollPane(textArea); container.add(scrollPane); this.setVisible(true); this.setBounds(100,100,300,500); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) {
new javaPractice(); }}

转载地址:http://stgji.baihongyu.com/

你可能感兴趣的文章
C++报错:C4700:使用了非初始化的局部变量
查看>>
【数据结构周周练】003顺序栈与链栈
查看>>
C++类、结构体、函数、变量等命名规则详解
查看>>
C++ goto语句详解
查看>>
【数据结构周周练】008 二叉树的链式创建及测试
查看>>
《软件体系结构》 第九章 软件体系结构评估
查看>>
《软件体系结构》 第十章 软件产品线体系结构
查看>>
《软件过程管理》 第六章 软件过程的项目管理
查看>>
《软件过程管理》 第九章 软件过程的评估和改进
查看>>
《软件过程管理》 第八章 软件过程集成管理
查看>>
分治法 动态规划法 贪心法 回溯法 小结
查看>>
《软件体系结构》 练习题
查看>>
《数据库系统概论》 第一章 绪论
查看>>
《数据库系统概论》 第二章 关系数据库
查看>>
《数据库系统概论》 第三章 关系数据库标准语言SQL
查看>>
SQL语句(二)查询语句
查看>>
SQL语句(六) 自主存取控制
查看>>
《计算机网络》第五章 运输层 ——TCP和UDP 可靠传输原理 TCP流量控制 拥塞控制 连接管理
查看>>
堆排序完整版,含注释
查看>>
二叉树深度优先遍历和广度优先遍历
查看>>