一.计算题(共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();
}
}
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();
}
}
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();
}
}