1.用AWT和布局管理器设计如图所示调色板界面,点击加减按钮能改变颜色值中各色值的分量,下面面板的颜色同步改变。
package com.yjq.lesson06;
import java.awt.*;
import java.awt.event.*;
public class Test_3 extends Frame {
//三个标签
Label l1,l2,l3;
//六个按钮(三个➕,三个➖)
Button b1,b2,b3,b4,b5,b6;
//三个文本框
TextField tf1,tf2,tf3;
int count1,count2,count3;
Color c;
//实现接口中的方法actionPerformed和textValueChanged
private class changeText implements ActionListener, TextListener
{
public void actionPerformed(ActionEvent e){
if(e.getSource()==b1){tf1.setText((count1++)+"");}
if(e.getSource()==b2){tf2.setText((count2++)+"");}
if(e.getSource()==b3){tf3.setText((count3++)+"");}
if(e.getSource()==b4){tf1.setText((count1--)+"");}
if(e.getSource()==b5){tf2.setText((count2--)+"");}
if(e.getSource()==b6){tf3.setText((count3--)+"");}
int c1=Integer.parseInt(tf1.getText());
int c2=Integer.parseInt(tf2.getText());
int c3=Integer.parseInt(tf3.getText());
if(c1>=255) {c1=0;count1=0;}
if(c2>=255) {c2=0;count2=0;}
if(c3>=255) {c3=0;count3=0;}
c=new Color(c1,c2,c3);
p1.setBackground(c);
}
public void textValueChanged(TextEvent e) {
c=new Color(Integer.parseInt(tf1.getText()),Integer.parseInt(tf2.getText()),Integer.parseInt(tf3.getText()));
p1.setBackground(c);
}
}
//两个面板
Panel p=new Panel();
Panel p1=new Panel();
//设置布局
GridBagLayout gbl=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
//构造器
public Test_3()
{
setSize(500, 400);
p.setLayout(gbl);
gbc.fill=GridBagConstraints.BOTH;
this.setLabel();
this.setButton_add();
this.setTxetField();
this.setButton_sub();
this.setPanel();
add(p);
setVisible(true);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
private void setPanel() {
gbc.weightx=10;
gbc.weighty=10;
gbc.gridx=0;
gbc.gridy=4;
gbc.gridwidth=4;
gbc.insets=new Insets(10,10,10,10);
p1.setBackground(new Color(220,220,220));
gbl.setConstraints(p1, gbc);
p.add(p1);
}
private void setButton_sub() {
b4=new Button("-");
b5=new Button("-");
b6=new Button("-");
b4.setFont(new Font("宋体",Font.PLAIN,20));
b5.setFont(new Font("宋体",Font.PLAIN,20));
b6.setFont(new Font("宋体",Font.PLAIN,20));
gbc.gridx=3;
gbc.gridy=0;
gbl.setConstraints(b4, gbc);
gbc.gridy=1;
gbl.setConstraints(b5, gbc);
gbc.gridy=2;
gbl.setConstraints(b6, gbc);
p.add(b4);
p.add(b5);
p.add(b6);
b4.addActionListener(new changeText());
b5.addActionListener(new changeText());
b6.addActionListener(new changeText());
}
private void setTxetField(){
tf1=new TextField(5);
tf2=new TextField(5);
tf3=new TextField(5);
tf1.setText("220");
tf2.setText("220");
tf3.setText("220");
count1=Integer.parseInt(tf1.getText());
count2=Integer.parseInt(tf1.getText());
count3=Integer.parseInt(tf1.getText());
gbc.weightx=1;
gbc.weighty=1;
gbc.gridx=2;
gbc.gridy=0;
gbl.setConstraints(tf1, gbc);
gbc.gridy=1;
gbl.setConstraints(tf2, gbc);
gbc.gridy=2;
gbl.setConstraints(tf3, gbc);
p.add(tf1);
p.add(tf2);
p.add(tf3);
tf1.addTextListener(new changeText());
tf2.addTextListener(new changeText());
tf3.addTextListener(new changeText());
this.setKeyPress(tf1);
this.setKeyPress(tf2);
this.setKeyPress(tf3);
}
private void setKeyPress(TextField tf)
{
//具体看addKeyListener的使用方法和说明
tf.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e)
{
char ch = e.getKeyChar();
if(!(ch>='0'&&ch<='9'))
{
e.consume();
}
}
});
}
private void setButton_add() {
b1=new Button("+");
b2=new Button("+");
b3=new Button("+");
b1.setFont(new Font("宋体",Font.PLAIN,20));
b2.setFont(new Font("宋体",Font.PLAIN,20));
b3.setFont(new Font("宋体",Font.PLAIN,20));
gbc.insets=new Insets(10,80,10,10);//组件间间距
gbc.gridx=1;
gbc.gridy=0;
gbc.weightx=1;
gbc.weighty=1;
gbl.setConstraints(b1, gbc);
gbc.gridy=1;
gbl.setConstraints(b2, gbc);
gbc.gridy=2;
gbl.setConstraints(b3, gbc);
p.add(b1);
p.add(b2);
p.add(b3);
b1.addActionListener(new changeText());
b2.addActionListener(new changeText());
b3.addActionListener(new changeText());
}
private void setLabel()
{
l1=new Label("红色", Label.CENTER);
l2=new Label("绿色",Label.CENTER);
l3=new Label("蓝色",Label.CENTER);
l1.setBackground(Color.RED);
l2.setBackground(Color.GREEN);
l3.setBackground(Color.BLUE);
gbc.gridx=0;
gbc.gridy=0;
gbc.weightx=1;
gbc.weighty=1;
gbc.insets=new Insets(10,10,10,10);
gbl.setConstraints(l1, gbc);
gbc.gridy=1;
gbl.setConstraints(l2, gbc);
gbc.gridy=2;
gbl.setConstraints(l3, gbc);
p.add(l1);
p.add(l2);
p.add(l3);
}
public static void main(String[] args) {
{
new Test_3();
}
}
}
//GridBagConstraints.insets: 此字段指定组件的外部填充,即组件与其显示区域边缘之间间距的最小量。
GridBagLayout参数
一个组件在父容器中的位置,由其设置的GridBagLayout的参数决定,而GridBagLayout参数封装在GridBagConstraints类中,其主要成员变量如下:
与组件所在方格相关的参数
如下图所示,父容器被分割成3*3的表格,各行各列占比如图中小数所示,然后在该容器中分别放了方格1,方格2,方格3,以方格3为例,其gridx=0,gridy=2,gridwidth=2,gridheight=1,weightx=0.2,weighty=0.5。
示意图
gridx = 2; // X2
gridy = 0; // Y0
gridwidth = 1; // 横占一个单元格
gridheight = 1; // 列占一个单元格
weightx = 0.0; // 当窗口放大时,长度不变
weighty = 0.0; // 当窗口放大时,高度不变
anchor = GridBagConstraints.NORTH; // 当组件没有空间大时,使组件处在北部
fill = GridBagConstraints.BOTH; // 当格子有剩余空间时,填充空间
insert = new Insets(0, 0, 0, 0); // 组件彼此的间距
ipadx = 0; // 组件内部填充空间,即给组件的最小宽度添加多大的空间
ipady = 0; // 组件内部填充空间,即给组件的最小高度添加多大的空间
new GridBagConstraints(gridx, gridy, gridwidth, gridheight, weightx, weighty, anchor, fill, insert, ipadx, ipady);
API使用(具体看API文档,实现编写过程中IDEA自动提示重写接口中方法)
addKeyListener
public void addKeyListener(KeyListener l)
添加指定的按键侦听器,以接收发自此组件的按键事件。如果 l 为 null,则不会抛出异常并且不执行动作。
java.awt.event
接口 KeyListener
用于接收键盘事件(击键)的侦听器接口。旨在处理键盘事件的类要么实现此接口(及其包含的所有方法),要么扩展抽象 KeyAdapter 类(仅重写有用的方法)。
然后使用组件的 addKeyListener 方法将从该类所创建的侦听器对象向该组件注册。按下、释放或键入键时生成键盘事件。然后调用侦听器对象中的相关方法并将该 KeyEvent 传递给它。
方法摘要
void keyPressed(KeyEvent e)
按下某个键时调用此方法。
void keyReleased(KeyEvent e)
释放某个键时调用此方法。
void keyTyped(KeyEvent e)
键入某个键时调用此方法。
2.利用Swing包创建一个窗口,窗口位置为(220,160)、大小为320×240,并在窗口(20,80)、(120,80)、(220,80)处各设置一个按钮,按钮大小为80 X 40。点击左按钮将窗口背景的红色分量增加10,点击中间按钮将窗口背景的绿色分量增加10,点击右按钮将窗口背景的蓝色分量增加10,上述三种分量大于255时变成0,下面面板的颜色同步改变,点击窗口关闭按钮时退出程序运行。
package com.yjq.lesson06;
//import java.awt.Color;
//import java.awt.event.ActionEvent;
//import java.awt.event.ActionListener;
//import java.awt.event.WindowAdapter;
//import java.awt.event.WindowEvent;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Test_4 extends JFrame {
//三个按钮
JButton jb1,jb2,jb3;
//三原色初始值
int r=100,g=100,b=255;
JPanel jp=new JPanel();
Test_4()
{
jp.setBackground(new Color(r,g,b));
jp.setLayout(null);
this.setSize(320, 240);
this.add(jp);
this.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e)
{
System.exit(0);
}
});
//对三个按钮的设置
jb1=new JButton("红色");
jb2=new JButton("绿色");
jb3=new JButton("蓝色");
jb1.setBackground(Color.RED);
jb2.setBackground(Color.GREEN);
jb3.setBackground(Color.BLUE);
jb1.setBounds(20,80,80,40);
jb2.setBounds(120,80,80,40);
jb3.setBounds(220,80,80,40);
jb1.addActionListener(new changeColor());
jb2.addActionListener(new changeColor());
jb3.addActionListener(new changeColor());
jp.add(jb1);
jp.add(jb2);
jp.add(jb3);
this.setVisible(true);
}
private class changeColor implements ActionListener
{
public void actionPerformed(ActionEvent e) {
if(e.getSource()==jb1)
{
r+=10;
if(r>255)r=0;
jp.setBackground(new Color(r,g,b));
}
if(e.getSource()==jb2)
{
g+=10;
if(g>255)
g=0;
jp.setBackground(new Color(r,g,b));
}
if(e.getSource()==jb3)
{
b+=10;
if(b>255)
b=0;
jp.setBackground(new Color(r,g,b));
}
}
}
public static void main(String[] args) {
new Test_4();
}
}
3.设计出如图所示Windows操作系统附带的小应用-计算器样式的简单计算器
1)只需要实现简单的加减乘除功能
2)菜单内容和样式如同系统自带计算器,但功能只需实现退出系统功能
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.event.*;
public class calculateTest {
private JFrame frame;
private JButton button_point,button8,button7,button9,button_div,button4,button5,button6,button_mul,button1,button2,button3,button0,button_equ,button_sub,button_add;
private JTextField textField,textField_1;
StringBuffer str=new StringBuffer();
StringBuffer str1=new StringBuffer();
private int sum;
private int count=0;
private double sum1;
private class change implements ActionListener{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button0)
{str.append("0");str1.append("0");textField.setText(str1.toString());}
if(e.getSource()==button1)
{str.append("1");str1.append("1");textField.setText(str1.toString());}
if(e.getSource()==button2)
{str.append("2");str1.append("2");textField.setText(str1.toString());}
if(e.getSource()==button3)
{str.append("3");str1.append("3");textField.setText(str1.toString());}
if(e.getSource()==button4)
{str.append("4");str1.append("4");textField.setText(str1.toString());}
if(e.getSource()==button5)
{str.append("5");str1.append("5");textField.setText(str1.toString());}
if(e.getSource()==button6)
{str.append("6");str1.append("6");textField.setText(str1.toString());}
if(e.getSource()==button7)
{str.append("7");str1.append("7");textField.setText(str1.toString());}
if(e.getSource()==button8)
{str.append("8");str1.append("8");textField.setText(str1.toString());}
if(e.getSource()==button9)
{str.append("9");str1.append("9");textField.setText(str1.toString());}
if(e.getSource()==button_point)
{str.append(".");str1.append(".");textField.setText(str1.toString());}
if(e.getSource()==button_equ)
{
String s=str.toString();
count=0;
if(s.contains("+"))
{
String[] arr=s.split("\\+");
sum=Integer.parseInt(arr[0])+Integer.parseInt(arr[1]);
textField_1.setText(sum+"");
str.delete(0, str.length());
str1.delete(0, str1.length());
}
else if(s.contains("-"))
{
String[] arr=s.split("\\-");
sum=Integer.parseInt(arr[0])-Integer.parseInt(arr[1]);
textField_1.setText(sum+"");
str.delete(0, str.length());
str1.delete(0, str1.length());
}
else if(s.contains("*"))
{
String[] arr=s.split("\\*");
sum1=Double.parseDouble(arr[0])*Double.parseDouble(arr[1]);
textField_1.setText(sum1+"");
str.delete(0, str.length());
str1.delete(0, str1.length());
}
else if(s.contains("÷"))
{
String[] arr=s.split("÷");
if(arr[1].equals("0"))
{ textField_1.setText("除数不能为0");str.delete(0, str.length());str1.delete(0, str1.length());}
else
{sum1=Double.parseDouble(arr[0])/Double.parseDouble(arr[1]);textField_1.setText(sum1+"");str.delete(0, str.length());str1.delete(0, str1.length());}
}
else
{
textField_1.setText(str.toString());
str.delete(0, str.length());
str1.delete(0, str1.length());
}
sum=0;
}
}
}
public static void main(String[] args) {
calculateTest window = new calculateTest();
window.frame.setVisible(true);
}
public calculateTest()
{
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(323, 405);
frame.setResizable(false);
frame.getContentPane().setLayout(null);
JMenuBar mb = new JMenuBar();
mb.setBounds(0, 0, 299, 23);
frame.getContentPane().add(mb);
JMenu jm1 = new JMenu("查看(V)");
mb.add(jm1);
JRadioButtonMenuItem jrb1 = new JRadioButtonMenuItem("标准型(T)");
jm1.add(jrb1);
JRadioButtonMenuItem jrb2 = new JRadioButtonMenuItem("科学型(S)");
jm1.add(jrb2);
JRadioButtonMenuItem jrb3 = new JRadioButtonMenuItem("程序员(P)");
jm1.add(jrb3);
JRadioButtonMenuItem jrb4 = new JRadioButtonMenuItem("统计信息(A)");
jm1.add(jrb4);
jm1.addSeparator();
JRadioButtonMenuItem jrb5 = new JRadioButtonMenuItem("历史记录(Y)");
jm1.add(jrb5);
JRadioButtonMenuItem jrb6 = new JRadioButtonMenuItem("数字分组(I)");
jm1.add(jrb6);
jm1.addSeparator();
JRadioButtonMenuItem jrb7 = new JRadioButtonMenuItem("基本(B)");
jm1.add(jrb7);
JRadioButtonMenuItem jrb8 = new JRadioButtonMenuItem("单位转换(U)");
jm1.add(jrb8);
JRadioButtonMenuItem jrb9 = new JRadioButtonMenuItem("日起计算(D)");
jm1.add(jrb9);
JMenu jm4 = new JMenu("工作表(W)");
jm1.add(jm4);
JRadioButtonMenuItem jrb10 = new JRadioButtonMenuItem("抵押(M)");
jm4.add(jrb10);
JRadioButtonMenuItem jrb11 = new JRadioButtonMenuItem("汽车租赁(V)");
jm4.add(jrb11);
JRadioButtonMenuItem jrb12 = new JRadioButtonMenuItem("油耗(mpg)(F)");
jm4.add(jrb12);
JRadioButtonMenuItem jrb13 = new JRadioButtonMenuItem("油耗(l/100km)(U)");
jm4.add(jrb13);
JMenu jm2 = new JMenu("编辑(E)");
mb.add(jm2);
JMenuItem jmi1 = new JMenuItem("复制(C)");
jm2.add(jmi1);
JMenuItem jmi2 = new JMenuItem("粘贴(P)");
jm2.add(jmi2);
JMenu jm5 = new JMenu("历史记录(H)");
jm2.add(jm5);
JMenuItem jmi3 = new JMenuItem("复制历史记录");
jm5.add(jmi3);
JMenuItem jmi4 = new JMenuItem("编辑(E)");
jm5.add(jmi4);
JMenuItem jmi5 = new JMenuItem("取消编辑(N)");
jm5.add(jmi5);
JMenuItem jmi6 = new JMenuItem("清除(L)");
jm5.add(jmi6);
JMenu jm3 = new JMenu("帮助(H)");
mb.add(jm3);
JMenuItem jmi7 = new JMenuItem("查看帮助(V)");
jm3.add(jmi7);
JMenuItem jmi8 = new JMenuItem("关于计算机(A)");
jm3.add(jmi8);
JPanel panel = new JPanel();
panel.setBounds(10, 127, 289, 150);
frame.getContentPane().add(panel);
panel.setLayout(new GridLayout(0, 5, 4, 4));
JButton bMC = new JButton("MC");
panel.add(bMC);
JButton bMR = new JButton("MR");
panel.add(bMR);
JButton bMS = new JButton("MS");
panel.add(bMS);
JButton bM1 = new JButton("M+");
panel.add(bM1);
JButton bM2 = new JButton("M-");
panel.add(bM2);
JButton b_delete = new JButton("<—");
panel.add(b_delete);
JButton bCE = new JButton("CE");
panel.add(bCE);
JButton bC = new JButton("C");
panel.add(bC);
JButton b_rev = new JButton("±");
panel.add(b_rev);
JButton b_sqrt = new JButton("√");
panel.add(b_sqrt);
button7 = new JButton("7");
panel.add(button7);
button8 = new JButton("8");
panel.add(button8);
button9 = new JButton("9");
panel.add(button9);
button_div = new JButton("/");
panel.add(button_div);
JButton b_mod = new JButton("%");
panel.add(b_mod);
button4 = new JButton("4");
panel.add(button4);
button5 = new JButton("5");
panel.add(button5);
button6 = new JButton("6");
panel.add(button6);
button_mul = new JButton("*");
panel.add(button_mul);
JButton b_back = new JButton("1/x");
panel.add(b_back);
JPanel panel_1 = new JPanel();
panel_1.setBounds(9, 281, 288, 75);
frame.getContentPane().add(panel_1);
panel_1.setLayout(null);
button1 = new JButton("1");
button1.setBounds(2, 0, 54, 34);
panel_1.add(button1);
button2 = new JButton("2");
button2.setBounds(60, 0, 54, 34);
panel_1.add(button2);
button3 = new JButton("3");
button3.setBounds(118, 0, 54, 34);
panel_1.add(button3);
button_point = new JButton(".");
button_point.setBounds(118, 38, 54, 34);
panel_1.add(button_point);
button0 = new JButton("0");
button0.setBounds(2, 38, 112, 34);
panel_1.add(button0);
button_equ = new JButton("=");
button_equ.setBounds(234, 0, 54, 72);
panel_1.add(button_equ);
button_equ.addActionListener(new change());
JPanel panel_2 = new JPanel();
panel_2.setBorder(BorderFactory.createLineBorder(Color.BLACK, 3) );
panel_2.setForeground(new Color(0, 0, 0));
panel_2.setBounds(10, 33, 289, 84);
frame.getContentPane().add(panel_2);
panel_2.setLayout(null);
textField = new JTextField();
textField.setHorizontalAlignment(SwingConstants.RIGHT);
textField.setEditable(false);
textField.setBounds(10, 10, 269, 31);
textField.setBorder(new EmptyBorder(0, 0, 0, 0));
textField.setFont(new Font("宋体",Font.PLAIN,20));
panel_2.add(textField);
textField_1 = new JTextField();
textField_1.setHorizontalAlignment(SwingConstants.RIGHT);
textField_1.setEditable(false);
textField_1.setBorder(BorderFactory.createEmptyBorder());
textField_1.setBounds(10, 38, 269, 36);
textField_1.setFont(new Font("宋体",Font.PLAIN,20));
textField_1.setText("0");
panel_2.add(textField_1);
bC.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText("");
textField_1.setText("0");
str.delete(0, str.length());
str1.delete(0, str1.length());
count=0;
}
});
button_sub = new JButton("-");
button_sub.setBounds(176, 0, 54, 34);
panel_1.add(button_sub);
button_sub.addActionListener(new change());
button_add = new JButton("+");
button_add.setBounds(176, 38, 54, 34);
panel_1.add(button_add);
button0.addActionListener(new change());
button1.addActionListener(new change());
button2.addActionListener(new change());
button3.addActionListener(new change());
button4.addActionListener(new change());
button5.addActionListener(new change());
button6.addActionListener(new change());
button7.addActionListener(new change());
button8.addActionListener(new change());
button9.addActionListener(new change());
button_point.addActionListener(new change());
button_add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
count++;
str1.append("+");
textField.setText(str1.toString());
if(count!=1)
{
calculate();
}
str.append("+");
}
});
button_sub.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
count++;
str1.append("-");
textField.setText(str1.toString());
if(count!=1)
{
calculate();
}
str.append("-");
}
});
button_div.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
count++;
str1.append("÷");
textField.setText(str1.toString());
if(count!=1)
{
calculate();
}
str.append("÷");
}
});
button_mul.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
count++;
str1.append("*");
textField.setText(str1.toString());
if(count!=1)
{
calculate();
}
str.append("*");
}
});
}
public void calculate()
{
String s=str.toString();
if(s.contains("+"))
{
String[] arr=s.split("\\+");
sum=Integer.parseInt(arr[0])+Integer.parseInt(arr[1]);
textField_1.setText(sum+"");
str.delete(0, str.length());
str.append(sum+"");
count=1;
}
else if(s.contains("-"))
{
String[] arr=s.split("\\-");
sum=Integer.parseInt(arr[0])-Integer.parseInt(arr[1]);
textField_1.setText(sum+"");
str.delete(0, str.length());
str.append(sum+"");
count=1;
}
else if(s.contains("*"))
{
String[] arr=s.split("\\*");
sum1=Double.parseDouble(arr[0])*Double.parseDouble(arr[1]);
textField_1.setText(sum1+"");
str.delete(0,str.length());
str.append(sum1+"");
count=1;
}
else if(s.contains("÷"))
{
String[] arr=s.split("÷");
if(arr[1].equals("0"))
{ textField_1.setText("除数不能为0");}
else
{sum1=Double.parseDouble(arr[0])/Double.parseDouble(arr[1]);textField_1.setText(sum1+"");
str.delete(0,str.length());
str.append(sum1+"");
count=1;}
}
}
}