JAVAGUI
1、简介
Gui的核心技术: Swing、 AWT
1.因为界面不美观
2.需要jre环境
学习原因
1.可以写出一些小工具
2.借此了解MVC架构,了解监听
2、AWT
2.1AWT介绍
1.包含了很多类和接口
2.元素:窗口、按钮、文本框
3.java.awt
2.2、组件和容器
1、Frame
package GUI;
import java.awt.*;
public class TestFrame01 {
public static void main(String[] args)
{
Frame f = new Frame("朱良德的第一个窗口");
//设置可见性
f.setVisible(true);
//设置窗口大小
f.setSize(400, 500);
//设置背景颜色
f.setBackground(Color.blue);
//设置窗口弹出初始位置
f.setLocation(400, 400);
//设置窗口大小固定
f.setResizable(false);
}
}
问题:发现窗口无法用’X‘关闭
回顾封装,编写Frame
package GUI;
import java.awt.*;
public class TestFrame02 {
public static void main(String[] args)
{
MyFrame mf1 = new MyFrame(100, 100, 400, 400, Color.GRAY);
MyFrame mf2 = new MyFrame(500, 100, 400, 400, Color.blue);
MyFrame mf3 = new MyFrame(100, 500, 400, 400, Color.cyan);
MyFrame mf4 = new MyFrame(500, 500,400, 400, Color.lightGray);
}
}
class MyFrame extends Frame
{
static int id = 0;
public MyFrame(int x, int y, int w, int h, Color c)
{
super("MyFrame"+(++id));
setBounds(x, y, w, h);
setBackground(c);
setVisible(true);
}
}
2、面板Panel
package GUI;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestPanel {
public static void main(String[] args)
{
Frame f1 = new Frame("尝试放入面板");
Panel p1 = new Panel();
//设置布局
f1.setLayout(null);
//窗口设置
f1.setBounds(300, 300, 1000, 1000);
f1.setBackground(Color.black);
f1.setVisible(true);
//面板设置
p1.setBounds(300, 300, 400, 400);
p1.setBackground(Color.green);
//嵌入面板
f1.add(p1);
//添加窗口关闭事件监听
f1.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
}
由于添加了事件监听,该窗口可以直接关闭,不需要使用编译器停止进程
2.3布局管理器
1、流式布局
package GUI;
import java.awt.*;
public class TestFlowLayout {
public static void main(String[] args)
{
Frame f1 = new Frame();
//创建组件对象——按钮
Button B1 = new Button("button1");
Button B2 = new Button("button2");
Button B3 = new Button("button3");
//设置布局——流式布局
f1.setLayout(new FlowLayout());//默认即居中等价于f1.setLayout(new FlowLayout(FlowLayout.center));
//f1.setLayout(new FlowLayout(FlowLayout.LEFT));//左对齐
//f1.setLayout(new FlowLayout(FlowLayout.RIGHT));//右对齐
f1.setBounds(100,100,400,400);
//添入组件
f1.add(B1);
f1.add(B2);
f1.add(B3);
f1.setVisible(true);
}
}
2、东西南北中
package GUI;
import java.awt.*;
public class TestBorderLayout {
public static void main(String[] args)
{
Frame f1 = new Frame("BorderLayout");
Button west = new Button("WEST");
Button east = new Button("EAST");
Button south = new Button("SOUTH");
Button north = new Button("NORST");
Button center = new Button("CENTER");
f1.setBounds(100,100,400,400);
f1.add(west, BorderLayout.WEST);
f1.add(east, BorderLayout.EAST);
f1.add(south, BorderLayout.SOUTH);
f1.add(north, BorderLayout.NORTH);
f1.add(center, BorderLayout.CENTER);
f1.setVisible(true);
}
}
3、表格布局
package GUI;
import com.sun.deploy.util.Base64Wrapper;
import java.awt.*;
public class TestGridLayout {
public static void main(String[] args) {
Frame f1 = new Frame("BorderLayout");
f1.setBounds(100,100,400,400);
Button B1 = new Button("B1");
Button B2 = new Button("B2");
Button B3 = new Button("B3");
Button B4 = new Button("B4");
Button B5 = new Button("B5");
Button B6 = new Button("B6");
f1.setLayout(new GridLayout(3,2));//表格布局,三行两列
f1.add(B1);
f1.add(B2);
f1.add(B3);
f1.add(B4);
f1.add(B5);
f1.add(B6);
f1.setVisible(true);
}
}
2.4事件监听
事件监听:通俗的讲就是当某个事情发生的时候,干什么。
package GUI;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestActionEvent {
public static void main(String[] args) {
Frame frame = new Frame();
Button button = new Button("Click");
//创建监听对象
MyActionListener myActionListener = new MyActionListener();
button.addActionListener(myActionListener);
frame.pack();
frame.add(button);
frame.setVisible(true);
WindowClose(frame);//关闭窗口
}
//关闭窗口的事件
private static void WindowClose (Frame frame)
{
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
//事件监听
class MyActionListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e) {
System.out.print("按我按我快按我\n");
}
}
2.5、输入框TextField
package GUI;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestText {
public static void main(String[] args) {
//启动
MyFrame2 myFrame2 = new MyFrame2();
WindowClose(myFrame2);
}
private static void WindowClose (Frame frame)
{
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyFrame2 extends Frame
{
public MyFrame2()
{
TextField textField = new TextField();
//监听这个文本框输入的文字
MyActionListener2 myActionListener = new MyActionListener2();
//按下回车就会触发这个输入框的事件
textField.addActionListener(myActionListener);
//设置替换编码
textField.setEchoChar('*');
add(textField);
pack();
setVisible(true);
}
}
class MyActionListener2 implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e) {
//e.getSource获得一些资源,返回一个对象
TextField text = (TextField) e.getSource();
System.out.println(text.getText());//获取文本框中输入的信息
}
}
2.6、简易计算机
package GUI;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestCalculator {
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.FrameLoad();
WindowClose(calculator);
}
private static void WindowClose (Frame frame)
{
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class Calculator extends Frame
{
TextField n1, n2, n3;
public void FrameLoad ()
{
n1 = new TextField(10);
n2 = new TextField(10);
n3 = new TextField(10);
Label label = new Label("+");
Button button = new Button("=");
button.addActionListener(new MyListener());
setLayout(new FlowLayout());
add(n1);
add(label);
add(n2);
add(button);
add(n3);
pack();
setVisible(true);
}
private class MyListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e) {
//获取加数和被加数
int i = Integer.parseInt(n1.getText());
int j = Integer.parseInt(n2.getText());
//将i、j相加发送到第三个文本框n3
n3.setText("" + (i+j));
//将前两个文本框置空
n1.setText("");
n2.setText("");
}
}
}
2.7、画笔
package GUI;
import java.awt.*;
public class TestPaint {
public static void main(String[] args) {
new MyPaint().FrameLoad();
}
}
class MyPaint extends Frame
{
public void FrameLoad()
{
setBounds(100,100,500,400);
setVisible(true);
}
@Override
public void paint(Graphics g)
{
g.setColor(Color.BLUE);
g.drawOval(100,100,100,100);
}
}
2.8、鼠标监听
package GUI;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
public class TestMouseListener {
public static void main(String[] args) {
new MyFrame5("画图");
}
}
class MyFrame5 extends Frame
{
ArrayList points;
public MyFrame5(String title)
{
super(title);
points = new ArrayList<>();
setBounds(100,100,600,500);
addMouseListener(new MyMouseListener());
setVisible(true);
}
public void addPoint (Point point)
{
points.add(point);
}
private class MyMouseListener extends MouseAdapter
{
@Override
public void mouseClicked(MouseEvent e) {
MyFrame5 myFrame = (MyFrame5) e.getSource();
//获取当前鼠标的位置,存为一个点
myFrame.addPoint(new Point(e.getX(), e.getY()));
//每次点击鼠标都要重画一遍
myFrame.repaint();
}
}
@Override
public void paint(Graphics g) {
Iterator iterator = points.iterator();
while (iterator.hasNext())
{
Point point = (Point) iterator.next();
g.setColor(Color.black);
g.fillOval(point.x, point.y,10,10 );
}
}
}
2.9、窗口监听
package GUI;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestWindowListener {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame
{
public WindowFrame()
{
setBounds(100,100,400,400);
setVisible(true);
addWindowListener(
new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
@Override
public void windowActivated(WindowEvent e) {
WindowFrame f = (WindowFrame) e.getSource();
f.setTitle("我被激活了");
}
}
);
}
}
切掉窗口再次点击之后:
出现“我被激活了”
2.10、键盘监听
package GUI;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class TestKeyListener {
public static void main(String[] args) {
new KeyFrame();
}
}
class KeyFrame extends Frame
{
public KeyFrame()
{
setBounds(1,2,300,400);
setVisible(true);
this.addKeyListener(new KeyAdapter() {
//键盘按下
@Override
public void keyPressed(KeyEvent e) {
//获得按下的是哪个键,当前的码
int keyCode = e.getKeyCode();
System.out.println(keyCode);
if(keyCode == KeyEvent.VK_UP)
{
System.out.println("你按下了'上'键");
}
}
});
}
}
3、Swing
3.1、窗口、面板
package GUI;
import javax.swing.*;
public class TestJFrame {
//init(); 初始化
public void init(){
JFrame frame = new JFrame("这是一个JFrame窗口");
frame.setVisible(true);
frame.setBounds(100,100,200,200);
//设置文字
JLabel label = new JLabel("这是JLabel文本框");
frame.add(label);
//关闭窗口事件
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
//建立一个窗口
new TestJFrame().init();
}
}
3.2、弹窗
package GUI;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestDialog extends JFrame
{
public TestDialog()
{
setBounds(100,100,600,500);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//创建一个容器,往JFrame里放东西
Container container = getContentPane();
//设置绝对布局
container.setLayout(null);
//按钮
JButton button = new JButton("点击按钮");
button.setBounds(10,10,100,100);
button.setHorizontalAlignment(SwingConstants.CENTER);
button.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new MyDialog();
}
}
);
container.add(button);
}
public static void main(String[] args) {
new TestDialog();
}
}
class MyDialog extends JDialog
{
public MyDialog()
{
setBounds(100,100,400,300);
setVisible(true);
}
}
3.3、标签
package GUI;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class TestImageIcon extends JFrame{
public TestImageIcon()
{
JLabel jLabel = new JLabel("TestImageIcon");
URL url = TestImageIcon.class.getResource("201.jpg");
ImageIcon imageIcon = new ImageIcon(url);
jLabel.setIcon(imageIcon);
Container container = getContentPane();
container.add(jLabel);
setBounds(100,100,500,400);
setVisible(true);
}
public static void main(String[] args) {
new TestImageIcon();
}
}
3.4、面板
package GUI;
import javax.swing.*;
import java.awt.*;
public class TestJScroll extends JFrame {
public TestJScroll()
{
Container container = this.getContentPane();
JTextArea textArea = new JTextArea(10,50);
textArea.setText("这是一个文本域");
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
setVisible(true);
setBounds(10,10,350,300);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJScroll();
}
}
3.5、按钮
1、单选框
package GUI;
import javax.swing.*;
import java.awt.*;
public class TestJButton01 extends JFrame {
public TestJButton01()
{
Container container = getContentPane();
//创建多个单选框
JRadioButton jRadioButton1 = new JRadioButton("左");
JRadioButton jRadioButton2 = new JRadioButton("中");
JRadioButton jRadioButton3 = new JRadioButton("右");
//由于单选框只能多个选一个,所以需要创建按钮组
ButtonGroup group = new ButtonGroup();
group.add(jRadioButton1);
group.add(jRadioButton2);
group.add(jRadioButton3);
container.add(jRadioButton1,BorderLayout.WEST);
container.add(jRadioButton2,BorderLayout.CENTER);
container.add(jRadioButton3,BorderLayout.EAST);
setVisible(true);
setSize(300,400);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJButton01();
}
}
2、多选框
package GUI;
import javax.swing.*;
import java.awt.*;
public class TestJButton02 extends JFrame {
public TestJButton02()
{
Container container = getContentPane();
//创建多个多选框
JCheckBox jCheckBox1 = new JCheckBox("自动登录");
JCheckBox jCheckBox2 = new JCheckBox("记住密码");
container.add(jCheckBox2,BorderLayout.EAST);
container.add(jCheckBox1,BorderLayout.WEST);
setVisible(true);
setSize(300,400);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJButton02();
}
}