Java高级编程 第4章 习题1

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

1

实现4.1.2节标准对话框(输入、消息、确认、选项)的例题。

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();

}

}

677434f5ee26f11f9acf4965133b16d0.png

 

a7c0f8047374fa033fb0dab907c06365.png

 

cf838c5bc5a7a231dde522c9819b81ca.png

 

dfa657d093d238a5404a2937e8c24c59.png

 2

实现4.1.3节文件对话框的例题。

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();

}



}

20811449b2d15b314847878544a583e6.png

 

6cebe777f3c8669086f6633a1e6e2c4c.png

 

5d047a87c652016e5b069e62f42fa7b1.png

161a0dc3718e1c7c45c35e224c870ab9.png

 

3

实现4.1.4节颜色对话框的例题。

package ex4;



import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;



import javax.swing.JButton;

import javax.swing.JColorChooser;

import javax.swing.JDialog;

import javax.swing.JFrame;

import javax.swing.JPanel;



public class JColorChooserDemo extends JFrame {



private JPanel p;

private JColorChooser ch;

private JDialog colorDialog;

private JButton btnChange;



public JColorChooserDemo() {

super("颜色对话框");

p = new JPanel();

ch = new JColorChooser();

colorDialog = JColorChooser.createDialog(this, "选取颜色", true, ch, null, null);

btnChange = new JButton("改变面板背景颜色");

btnChange.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent arg0) {

colorDialog.setVisible(true);

p.setBackground(ch.getColor());

}

});

p.add(btnChange);

this.add(p);

this.setSize(800, 600);

this.setLocation(200, 100);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setVisible(true);

}



public static void main(String[] args) {

new JColorChooserDemo();



}



}







88f18daed07f6759c88158f86685d32e.png

 

445399d2673962ee02714622d1789a8c.png

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值