一.AWT组件及其属性
import java.awt.*
组件(component)是构成图形用户界面的基本组成和核心元素。Component是所有组件类的根类,实际使用的组件是Component的子类。
常用组件:
1.Window窗口类和Panel面板类:
窗口是顶层容器,窗口不能包含在其他容器中。Panel必须包含在其他容器中,窗口可以包含多个面板,一个面板可以包含另一个面板。
2.Frame框架,Dialog对话框:通常把Frame框架当做父类。一定要设置setVisible(true).
3.标签Label:标签只能用于显示信息,不能用于输入。
4.文本行TextField:用于输入文本,gettext()
5.按钮Button:需要触发事件监听器(actionListener等),可以创建一个带有标签的按钮。
二.布局管理器
Flowlayout:流式布局,可以通过更改框架大小而改变布局。
BorderLayout:边布局,“东,西,南,北,中”,中间组件的长度和宽度随容器大小而变化。
GridLayout:网格布局
复数计算器的实例
package tuxing;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Complex {
private double real;
private double image;
public Complex(String real, String image) {
try {
this.real = Double.parseDouble(real);
this.image = Double.parseDouble(image);
}catch(Exception e) {
System.out.println("输入错误");
}
}
public void Add(Complex c) {
this.real = this.real + c.getReal();
this.image = this.image + c.getImage();
}
public void Reduce(Complex c) {
this.real = this.real - c.getReal();
this.image = this.image - c.getImage();
}
public void Multiply(Complex c) {
double a, b;
a = this.real;
b = this.image;
this.real = a * (c.getReal()) - b * (c.getImage());
this.image = b * (c.getReal()) + a * (c.getImage());
}
public void Divide(Complex c) {
double a, b;
a = this.real;
b = this.image;
this.real = (a * (c.getReal()) + b * (c.getImage()))
/ ((c.getReal()) * (c.getReal()) + (c.getImage() * c.getImage()));
this.image = (b * (c.getReal()) - a * (c.getImage()))
/ ((c.getReal()) * (c.getReal()) + (c.getImage() * c.getImage()));
}
public String toString() {
if (image > 0)
return real + "+" + image + "i";
else if (image < 0) {
image = -image;
}
return real + "-" + image + "i";
}
public double getReal() {
return real;
}
public void setReal(double real) {
this.real = real;
}
public double getImage() {
return image;
}
public void setImage(double image) {
this.image = image;
}
public static void main(String[] args) {
new Complex("2", "2");
new Calculate();
}
}
public class Calculate extends JFrame implements ActionListener{
JTextField j1,j2,j3,j4,j5,j6,j7,j8;
private String czy1 = "+",czy2 = "+";
public JComboBox<String> comboBox1, comboBox2;
JButton b = new JButton("=");
public Calculate()
{
this.setTitle("复数表达式计算");
this.setLayout(new FlowLayout());
this.setBounds(200, 200,400, 400);
this.setBackground(Color.gray);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
init();
this.getContentPane().add(b);
b.addActionListener(this);
j7 = new JTextField(10);
j8 = new JTextField(10);
j7.setHorizontalAlignment(JTextField.CENTER);//设置文本的水平对齐方式
j8.setHorizontalAlignment(JTextField.CENTER);
j7.setEditable(false);
j8.setEditable(false);
this.getContentPane().add(j7);
this.getContentPane().add(new JLabel("+"));
this.getContentPane().add(j8);
this.getContentPane().add(new JLabel("i"));
this.setVisible(true);
}
public void init()
{
j1 = new JTextField(10);
j2 = new JTextField(10);
j1.setHorizontalAlignment(JTextField.CENTER);
j2.setHorizontalAlignment(JTextField.CENTER);
this.getContentPane().add(j1);
this.getContentPane().add(new JLabel("+"));
this.getContentPane().add(j2);
this.getContentPane().add(new JLabel("i"));
this.add(new JLabel(""));
JComboBox1();
j3 = new JTextField(10);
j4 = new JTextField(10);
j3.setHorizontalAlignment(JTextField.CENTER);
j4.setHorizontalAlignment(JTextField.CENTER);
this.getContentPane().add(j3);
this.getContentPane().add(new JLabel("+"));
this.getContentPane().add(j4);
this.getContentPane().add(new JLabel("i"));
this.add(new JLabel(""));
JComboBox2();
j5 = new JTextField(10);
j6 = new JTextField(10);
j5.setHorizontalAlignment(JTextField.CENTER);
j6.setHorizontalAlignment(JTextField.CENTER);
this.getContentPane().add(j5);
this.getContentPane().add(new JLabel("+"));
this.getContentPane().add(j6);
this.getContentPane().add(new JLabel("i"));
this.add(new JLabel(""));
}
public void JComboBox1() {
String j[] = { "+", "-", "*", "/" };
comboBox1 = new JComboBox<String>(j);
this.add(comboBox1);
this.setLocationRelativeTo(comboBox1);
comboBox1.addActionListener(this);
}
public void JComboBox2() {
String j[] = { "+", "-", "*", "/" };
comboBox2 = new JComboBox<String>(j);
this.add(comboBox2);
this.setLocationRelativeTo(comboBox2);
comboBox2.addActionListener(this);
}
public void actionPerformed(ActionEvent ev)
{
if(ev.getSource()== b)
{
count();
}
else if(ev.getSource()==comboBox1)
{
czy1 = (String) comboBox1.getSelectedItem();
}
else if(ev.getSource()==comboBox2)
{
czy2 = (String) comboBox1.getSelectedItem();
}
}
public void count() {
Complex c1 = new Complex(j1.getText(), j2.getText());
Complex c2 = new Complex(j3.getText(), j4.getText());
Complex c3 = new Complex(j5.getText(), j6.getText());
Complex cc;
System.out.println("开始计算");
try {
if (czy1.equals("+") == true) {
c1.Add(c2);
System.out.print("+");
}
if (czy1.equals("-") == true) {
c1.Reduce(c2);
System.out.print("-");
}
if (czy1.equals("*") == true) {
c1.Multiply(c2);
System.out.print("*");
}
if (czy1.equals("/") == true) {
c1.Divide(c2);
System.out.print("/");
}
// ?ж?????? 2
if (czy2.equals("+") == true) {
c1.Add(c3);
System.out.print("+");
}
if (czy2.equals("-") == true) {
c1.Reduce(c3);
System.out.print("-");
}
if (czy2.equals("*") == true) {
c1.Multiply(c3);
System.out.print("*");
}
if (czy2.equals("/") == true) {
c1.Divide(c3);
System.out.print("/");
}
cc = c1;
c1 = null;
j7.setText(String.valueOf(cc.getReal()));
j8.setText(String.valueOf(cc.getImage()));
}catch (Exception e) {
JOptionPane.showMessageDialog(this, "输入错误");
}
}
}
三.Swing组件及事件
Swing组件以“J”开头,JFrame框架作为主窗口,JFrame和JDialog是两个重型组件,因此不能将Swing组件添加到这两个组件中,应该加到内容窗格中(contentpane),
this.getcontentpane().add(component),其他组件省略
四.图形图像
通过绘图类Graphics对象调用绘图方法实现。
组件绘图方法:
public void paint(Graphics g)
public void repaint()
public void update(Graphics g)
paint()和update()方法由虚拟机自动执行,当更新组件时,自动执行update()方法。
画布:通常在画布上绘制图形。public Canvas()