Java高级编程 第4章 高级UI组件

一.计算题(共4 题,100.0分)

1

创建一个窗体(大小400*300,位置200*100),包含一个面板,面板中放入三个按钮,名字如图1所示。

(1)单击“模式对话框”按钮,显示一个模式对话框,如图2所示。

(2)单击“非模式对话框”按钮,显示一个非模式对话框,如图3所示。

(3)自定义一个对话框类(继承Jdialog类),一个面板中放一个标签、一个文本框与一个确定按钮。

(4)单击“自定义对话框”按钮,显示一个自定义对话框,如图4所示。

(5)自定义对话框中用于接收一个数据,当输入的是非数字时,控制台会有错误提示,当输入是数字时,控制台输出此数字的平方,如图5所示。

7800cca6affe9a72e229740b5f921fcd.png

 

422d0a45e1169b5cc7421299665c5d5f.png

 

8661419f6e613de2189f432c6c3860ef.png

 

0245707ee998670e43a36e42ef85ea0a.png

 

3af5ae53ad0d8a6b32fa9e8896182d87.png

 

package ex4;



import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;



import javax.swing.JButton;

import javax.swing.JDialog;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextField;



public class JDialogDemo extends JFrame {



private JPanel p;

private JButton btnMod, btnNon, btnMy;

private JDialog modDialog, nonDialog;

private MyDialog myDialog;



public JDialogDemo() {

super("测试对话框");

p = new JPanel();

btnMod = new JButton("模式对话框");

btnNon = new JButton("非模式对话框");

btnMy = new JButton("自定义对话框");



// 下面这两个都忘记设置大小了

modDialog = new JDialog(this, "模式对话框", true);

modDialog.setBounds(250, 200, 200, 100);

nonDialog = new JDialog(this, "非模式对话框", false);

nonDialog.setBounds(250, 200, 200, 100);

myDialog = new MyDialog(this);



btnMod.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent arg0) {

modDialog.setVisible(true);

}

});



btnNon.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent arg0) {

nonDialog.setVisible(true);

}

});



btnMy.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent arg0) {

myDialog.setVisible(true);

}

});

p.add(btnMod);

p.add(btnNon);

p.add(btnMy);

this.add(p);

this.setBounds(200, 100, 400, 300);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setVisible(true);

}



public static void main(String[] args) {

new JDialogDemo();

}



class MyDialog extends JDialog {

private JPanel p;

private JLabel lbNum;

private JButton btnOk;

private JTextField txtNum;



public MyDialog(JFrame jf) {

super(jf, "我的对话框", true);

p = new JPanel();

lbNum = new JLabel("请输入一个数");

txtNum = new JTextField(10);

btnOk = new JButton("确定");

btnOk.addActionListener(new ActionListener() {

@Override



public void actionPerformed(ActionEvent e) {

try {

int num = Integer.parseInt(txtNum.getText().trim());

System.out.println(num + "*" + num + "=" + num * num);

} catch (Exception ex) {

System.out.println(txtNum.getText() + "不是数字,请重新输入!");

}

}

});

p.add(lbNum);

p.add(txtNum);

p.add(btnOk);

this.add(p);

// this.setBounds(200, 100, 400, 300);



// 下面这个是包裹他的大小

this.pack();

this.setLocation(250, 200);

}

}

}

389be23911e2c9ce387a4fc1b752f71e.png

e9b86b0a77cb900c7ec9ec433d3a9dbe.png

 

5f078eb51462bd570a7b576fdae99ee3.png

2

实现书本4.1.2节中代码4-2 JOptionPaneDemo.java的内容。

44fd185978db5e4522606729df514a24.png

 

645ef36f180f7845df0dc21411168b7a.png

7f6135ccac5b9d29e459c2d9e8385891.png

 

5a980dad1ff89cef3f2fbc5a0dda9998.png

d02f0beb347545eefb0833646fea447c.png

 

package ex4;



import java.awt.BorderLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;



import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JTextArea;



public class JOptionPaneDemo extends JFrame {



private JPanel p;

private JTextArea txtContent;

private JButton btnInput, btnMsg, btnConfirm, btnOption;



public JOptionPaneDemo() {

super("JOptionPane标准对话框");

p = new JPanel();

btnInput = new JButton("输入");

btnMsg = new JButton("消息");

btnConfirm = new JButton("确认");

btnOption = new JButton("选项");

txtContent = new JTextArea(20, 10);



btnInput.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

String str = JOptionPane.showInputDialog(btnInput, "请输入一个数字", "输入", JOptionPane.QUESTION_MESSAGE);

try {

int num = Integer.parseInt(str.trim());

txtContent.append(num + "*" + num + " = " + num * num + "\n");

} catch (Exception e1) {

txtContent.append(str + "不是数字,请重新输入\n");

}

}

});



