1.编写一个Java程序,该程序运行的运行结果如下所示。
代码:
import java.awt.*;
import java.awt.event.*;
public class TestFlowLayout {
public static void main(String args[]) {
Frame f = new Frame("Flow Layout");
f.setLocation(500, 500);
Button button1 = new Button("确定");
Button button2 = new Button("打开");
Button button3 = new Button("关闭");
Button button4 = new Button("取消");
f.setLayout(new FlowLayout());
f.add(button1);
f.add(button2);
f.add(button3);
f.add(button4);
f.setSize(300, 200);
f.setVisible(true);
f.addWindowListener(new MyWindowListener());
}
}
class MyWindowListener extends WindowAdapter{
public void windowClosing(WindowEvent e){
Window window = (Window) e.getComponent();
window.dispose();
}
}
结果:
2.编写一个Java程序,该程序运行的运行结果如下所示。
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
public class BorderLayoutWindow {
public static void main(String args[]) {
JFrame frame=new JFrame();
frame.setSize(300, 200);
frame.setLocation(300, 200);
frame.setLayout(new BorderLayout());
frame.add(new Button("BN"), "North");
frame.add( new Button("BS"),"South");
frame.add( new Button("BE"),"East");
frame.add( new Button("BW"),"West");
frame.add( new Button("BC"),"Center");
frame.setTitle("BorderWindow");
frame.setVisible(true);
frame.addWindowListener(new MyWindowListener());
}
}
class MyWindowListener extends WindowAdapter{
public void windowClosing(WindowEvent e){
Window window = (Window) e.getComponent();
window.dispose();
}
}
结果:
3.编写一个Java Application程序,该程序运行的运行结果如下所示。
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
public class GridLayoutWindow extends JFrame {
public GridLayoutWindow() {
setLayout(new GridLayout(3, 2));
add(new Button("b1"));
add(new Button("b2"));
add(new Button("b3"));
add(new Button("b4"));
add(new Button("b5"));
add(new Button("b6"));
}
public static void main(String args[]) {
GridLayoutWindow window = new GridLayoutWindow();
window.setSize(300, 150);
window.setLocation(200, 200);
window.setTitle("GridLayout Example");
window.setVisible(true);
window.addWindowListener(new MyWindowListener());
}
}
class MyWindowListener extends WindowAdapter {
public void windowClosing(WindowEvent e) {
Window window = (Window) e.getComponent();
window.dispose();
}
}
4、编写一个Java GUI应用程序,窗口标题为“GridLayout”,窗口布局如下图A所示,在图A窗口中单击任意一个Button,网格的划分方式会变化为图B;在图B窗口中单击任意一个Button,网格的划分方式会变化为图A。
A图 B图
代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
public class GuiTest implements ActionListener {
private JFrame frame;
private Button[] buttons;
private boolean flag;
public GuiTest() {
flag = true;
frame = new JFrame();
frame.setTitle("GridLayout");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(2, 3, 10, 10));
frame.setSize(300, 300);
frame.setLocation(200, 300);
frame.setVisible(true);
buttons = new Button[6];
String ch[] = { "one", "two", "three", "four", "five", "six" };
for (int i = 0; i < 6; i++) {
buttons[i] = new Button(ch[i]);
buttons[i].addActionListener(this);
frame.add(buttons[i]);
}
frame.addWindowListener(new MyWindowListener());
}
public static void main(String[] args) {
new GuiTest();
}
public void actionPerformed(ActionEvent e) {
if (flag) {
frame.repaint();
frame.setLayout(new GridLayout(3, 2));
for (int i = 0; i < 6; i++) {
frame.add(buttons[i]);
}
frame.setVisible(true);
flag = false;
} else {
frame.repaint();
frame.setLayout(new GridLayout(2, 3, 10, 10));
for (int i = 0; i < 6; i++) {
frame.add(buttons[i]);
}
frame.setVisible(true);
flag = true;
}
}
}
class MyWindowListener extends WindowAdapter {
public void windowClosing(WindowEvent e) {
Window window = (Window) e.getComponent();
window.dispose();
}
}
5.编写一个Java 程序,该程序运行的运行结果如下所示。
代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
public class MenuTest{
public static void main(String args[]){
Frame fr=new Frame("Menu");
fr.setSize(300, 300);
fr.setLocation(200, 300);
MenuBar mb=new MenuBar();
fr.setMenuBar(mb);
Menu m1=new Menu("File");
Menu m2=new Menu("Edit");
Menu m3=new Menu("Help");
mb.add(m1);
mb.add(m2);
mb.setHelpMenu(m3);
MenuItem m11=new MenuItem("New");
MenuItem m12=new MenuItem("Save");
MenuItem m13=new MenuItem("Load");
MenuItem m14=new MenuItem("Quit");
m1.add(m11);
m1.add(m12);
m1.add(m13);
m1.addSeparator();
m1.add(m14);
fr.pack();
fr.setVisible(true);
fr.addWindowListener(new MyWindowListener());
}
}
class MyWindowListener extends WindowAdapter {
public void windowClosing(WindowEvent e) {
Window window = (Window) e.getComponent();
window.dispose();
}
}
6.编写一个Java程序,该程序运行的运行结果如下所示。
代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
public class MenuTest{
public static void main(String args[]){
Frame fr=new Frame("Menu");
fr.setSize(300, 300);
fr.setLocation(200, 300);
MenuBar mb=new MenuBar();
fr.setMenuBar(mb);
Menu m1=new Menu("File");
Menu m2=new Menu("Format");
Menu m3=new Menu("Help");
Menu m4=new Menu("进制");
mb.add(m1);
mb.add(m2);
mb.setHelpMenu(m3);
MenuItem m21=new MenuItem("中文");
MenuItem m41=new MenuItem("二进制");
MenuItem m42=new MenuItem("八进制");
MenuItem m43=new MenuItem("十进制");
m2.add(m21);
m2.add(m4);
m4.add(m41);
m4.add(m42);
m4.add(m43);
fr.pack();
fr.setVisible(true);
fr.addWindowListener(new MyWindowListener());
}
}
class MyWindowListener extends WindowAdapter {
public void windowClosing(WindowEvent e) {
Window window = (Window) e.getComponent();
window.dispose();
}
}
结果:
7、完成下图所示用户登录界面的设计,如果输入用户名:admin,密码:123456,则弹出登录成功对话框,否则弹出登录失败对话框,当单击清空按钮时,将文本框和密码框中的内容清空。
代码:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Login extends JFrame {
private JPanel p;
private JLabel lblName, lblPwd;
private JTextField txtName;
private JPasswordField txtPwd;
private JButton btnOk, btnCancle;
public Login() {
super("登录");
p = new JPanel();
p.setLayout(null);
lblName = new JLabel("用户名");
lblPwd = new JLabel("密 码");
txtName = new JTextField(20);
txtPwd = new JPasswordField(20);
txtPwd.setEchoChar('*');
btnOk = new JButton("确定");
btnCancle = new JButton("清空");
lblName.setBounds(30, 30, 60, 25);
txtName.setBounds(95, 30, 120, 25);
lblPwd.setBounds(30, 60, 60, 25);
txtPwd.setBounds(95, 60, 120, 25);
btnOk.setBounds(60, 90, 60, 25);
btnCancle.setBounds(125, 90, 60, 25);
p.add(lblName);
p.add(txtName);
p.add(lblPwd);
p.add(txtPwd);
p.add(btnOk);
p.add(btnCancle);
this.add(p);
this.setSize(250, 170);
this.setLocation(300, 300);
// 设置窗体不可改变大小
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 注册确定按钮的事件处理
btnOk.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 获取用户输入的用户名
String strName = txtName.getText();
// 获取用户输入的密码
String strPwd = new String(txtPwd.getPassword());
// 在消息对话框中显示用户输入的信息
if (strName.equals("admin") && strPwd.equals("123456"))
JOptionPane.showMessageDialog(null, "恭喜您,登录成功!", "提示窗口", JOptionPane.INFORMATION_MESSAGE);
else
JOptionPane.showMessageDialog(null, "登录失败!", "提示窗口", JOptionPane.INFORMATION_MESSAGE);
}
});
// 注册取消按钮的事件处理
btnCancle.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 清空文本框中的文本
txtName.setText("");
txtPwd.setText("");
}
});
}
public static void main(String[] args) {
Login f = new Login();
f.setVisible(true);
}
}
结果:
8、设计一个简单计算器,如下图所示。在“操作数”标签右侧的两个文本框输入操作数,当单击操作符+,-,×,÷按钮时,对两个操作数进行运算并将结果填入到“结果”标签右侧的文本框中。
代码:
import java.awt.Font;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Calc implements ActionListener {
private Frame Fm = new JFrame();
private JButton quit = new JButton("退出");
private JButton Add = new JButton("+");
private JButton Sub = new JButton("-");
private JButton Mul = new JButton("×");
private JButton Div = new JButton("÷");
private Label Addend = new Label("操作数:");
private Label Augend = new Label("操作数:");
private Label Result = new Label("结果");
private TextField fieldAdd = new TextField();
private TextField fieldAug = new TextField();
private TextField areaResult = new TextField();
private Font Ft = new Font("宋体", Font.BOLD, 18);
public Calc() {
Fm.setTitle("简单计算器");
Fm.setLayout(null);
Fm.setBounds(200, 100, 400, 300);
Addend.setBounds(20, 70, 60, 30);
Fm.add(Addend);// 标签--操作数1
Addend.setFont(Ft);// 字体类型
fieldAdd.setBounds(100, 70, 140, 30);
Fm.add(fieldAdd);// 输入区--操作数1
fieldAdd.setFont(Ft);// 字体类型
Augend.setBounds(20, 120, 60, 30);
Fm.add(Augend);// 标签--操作数2
Augend.setFont(Ft);
fieldAug.setBounds(100, 120, 140, 30);
Fm.add(fieldAug);// 输入区--操作数2
fieldAug.setFont(Ft);
Result.setBounds(20, 170, 60, 30);
Fm.add(Result);// 标签--结果
Result.setFont(Ft);
areaResult.setBounds(100, 170, 140, 30);
Fm.add(areaResult);
areaResult.setFont(Ft);
Add.setBounds(260, 50, 100, 30);
Fm.add(Add);
Add.addActionListener(this);
Sub.setBounds(260, 90, 100, 30);
Fm.add(Sub);
Sub.addActionListener(this);
Mul.setBounds(260, 130, 100, 30);
Fm.add(Mul);
Mul.addActionListener(this);
Div.setBounds(260, 170, 100, 30);
Fm.add(Div);
Div.addActionListener(this);
//退出
quit.setBounds(260, 210, 100, 30);
Fm.add(quit);
quit.addActionListener(this);
Fm.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
double a, b;
a = Double.parseDouble(fieldAdd.getText());
b = Double.parseDouble(fieldAug.getText());
try {
if (e.getSource() == quit)
System.exit(0);
else if (e.getSource() == Add)
areaResult.setText(String.valueOf(a + b));
else if (e.getSource() == Sub)
areaResult.setText(String.valueOf(a - b));
else if (e.getSource() == Mul)
areaResult.setText(String.valueOf(a * b));
else if (e.getSource() == Div)
areaResult.setText(String.valueOf(a / b));
} catch (Exception Ex) {
areaResult.setText("出错了:" + Ex);
}
}
public static void main(String args[]) {
Calc call = new Calc();
}
}