1.窗口类的定义
用JFrame类或其子类可以实现窗口,它是一个等层容器。
import javax.swing.JFrame;
public class f {
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame w1=new JFrame("第一个窗口");
w1.setVisible(true);
w1.setSize(960, 540);
w1.setLocation(300, 250);
// w1.setBounds(300, 250, 960, 540);
w1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
2.常用组件
(1)标签 JLabel
(2)文本框 JTextField
(3)按钮 JButton
(4)文本区 JTextArea
(5)复选框 JCheckBox
import java.awt.FlowLayout;
import javax.swing.*;
public class f {
public static void main(String[] args) {
// TODO Auto-generated method stub
MyWindow4 mw = new MyWindow4();
}
}
class MyWindow4 extends JFrame {
JLabel label; // 定义
JTextField txt;
JButton button;
JTextArea area;
JCheckBox check1, check2, check3;
JRadioButton radio1, radio2, r1, r2, ra1, ra2;
JComboBox<String> c;
JPasswordField f;
public MyWindow4() {
init();
setTitle("常用组件");
setVisible(true);
setSize(500, 540);
setLocation(300, 250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void init() {
setLayout(new FlowLayout()); // 设置布局
label = new JLabel("今天"); // 创建标签
add(label); // 添加
txt = new JTextField(5);
add(txt);
button = new JButton("^");
add(button);
area = new JTextArea(5, 8); // 5行8列
add(area);
check1 = new JCheckBox("abc");
add(check1);
check2 = new JCheckBox("电脑");
add(check2);
check3 = new JCheckBox("桌面");
add(check3);
radio1 = new JRadioButton("听");
radio2 = new JRadioButton("不听");
r2 = new JRadioButton("不看");
r1 = new JRadioButton("看");
ra2 = new JRadioButton("不喝");
ra1 = new JRadioButton("喝");
add(radio1);
add(radio2);
add(r2);
add(r1);
add(ra2);
add(ra1);
ButtonGroup g = new ButtonGroup();
g.add(radio1);
g.add(radio2);
ButtonGroup g1 = new ButtonGroup();
g1.add(r2);
g1.add(r1);
ButtonGroup g2 = new ButtonGroup();
g2.add(ra2);
g2.add(ra1);
c = new JComboBox<String>();
c.addItem("软件");
c.addItem("系统");
c.addItem("程序");
add(c);
add(c);
add(c);
f = new JPasswordField(5);
add(f);
}
}
3.菜单
菜单条,菜单,菜单项。
菜单放在菜单条,菜单项放在菜单。
import java.awt.FlowLayout;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
public class f {
public static void main(String[] args) {
// TODO Auto-generated method stub
MyWindow3 mw = new MyWindow3();
}
}
class MyWindow3 extends JFrame {
JMenuBar menuBar; // 菜单条
JMenu menu;
JMenu menu1;
JMenuItem item; // 菜单项
JMenuItem item1;
JMenuItem item2;
JMenuItem item3;
public MyWindow3() {
init();
setTitle("带菜单的窗口");
setVisible(true);
setSize(500, 540);
setLocation(300, 250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
// 初始化窗口
public void init() {
menuBar = new JMenuBar();
menu = new JMenu("菜单");
menu1 = new JMenu("体育话题");
item = new JMenuItem("文学话题", new ImageIcon("feature.gif"));
item.setAccelerator(KeyStroke.getKeyStroke("A"));
item1 = new JMenuItem("烹饪话题", new ImageIcon(";2.gif"));
item1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK));
item2 = new JMenuItem("足球", new ImageIcon("exportrunnablejar_wiz.gif"));
item3 = new JMenuItem("篮球", new ImageIcon("alphab_sort_co.gif"));
menu.add(item); // 把菜单项加入菜单
menu.add(item1);
menu.add(menu1);
menu1.add(item2);
menu1.add(item3);
menuBar.add(menu); // 把菜单加入菜单条
setJMenuBar(menuBar); // 把菜单条放到窗口中
}
}
4.常用容器
(1)面板 JPanel
(2)滚动窗格 JScrollPane
(3)拆分窗格 JSplitPane
(4)分层窗格 JLayeredPane
(5)选项卡 JTabbedPane
import java.awt.FlowLayout;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
public class f {
public static void main(String[] args) {
// TODO Auto-generated method stub
MyWindow5 mwi = new MyWindow5();
}
}
class MyWindow5 extends JFrame {
// JTextField jfie;
// JCheckBox jche;
// JRadioButton jrad;
// JComboBox jcom;
// JPasswordField jpass;
JPanel panel; // 定义
JLabel jla;
JTextArea jarea;
JButton jbu, jbu1, jbu2;
JScrollPane scrool;
JSplitPane split;
JLayeredPane layer;
JTabbedPane jta;
public MyWindow5() {
init();
setTitle("常用的");
setVisible(true);
setSize(500, 540);
setLocation(300, 250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void init() {
// panel =new JPanel(); //创建
// jla=new JLabel("文本"); //创建
// panel.add(jla); //把组件放到面板中
// jbu=new JButton("对");
// add(panel); //把面板放到窗口中
// //滚动窗格
// jarea = new JTextArea(5, 8);
// scrool = new JScrollPane(jarea);
// add(scrool);
// //拆分窗格--水平:|
// split=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,new JButton("对"),new JButton("否")); //创建
// add(split); //添加
// //拆分窗格--垂直: -
// split=new JSplitPane(JSplitPane.VERTICAL_SPLIT,new JButton("是"),new JButton("不是"));
// add(split);
// //分层窗格
// layer=new JLayeredPane();
// jbu=new JButton("0");
// jbu1=new JButton("1");
// jbu2=new JButton("2");
// jbu.setBounds(300, 200, 100, 80);
// jbu1.setBounds(400, 190, 70, 110);
// jbu2.setBounds(250, 200, 200, 200);
// layer.add(jbu,JLayeredPane.MODAL_LAYER);
// layer.add(jbu1,JLayeredPane.PALETTE_LAYER);
// layer.add(jbu2,JLayeredPane.DEFAULT_LAYER);
// setLayeredPane(layer);
// 选项卡
jta = new JTabbedPane(JTabbedPane.LEFT);
jta.addTab("文件", new JButton("页面"));
jta.addTab("保存", new JButton("第二个页面"));
add(jta);
}
}
5.常用布局
容器可使用方法 setLayout(布局对象)来设置布局
布局方式:(1)流式布局 FlowLayout
(2)边界布局 BorderLayout
(3)卡片布局 GardLayout
(4)网格布局 GridLayout
(5)盒式布局 BoxLayout
import java.awt.FlowLayout;
import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
public class f {
public static void main(String[] args) {
// TODO Auto-generated method stub
Mywindows9 m = new Mywindows9();
}
}
class Mywindows9 extends JFrame {
public Mywindows9() {
init();
setTitle("布局");
setVisible(true);
setSize(500, 400);
setLocation(250, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void init() {
// 流式布局
// setLayout(new FlowLayout());
// add(new JLabel("这是第一个"));
// add(new JLabel("这是第2个"));
// for (int i = 0; i <= 49; i++) {
// add(new JLabel("这是标签"+ i));
// }
// 边界布局
// setLayout(new BorderLayout());
// add(new JButton("白"), BorderLayout.CENTER);
// add(new JButton("黑"), BorderLayout.WEST);
// add(new JButton("红"), BorderLayout.EAST);
// add(new JButton("蓝"), BorderLayout.SOUTH);
// add(new JButton("紫"), BorderLayout.NORTH);
// 卡片布局
// setLayout(new CardLayout());
// add(new JButton("大鬼"));
// add(new JButton("小鬼"));
// add(new JButton("J"));
// add(new JButton("Q"));
// 网格布局
// setLayout(new GridLayout(5,3));
add(new JButton("这是第一个"));
add(new JButton("这是第2个"));
add(new JButton("这是第3个"));
// for(int i=1;i<=2;i++) {
//
// for(int j=1;j<=3;j++) {
// add(new JButton("第"+i+"行第"+j+"列"));
// }
//
// }
// 盒式布局
setLayout(new FlowLayout());
Box box = Box.createHorizontalBox(); // 创建一个水平盒子
box.add(new JLabel("姓名"));
box.add(Box.createHorizontalStrut(5)); // 两个组件中间添加水平间距
add(box);
Box box1 = Box.createHorizontalBox();
box1.add(new JLabel("email"));
box1.add(Box.createHorizontalStrut(6));
add(box1);
Box box2 = Box.createVerticalBox(); // 创建一个垂直盒子
box2.add(box);
box2.add(Box.createVerticalStrut(8)); // 两个组件中间添加垂直间距
box2.add(box1);
add(box2);
}
}
6.ActionEvent事件
(1)确定事件源
(2)确定事件类型
(3)定义处理器类,创建对象
(4)事件源注册事件处理器对象
import java.awt.FlowLayout;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
public class f {
public static void main(String[] args) {
// TODO Auto-generated method stub
Mywindows m = new Mywindows();
}
}
class Mywindows extends JFrame {
JTextField text;
JButton but1, but2;
public Mywindows() {
init();
setLocation(250, 200);
setSize(400, 500);
setTitle("动作事件");
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void init() {
setLayout(new FlowLayout());
text = new JTextField(15);
but1 = new JButton("📃");
but2 = new JButton("👌");
add(text);
add(but1);
add(but2);
Lala l = new Lala();
but1.addActionListener(l); // 娉ㄥ唽
but2.addActionListener(new Clear());
}
class Lala implements ActionListener {
@Override
public void actionPerformed(java.awt.event.ActionEvent arg0) {
// TODO Auto-generated method stub
System.out.println("吃饭");
text.setText("吃饭");
}
}
class Clear implements ActionListener {
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
// TODO Auto-generated method stub
text.setText("什么");
}
}
}
7.ItemEvent事件
复选框、下拉列表都可触发ItemEvent事件。
import java.awt.FlowLayout;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import javax.swing.*;
public class f {
public static void main(String[] args) {
// TODO Auto-generated method stub
window m = new window();
}
}
class window extends JFrame {
JTextField fie1, fie2, fie3;
JComboBox<String > comBox;
JButton button;
public window() {
init();
setTitle("ItemEvent");
setLocation(400, 350);
setSize(550, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void init() {
setLayout(new FlowLayout());
fie1 = new JTextField(5);
fie2 = new JTextField(5);
fie3 = new JTextField(5);
comBox =new JComboBox<String> ();
comBox.addItem("+");
comBox.addItem("-");
comBox.addItem("*");
comBox.addItem("/");
add(fie1);
add(comBox);
add(fie2);
add(fie3);
comBox.addItemListener(new ComBoxPolice());
String []s= {"+","-","*","/"};
for(int i=0;i<s.length;i++) {
comBox.addItem(s[i]);
}
}
class ComBoxPolice implements ItemListener{
@Override
public void itemStateChanged(java.awt.event.ItemEvent e) {
// TODO Auto-generated method stub
int m=Integer.parseInt(fie1.getText());
int n=Integer.parseInt(fie2.getText());
String s=comBox.getSelectedItem().toString();
int result = 0;
switch (s) {
case "+":
fie3.setText(m + n + "");
break;
case "-":
fie3.setText(m - n + "");
break;
case "*":
fie3.setText(m * n + "");
break;
case "/":
fie3.setText(m / n + "");
break;
}
}
}
}
8.DocumentEvent事件
文本区所维护的文档能触发DocumentEvent事件。
import java.awt.FlowLayout;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import javax.swing.*;
import javax.swing.event.DocumentListener;
public class f {
public static void main(String[] args) {
// TODO Auto-generated method stub
mywindow m = new mywindow();
}
}
class mywindow extends JFrame {
JTextArea area1, area2;
public mywindow() {
init();
setTitle("DocumentEvent");
setLocation(300, 400);
setSize(400, 500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void init() {
setLayout(new FlowLayout());
area1 = new JTextArea(6, 30);
area2 = new JTextArea(6, 30);
add(area1);
add(area2);
area1.getDocument().addDocumentListener(new TextPolice());
}
class TextPolice implements DocumentListener {
@Override
public void changedUpdate(javax.swing.event.DocumentEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void insertUpdate(javax.swing.event.DocumentEvent arg0) {
// TODO Auto-generated method stub
area2.setText(area1.getText());
}
@Override
public void removeUpdate(javax.swing.event.DocumentEvent arg0) {
// TODO Auto-generated method stub
area2.setText(area1.getText());
}
}
}
9.MouseEvent事件
任何组件上都可以发生鼠标事件。
import java.awt.FlowLayout;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.*;
import javax.swing.event.DocumentListener;
public class f {
public static void main(String[] args) {
// TODO Auto-generated method stub
mywindow10 m = new mywindow10();
}
}
class mywindow10 extends JFrame {
JTextArea a;
JButton b;
JLabel la;
public mywindow10() {
init();
setTitle(";MouseEvent");
setLocation(400, 500);
setSize(400, 500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void init() {
setLayout(new FlowLayout());
a = new JTextArea(6,30);
add(new JScrollPane(a)); // 文本区放到滚动窗格
b = new JButton("是");
add(a);
add(new JScrollPane(a));
b.addMouseListener(new ButtonPolice());
add(b);
}
class ButtonPolice implements MouseListener {
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
a.append("\n单击");
a.setText(a.getText());
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
a.append("\nmouseEntered");
a.setText(a.getText());
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
a.append("\n进入");
a.setText(a.getText());
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
a.append("\n按下");
a.setText(a.getText());
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
a.append("\n释放");
a.setText(a.getText());
}
}
}
10.FocusEvent事件
组件可以触发焦点事件。
处理器类必须要实现FocusListener接口。
事件源.addFocusListener(处理器对象名);
11.KeyEvent事件
当一个组件处于激活状态时,敲击键盘上的一个键就导致了这个组件触发键盘事件。
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.*;
public class f {
public static void main(String[] args) {
// TODO Auto-generated method stub
Mywindow_1 m = new Mywindow_1();
}
}
class Mywindow_1 extends JFrame {
JTextArea a1, a2;
JButton b;
public Mywindow_1() {
init();
setTitle("KeyEvent");
setLocation(300, 400);
setSize(400, 500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void init() {
setLayout(new FlowLayout());
a1 = new JTextArea(6,30);
add(a1);
a2 = new JTextArea(6,30);
add(a2);
b = new JButton("对");
add(b);
TextPolice t = new TextPolice();
a1.addKeyListener(t); // 第一个文本区注册处理器
b.addKeyListener(t); // 注册同一个处理器对象
}
class TextPolice implements KeyListener {
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == a1) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
a2.setText(a1.getText());
}
} else if (e.getSource() == b) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
b.setLocation(b.getLocation().x - 50, b.getLocation().y);
}
else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
b.setLocation(b.getLocation().x + 50, b.getLocation().y);
} else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
b.setLocation(b.getLocation().x, b.getLocation().y - 5);
} else if (e.getKeyCode() == KeyEvent.VK_UP) {
b.setLocation(b.getLocation().x, b.getLocation().y + 5);
}
}
}
@Override
public void keyReleased(java.awt.event.KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(java.awt.event.KeyEvent e) {
// TODO Auto-generated method stub
}
}
}
12.WindowEvent事件
(1)适配器类
(2)匿名类
Lambda表达式
(3)窗口做监视器
Window子类(JFrame)创建的对象都可以发生WindowEvent事件。
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import javax.swing.*;
public class f {
public static void main(String[] args) {
// TODO Auto-generated method stub
MyWindow_2 m=new MyWindow_2();
}
}
class MyWindow_2 extends JFrame{
public MyWindow_2() {
init();
setTitle("windowEvent");
setLocation(300, 400);
setSize(400, 500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void init() {
addWindowListener(new windowPolice());
}
//适配器类作为处理器类
class windowPolice extends WindowAdapter{
@Override
public void windowOpened(java.awt.event.WindowEvent arg0) {
// TODO Auto-generated method stub
super.windowOpened(arg0);
System.out.println("打开");
}
}
//class windowPolice implements WindowListener {
//
// @Override
// public void windowActivated(java.awt.event.WindowEvent arg0) {
// // TODO Auto-generated method stub
// System.out.println("windowActivated");
// }
//
// @Override
// public void windowClosed(java.awt.event.WindowEvent arg0) {
// // TODO Auto-generated method stub
// System.out.println("windowClosed");
// }
//
// @Override
// public void windowClosing(java.awt.event.WindowEvent arg0) {
// // TODO Auto-generated method stub
// System.out.println("windowClosing");
// }
//
// @Override
// public void windowDeactivated(java.awt.event.WindowEvent arg0) {
// // TODO Auto-generated method stub
// System.out.println("windowDeactivated");
// }
//
// @Override
// public void windowDeiconified(java.awt.event.WindowEvent arg0) {
// // TODO Auto-generated method stub
// System.out.println("windowDeiconified");
// }
//
// @Override
// public void windowIconified(java.awt.event.WindowEvent arg0) {
// // TODO Auto-generated method stub
// System.out.println("windowIconified");
// }
//
// @Override
// public void windowOpened(java.awt.event.WindowEvent arg0) {
// // TODO Auto-generated method stub
// System.out.println("windowOpened");
// }
//
//}
}
13.处理器类的其他写法
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.util.Random;
import javax.swing.*;
public class f {
public static void main(String[] args) {
// TODO Auto-generated method stub
window_3 m = new window_3();
}
}
class window_3 extends JFrame implements ActionListener {
JButton b1, b2, b3;
JLabel lab;
JTextField fed;
Random r;
int a;
public window_3() {
init();
setTitle("处理器");
setLocation(300, 400);
setSize(400, 500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void init() {
setLayout(new BorderLayout());
b1 = new JButton("得到一个100以内的随机数");
b2 = new JButton("重置随机数");
b3 = new JButton("确定");
lab = new JLabel("请猜数");
fed = new JTextField(10);
r = new Random();
add(fed, BorderLayout.CENTER);
add(b1, BorderLayout.NORTH);
add(b3, BorderLayout.SOUTH);
add(lab, BorderLayout.EAST);
add(b2, BorderLayout.WEST);
// 匿名类作为处理器类
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
a = r.nextInt(100); // 得到[0,100)间的随机数
System.out.println(a);
}
});
// Lambda作为处理器类,只有一个方法时适用
b2.addActionListener((ActionEvent e) -> {
// TODO Auto-generated method stub
a = r.nextInt(100); // 得到[0,100)间的随机数
System.out.println(a);
});
// 窗口类作为处理器类
b3.addActionListener(this); // 当前类的对象
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
// 获得文本框中输入的内容,然后与随机数比较,判断大小,给出提示信息,信息显示在标签
int g=Integer.parseInt(fed.getText());
if(g<a) {
lab.setText("写小了");
}
else if(g>a) {
lab.setText("写大了");
}
else if(g==a) {
lab.setText("对");
}
}
}
14.MVC结构
MVC结构:模型-视图-控制器(Model-View-Controller)
是一种软件的设计思想。
视图:用于显示数据。
模型:用于存储数据。
控制器:用于处理用户的信息,通过视图修改、获取模型中的数据,当模型中的数据变化时,让视图更新显示。
例题:首先编写一个封装三角形的类(模型角色),然后编写一个窗口,要求窗口使用3个文本框和一个文本区为三角形对象中的数据提供视图,其中,3个文本框用来显示和更新三角形对象的三个边的长度;文本区对象用来显示三角形的面积。窗口中有—个监视器担任控制器角色,用户单击该按钮后,控制器用3个文本框中的数据更新三角形的3个边,然后让三角形返回面积,并将返回的面积显示在文本区中。
(1)定义一个名为Mywindow_mvc
的 Java 窗口应用程序。它包含了一些用户界面组件,如文本框、按钮和标签,并实现了计算三个数的面积的功能。
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.util.Random;
import javax.swing.*;
public class Mywindow_mvc extends JFrame {
JTextField fie1, fie2, fie3;
JButton b;
JLabel l;
JTextArea a;
JPanel pan;
T m1;
ButtonPolice control;
public Mywindow_mvc() {
init();
setTitle("mvc");
setVisible(true);
setSize(500, 540);
setLocation(300, 250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void init() {
setLayout(new BorderLayout());
pan = new JPanel();
b = new JButton("计算面积");
l = new JLabel("请输入三条边长");
fie1 = new JTextField(5);
fie2 = new JTextField(5);
fie3 = new JTextField(5);
a=new JTextArea();
pan.add(l);
add(a, BorderLayout.CENTER);
pan.add(fie2);
pan.add(fie1);
pan.add(fie3);
pan.add(b);
add(pan, BorderLayout.NORTH);
m1=new T();
control=new ButtonPolice();
control.setM(this);
b.addActionListener(control);
}
}
(2)定义了一个名为T
的类,这个类代表一个具有三个边(a
、b
和c
)的对象,并且提供了一些方法来设置和获取这些边的值,以及计算并返回这个对象的面积。
package test;
public class T {
double a, b, c;
boolean isT;
public double getA() {
return a;
}
public void setA(double a) {
this.a = a;
if(a+b>c &&b+c>a&& a+c>b)
isT=true;
else
isT=false;
}
public double getB() {
return b;
}
public void setB(double b) {
this.b = b;
if(a+b>c &&b+c>a&& a+c>b)
isT=true;
else
isT=false;
}
public double getC() {
return c;
}
public void setC(double c) {
this.c = c;
if(a+b>c &&b+c>a&& a+c>b)
isT=true;
else
isT=false;
}
public double getArea() {
if(isT) {
double p=(a+b+c)/2;
return Math.sqrt(p*(p-a)*(p-b)*(p-c));
}
return 0;
}
}
(3)定义一个名为ButtonPolice
的类,它实现了ActionListener
接口。这个类的主要作用是在按钮被点击时执行一些操作。
package test;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusListener;
import javax.swing.*;
public class ButtonPolice implements ActionListener{
Mywindow_mvc m;
public void setM(Mywindow_mvc m) {
this.m = m;
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//获得文本框中输入的三边信息,计算面积,再把面积结果显示到文本区
double f=Double.parseDouble(m.fie1.getText());
double g=Double.parseDouble(m.fie2.getText());
double h=Double.parseDouble(m.fie3.getText());
m.m1.setA(f);
m.m1.setB(g);
m.m1.setC(h);
double s=m.m1.getArea();
m.a.setText("三角形面积为"+s);
}
}
(4)创建了一个名为Mywindow_mvc
的对象。
public class lian {
public static void main(String[] args) {
// TODO Auto-generated method stub
Mywindow_mvc m=new Mywindow_mvc();
}
}