窗口

1、 制作一个如图所示的界面(使用lowLayoutF布局),不要求实现功能。

package ad;
import java.awt.;
import javax.swing.
;

public class ap {
JFrame jf;
JButton b1,b2;
JPanel p;
JLabel l1,l2,l3,l4,l5,l6,l7,l8,l9;
JTextField t1,t2,t3,t4,t5,t6;
public ap(){
jf = new JFrame();
b1 = new JButton("开");
b2 = new JButton("关");
p = new JPanel();
l1 = new JLabel("当前时间:");
l2 = new JLabel("闹钟时间:");
l3 = new JLabel("时");
l4 = new JLabel("分");
l5 = new JLabel("秒");
l6 = new JLabel("时");
l7 = new JLabel("分");
l8 = new JLabel("秒");
l9 = new JLabel("闹钟设置");
t1 = new JTextField(5);
t2 = new JTextField(5);
t3 = new JTextField(5);
t4 = new JTextField(5);
t5 = new JTextField(5);
t6 = new JTextField(5);
jf.add(p);
p.add(l1);
p.add(t1);
p.add(l3);
p.add(t2);
p.add(l4);
p.add(t3);
p.add(l5);
p.add(l2);
p.add(t4);
p.add(l6);
p.add(t5);
p.add(l7);
p.add(t6);
p.add(l8);
p.add(l9);
p.add(b1);
p.add(b2);
jf.setVisible(true);
jf.setSize(350,350);
}
public static void main(String[] args){
new ap();
}

}
2、设计一个用标签、文本行与按钮来登录的界面(用GridLayout布局方式)。如图所示。

package ad;
import java.awt.*;

import javax.swing.*;

public class fz extends JFrame {
JFrame jf;
JButton b1,b2;
JPanel p;
JLabel l1,l2;
JTextField t1,t2;
public fz(){
Container jf = getContentPane();
setLayout(new GridLayout(7,3,5,5));
jf = new JFrame();
b1 = new JButton("确定");
b2 = new JButton("取消");
p = new JPanel();
l1 = new JLabel("用户名:");
l2 = new JLabel("口令:");
t1 = new JTextField(5);
t2 = new JTextField(5);
jf.add(p);
p.add(l1);
p.add(t1);
p.add(l2);
p.add(t2);
p.add(b1);
p.add(b2);
jf.setVisible(true);
jf.setSize(300,300);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new fz();
}
}
3.

设计一个如图所示的界面,不需要提供组件的功能。
import java.awt.;
import javax.swing.
;
public class jisuanji{
public static void main(String[] args){
JFrame jf=new JFrame("计算器");
jf.setLayout(new GridLayout(6,3,3,3));
JPanel jp = new JPanel(new GridLayout(1,3,3,3));

jf.add(jp);
JPanel jp1= new JPanel(new GridLayout(1,4,3,3));
JPanel jp2= new JPanel(new GridLayout(1,4,3,3));
JPanel jp3= new JPanel(new GridLayout(1,4,3,3));
JPanel jp4= new JPanel(new GridLayout(1,4,3,3));
JTextField jt1=new JTextField();
jf.add(jt1);
String str[]={"7", "8", "9", "/",};
JButton jb=null;
for(int i=0;i<4;i++){
jb = new JButton(""+str[i]);
jp1.add(jb) ;
}
String str2[]={ "4", "5", "6", "*", };
JButton jb2=null;
for(int i=0;i<4;i++){
jb2=new JButton(""+str2[i]);
jp2.add(jb2);
}

String str3[]={"1", "2", "3", "-",};
JButton jb3=null;
for(int i=0;i<4;i++){
jb3 = new JButton(""+str3[i]);
jp3.add(jb3) ;
}

String str4[]={ "0", ".", "=", "+"};
JButton jb4=null;
for(int i=0;i<4;i++){
jb4 = new JButton(""+str4[i]);
jp4.add(jb4) ;
}
jf.add(jp1);
jf.add(jp2);
jf.add(jp3);
jf.add(jp4);
jf.pack();
jf.setVisible(true);
}
}

4.编写可改变背景颜色的窗口。

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class ap {
JFrame jf;
JPanel p1,p2;
JButton b1,b2,b3;
public ap(){
jf = new JFrame();
jf.setLayout(new BorderLayout());
p1 = new JPanel();
p2 = new JPanel();
b1 = new JButton("红色");
b2 = new JButton("绿色");
b3 = new JButton("蓝色");
ColorAction redAction = new ColorAction(Color.RED);
ColorAction greenAction = new ColorAction(Color.GREEN);
ColorAction blueAction = new ColorAction(Color.BLUE);
b1.addActionListener(redAction);
b2.addActionListener(greenAction);
b3.addActionListener(blueAction);
jf.add(p1,BorderLayout.NORTH);
jf.add(p2,BorderLayout.CENTER);
p1.add(b1);
p1.add(b2);
p1.add(b3);
jf.setVisible(true);
jf.setSize(350,350);
}
public static void main(String[] args){
new ap();
}
class ColorAction implements ActionListener{
private Color backgroundColor;
public ColorAction(Color c){
backgroundColor = c;
}
public void actionPerformed(ActionEvent event){
p2.setBackground(backgroundColor);
}

}
}

posted @ 2019-05-15 11:40  杨垚1  阅读(198)  评论(0编辑  收藏  举报