一.计算题(共4 题,100.0分)
1
创建一个窗体(大小400*300,位置200*100),包含一个面板,面板中放入三个按钮,名字如图1所示。
(1)单击“模式对话框”按钮,显示一个模式对话框,如图2所示。
(2)单击“非模式对话框”按钮,显示一个非模式对话框,如图3所示。
(3)自定义一个对话框类(继承Jdialog类),一个面板中放一个标签、一个文本框与一个确定按钮。
(4)单击“自定义对话框”按钮,显示一个自定义对话框,如图4所示。
(5)自定义对话框中用于接收一个数据,当输入的是非数字时,控制台会有错误提示,当输入是数字时,控制台输出此数字的平方,如图5所示。
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);
}
}
}
2
实现书本4.1.2节中代码4-2 JOptionPaneDemo.java的内容。
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();
}
}
3
实现书本4.1.3节中代码4-3 JFileChooserDemo.java的内容,实现以下效果。
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();
}
}