38Java GUI(图形用户接口)---全解<1>

1.Swing概述

  • Swing是一种轻量级组件,底层一AWT(抽象窗口工具包)为基础。在实际开发中,更多的是使用Swing进行图形用户界面开发。但是Swing并不是AWT的替代品,而是在原有的AWT的基础上进行了补充和改进。
  • Swing组件的所有类都继承自Container类,然后根据GUI开发的功能扩展了两个主要分支:容器分支(包括Window窗口和Panel面板)和组件分支。其中,容器分支就是为了实现图形用户界面窗口容器设计的,而组件分支则是为了实现向容器中填充数据、元素以及人际交互组件等功能。

2.Swing顶级容器

JFrame
  • 在Swing组件中,最常见的一个容器就是JFrame,它是一个独立存在的顶级容器(也叫窗口),不能放置在其他容器之中,JFrame支持通过窗口所有的基本功能,例如窗口最小化、设定窗口大小等。
package gui;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Example01 {
   
	private static void creatAndshowGUI() {
   
		//创建并设置JFrame容器窗口
		JFrame frame = new JFrame("JFrameTest");
		//设置关闭窗口时的默认操作
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//设置窗口尺寸
		frame.setSize(250, 150);
		//展示JFrame容器窗口
		frame.setVisible(true);

	}
	public static void main(String[] args) {
   
		//使用SwingUtilities工具类调用creatAndshowGUI()方法显示GUI程序
		SwingUtilities.invokeLater(Example01::creatAndshowGUI);
	}

}
JDialog
  • JDialog是Swing的另外一个顶级容器,通常用来表示对话框窗口。JDialog对话框可分为两种:模态对话框(用户需要等到处理完对话框后才能继续与其他窗口交互)和非模态对话框(允许用户在处理对话框的同时与其他窗口交互)
  • 对话框是模态或者是非模态,可以在创建JDialog对象时为构造方法传入参数来设置,也可以在创建JDialog对象后调用它的setModal()方法来进行设置,JDialog常用的构造方法如下
方法声明 功能描述
JDialog(Frame owner) 构造方法,用来创建一个非模态的对话框,owner为所有对话框所有者(顶级窗口JFrame)
JDialog(Frame owner,String title) 构造方法,创建一个具有指定标题的非模态对话框
JDialog(Frame owner,boolean modal) 创建一个指定模式的无标题对话框
  • 上表列举了JDialog三个常用的构造方法,在这三个构造方法中都需要接收一个Frame类型的对象,表示对话框所有者。第三个构造方法中,参数modal用来指定JDialog窗口是模态还是非模态,如果modal值设置为true,对话框就是模态对话框,反之则是非模态对话框,如果不设置modal的值,其默认值为false,也就是非模态对话框
package gui;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Example02 {
   
	private static void creatAndshowGUI() {
   
		//创建并设置JFrame容器窗口
		JFrame frame = new JFrame("JFrameTest");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(350, 150);
		frame.setVisible(true);
		//在JFrame窗口基础上创建并设置JDialog容器窗口
		JDialog dialog = new JDialog(frame,"JDialog对话框",true);
		dialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
		dialog.setSize(200, 100);
		dialog.setVisible(true);
	}
	
	public static void main(String[] args) {
   
		//使用SwingUtilities工具类调用creatAndshowGUI()方法显示GUI程序
				SwingUtilities.invokeLater(Example02::creatAndshowGUI);
	}

}

  • 虽然JFrame和JDialog都可创建顶级容器窗口,但JDialog创建的窗口并没有放大和缩小功能。另外,由于创建JDialog容器对象时,设置的模态参数modal为true,所以在操作时,必须先关闭JDialog对话框后才可以与JFrame窗口进行交互

3.布局管理器

Swing组件不能单独存在,必须放置于容器当中,而组件在容器中的位置和尺寸是由布局管理器来决定的。Swing工具在AWT的基础上提供了8种布局管理器,分别为BorderLayout(边界布局管理器)、BoxBagLayout(箱式时布局管理器)、CardLayout(卡片布局管理器)、FlowLayout(流式布局管理器)、GridBagLayout(网格包布局管理器)、GridLayout(网格布局管理器)、GroupLayout(分组布局管理器)、SpringLayout(弹性布局管理器)

BorderLayout
  • BorderLayout(边界布局管理器)是一种较为复杂的布局方式,它将容器化分为5个区域,分别是页头(PAGE_START)、页尾(PAGE_END)、行首(LINE_START)、行尾(LINE_END)、中部(CENTER),组件可以被放置在这5个区域中的任意一个位置
  • 当向BorderLayout布局管理器的容器中添加组件时,需要使用add(Component comp, Object constraints)方法,其中参数comp表示要添加的组件,constraints指定将组件添加到布局中的位置,它是一个Object类型,再传参时可以使用页头(PAGE_START)、页尾(PAGE_END)、行首(LINE_START)、行尾(LINE_END)、中部(CENTER)设置组件位置
package gui;

import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Example03 {
   
	private static void creatAndShowGUI() {
   
		//创建一个名为BorderLayout的顶级容器窗口
		JFrame f = new JFrame("BorderLayout");
		//设置窗口中的布局管理器为BorderLayout
		f.setLayout(new BorderLayout());
		f.setSize(300, 300);
		f.setLocation(300, 200);
		//下面的代码是创建5个按钮组件
		JButton but1 = new JButton("PAGE_START");
		JButton but2 = new JButton("PAGE_END");
		JButton but3 = new JButton("LINE_START");
		JButton but4 = new JButton("LINE_END");
		JButton but5 = new JButton("CENTER");
		//下面的代码是将创建好的按钮添加到窗体中,并设置按钮所在的区域
		f.add(but1,BorderLayout.PAGE_START);
		f.add(but2,BorderLayout.PAGE_END);
		f.add(but3
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
<?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <tomcat-users xmlns="http://tomcat.apache.org/xml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd" version="1.0"> <!-- By default, no user is included in the "manager-gui" role required to operate the "/manager/html" web application. If you wish to use this app, you must define such a user - the username and password are arbitrary. Built-in Tomcat manager roles: - manager-gui - allows access to the HTML GUI and the status pages - manager-script - allows access to the HTTP API and the status pages - manager-jmx - allows access to the JMX proxy and the status pages - manager-status - allows access to the status pages only The users below are wrapped in a comment and are therefore ignored. If you wish to configure one or more of these users for use with the manager web application, do not forget to remove the <!.. ..> that surrounds them. You will also need to set the passwords to something appropriate. --> <!-- <user username="admin" password="<must-be-changed>" roles="manager-gui"/> <user username="robot" password="<must-be-changed>" roles="manager-script"/> --> <!-- The sample user and role entries below are intended for use with the examples web application. They are wrapped in a comment and thus are ignored when reading this file. If you wish to configure these users for use with the examples web application, do not forget to remove the <!.. ..> that surrounds them. You will also need to set the passwords to something appropriate. --> <!-- <role rolename="tomcat"/> <role rolename="role1"/> <user username="tomcat" password="<must-be-changed>" roles="tomcat"/> <user username="both" password="<must-be-changed>" roles="tomcat,role1"/> <user username="role1" password="<must-be-changed>" roles="role1"/> --> </tomcat-users> 这个怎么改
07-23

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值