btnMsg.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

// 下面是messsage,不是input

JOptionPane.showMessageDialog(btnMsg, "下午两点开QST员工大会", "消息", JOptionPane.INFORMATION_MESSAGE);

txtContent.append("显示消息对话框\n");

}

});



btnConfirm.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

int r = JOptionPane.showConfirmDialog(btnConfirm, "你确定要删除吗", "删除", JOptionPane.YES_NO_OPTION);

if (r == JOptionPane.YES_OPTION) {

txtContent.append("显示确认对话框您选择了'是'" + r + "\n");

} else {

txtContent.append("显示确认对话框您选择了'否'" + r + "\n");

}

}

});



btnOption.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

Object[] options = { "Red", "Green", "Blue" };

int sel = JOptionPane.showOptionDialog(btnOption, "选择颜色", "选择", JOptionPane.DEFAULT_OPTION,

JOptionPane.WARNING_MESSAGE, null, options, options[0]);

if (sel != JOptionPane.CLOSED_OPTION) {

txtContent.append("显示选择对话框!颜色:" + options[sel] + "\n");

}

}

});

p.add(btnInput);

p.add(btnMsg);

p.add(btnConfirm);

p.add(btnOption);



this.add(txtContent);

this.add(p, BorderLayout.SOUTH);

this.setSize(400, 300);

this.setLocation(200, 100);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setVisible(true);

}



public static void main(String[] args) {

new JOptionPaneDemo();

}

}

b111c50372457d00f7e3e3d49b365571.png

 

86846bec01947de91a7a02482b6f04e4.png

 

83c943dbbb9815cb2f7ab94b2e6b7d94.png

 

8bfe71113896607324c4576bfa44d4b8.png

 3

实现书本4.1.3节中代码4-3 JFileChooserDemo.java的内容,实现以下效果。

11d83f9c315783afbda59208d1d455b1.png

 

289d630fa2efe379faf4714c341f67a9.png

 

82a2d515237b887d74795c2b03d9ea17.png

 

75cb0f4d2b17c3cc0fe13a723a402284.png

 

31d77f29d17e236b38d20504d093eb7f.png

 

cb8564ea2f132f4d656136212d87c301.png

 

package ex4;



import java.awt.BorderLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.FileWriter;

import javax.swing.JButton;

import javax.swing.JFileChooser;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;



public class JFileChooserDemo extends JFrame {

private JPanel p;

private JScrollPane sp;

private JTextArea txtConTent;

private JButton btnOpen, btnSave, btnClear;

public JFileChooserDemo() {

super("JFileChooser文件对话框");

p = new JPanel();

txtConTent = new JTextArea(20, 10);

btnOpen = new JButton("打开");

btnSave = new JButton("保存");

btnClear = new JButton("清空");

sp = new JScrollPane(txtConTent);

btnOpen.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

openFile();

}

});



btnSave.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

SaveFile();

}

});



btnClear.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

txtConTent.setText("");

}

});



p.add(btnOpen);

p.add(btnSave);

p.add(btnClear);

this.add(sp);

this.add(p, BorderLayout.SOUTH);

// this.setBounds(600, 500, 200, 100);

this.setBounds(200, 100, 600, 500);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setVisible(true);

}



protected void openFile() {

JFileChooser fChooser = new JFileChooser();

int rV = fChooser.showOpenDialog(this);

if (rV == JFileChooser.APPROVE_OPTION) {

String filename = fChooser.getSelectedFile().getName();

String path = fChooser.getCurrentDirectory().toString();

try {

FileReader fread = new FileReader(path + "/" + filename);

BufferedReader bread = new BufferedReader(fread);

String line = bread.readLine();

while (line != null) {

txtConTent.append(line + "\n");

line = bread.readLine();

}

bread.close();

fread.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}



protected void SaveFile() {



JFileChooser fChooser = new JFileChooser();

int rV = fChooser.showOpenDialog(this);

if (rV == JFileChooser.APPROVE_OPTION) {

String filename = fChooser.getSelectedFile().getName();

String path = fChooser.getCurrentDirectory().toString();

try {

FileWriter fwriter = new FileWriter(path + "/" + filename);

fwriter.write(txtConTent.getText());

fwriter.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}



public static void main(String[] args) {

new JFileChooserDemo();

}



}

 

91ffee46fdc9433ecfccf385b443498e.png

 

5b5ab2a8603ee06eaed3215b5615d5fa.png

 

a41ea4a86c99a6da3b35328a71f7ff9a.png

 

85b7baa7ded28e4cb8a97b2422f97f4a.png

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值