} }
//例程5-12:ToolBarDemo.java import java.awt.*; import javax.swing.*; import java.awt.event.*; public class ToolBarDemo {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle(\JToolBar toolbar = new JToolBar(); //将工具栏添加在窗口顶部
frame.getContentPane().add(toolbar, BorderLayout.NORTH); JButton button = new JButton(\toolbar.add(button); toolbar.addSeparator();
JComboBox combo = new JComboBox(); combo.addItem(\combo.addItem(\toolbar.add(combo);
Dimension dim = new Dimension(200,100); JTextArea text = new JTextArea(); text.setPreferredSize(dim); frame.getContentPane().add(text);
frame.addWindowListener(new WindowAdapter() { });
frame.pack(); frame.setVisible(true);
public void windowClosing (WindowEvent e) { }
System.exit(0);
} }
//例程5-13:DialogDemo.java import java.awt.*; import java.awt.event.*; public class DialogDemo {
public static void main(String[] args) {
DialogFrame df = new DialogFrame(\df.setSize(200,150);
df.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
}
}
});
df.show();
class DialogFrame extends Frame implements ActionListener {
Label message; Button get;
OkCancelDialog dialog; DialogFrame(String title) { }
public void actionPerformed(ActionEvent event) { }
if (event.getSource() == get) { }
dialog.setVisible(true); message.setText(dialog.data); super(title);
message = new Label(\get = new Button(\get.addActionListener(this); setLayout(new GridLayout(2,1)); add(message); add(get);
dialog = new OkCancelDialog(this, \
}
class OkCancelDialog extends Dialog implements ActionListener {
Button ok, cancel; TextField text; public String data;
OkCancelDialog(Frame hostFrame, String title, boolean dModal) {
super(hostFrame, title, dModal); setSize(250, 100);
setLayout(new FlowLayout()); text = new TextField(30); add(text);
ok = new Button(\
ok.addActionListener((ActionListener)this); add(ok);
cancel = new Button(\
cancel.addActionListener((ActionListener)this); add(cancel); data = new String(\
}
public void actionPerformed(ActionEvent event) {
}
if (event.getSource() == ok) { }
setVisible(false);
//将窗口设置为不可见
data = text.getText(); data = \} else {
}
//例程5-14:OptionDialogDemo.java import java.awt.*; import javax.swing.*;
public class OptionDialogDemo {
public static void main(String[] args) {
JFrame frame = new JFrame();
Object[] options = {\是的,很喜欢\喜欢,但更喜欢别的\我会试着喜欢\不,
很不喜欢\
//显示一个OptionDialog,并获得方法返回值
int n = JOptionPane.showOptionDialog(frame, \你喜欢使用JAVA吗?\\调查\
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
if (n >= 0 && n < options.length)
JOptionPane.showMessageDialog(frame, \你的选择是:\选择提
示\
else
JOptionPane.showMessageDialog(frame, \你没有作选择\选择提示\
JOptionPane.PLAIN_MESSAGE);
System.exit(1);
}
}
//例程5-15:AwtTotalDemo.java import java.awt.*; import java.awt.event.*; public class AwtTotalDemo {
public static void main(String[] args) {
Frame f = new Frame(\f.setLayout(new GridLayout(7, 1));
Panel p1 = new Panel();
p1.add(new Label(\请输入您的姓名:\p1.add(new TextField(30)); f.add(p1);
Panel p2 = new Panel(); p2.add(new Label(\性别:\
CheckboxGroup cbg = new CheckboxGroup(); //建立一个CheckboxGroup对象 p2.add(new Checkbox(\男\
p2.add(new Checkbox(\女\ f.add(p2);
Panel p3 = new Panel();
p3.add(new Label(\您的爱好:\ p3.add(new Checkbox(\体育\ p3.add(new Checkbox(\音乐\ p3.add(new Checkbox(\电脑\
p3.add(new Checkbox(\文学\f.add(p3);
Panel p4 = new Panel();
p4.add(new Label(\您常用的编程语言有:\ List list = new List(3, true); list.add(\ list.add(\ list.add(\ list.add(\ list.add(\ list.add(\汇编\
p4.add(list);
f.add(p4);
Panel p5 = new Panel(); p5.add(new Label(\您的学历\ Choice c = new Choice();
c.addItem(\中专\ c.addItem(\高中\ c.addItem(\大专\ c.addItem(\本科\ c.addItem(\研究生\ p5.add(c); f.add(p5);
Panel p6 = new Panel();
p6.add(new Label(\请您留言:\ p6.add(new TextArea(\畅所欲言!\ f.add(p6);
Panel p7 = new Panel(); p7.add(new Button(\提交\ p7.add(new Button(\取消\
f.add(p7); }); f.pack(); f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) { }
System.exit(0);
}
}
//例程5-16:SwingDemo.java import java.awt.*; import java.awt.event.*; import javax.swing.*;
class SwingDemo extends JFrame implements ActionListener { private Container content;
JMenuBar bar = new JMenuBar();
//新建一个菜单栏 //新建“File”菜单
JMenu fileMenu = new JMenu(\
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说教育文库Java程序设计教程 冶金工业出版社第5章(3)在线全文阅读。
相关推荐: