11菜单:
一个完整的菜单是由菜单、菜单条和菜单项组成;
Java中与菜单有关的类有三个:Menubar(菜单条),Menu(菜单),MenuItem(菜单项目)
package myprojects.TestMenuBar;
import java.awt.*;
import java.awt.event.*;
public class TestMenuBar {
MenuBar menubar = new MenuBar();
Menu fileM = new Menu("File");
Menu editM = new Menu("Edit");
Menu toolsM = new Menu("Tools");
Menu helpM = new Menu("Help");
MenuItem fileMI1 = new MenuItem ("New");
MenuItem fileMI2 = new MenuItem("Open");
MenuItem fileMI3 = new MenuItem("Save");
CheckboxMenuItem fileMI5 = new CheckboxMenuItem("Quit",true);
Menu filePrint = new Menu("print");
MenuItem printM1 = new MenuItem("preview");
MenuItem printM2 = new MenuItem("setting");
TestMenuBar()
{
FlowLayout f1 = new FlowLayout();
Frame f = new Frame("TestMenuBar");
f.setLayout(f1);
menubar.add(fileM);//将菜单加入菜单条;
menubar.add(editM);
menubar.add(toolsM);
menubar.add(helpM);
fileM.add(fileMI1);//将菜单项加入到file菜单中
fileM.add(fileMI2);
fileM.add(fileMI3);
filePrint.add(printM1);
filePrint.add(printM2);
fileM.add(filePrint);
//将print菜单作为一个菜单项加入file菜单中
fileM.addSeparator();//将一条分割线加入到菜单中
fileM.add(fileMI5);//将菜单项加入菜单中
f.setMenuBar(menubar);//把整个菜单系统显示在窗口中
f.setBounds(0,0,250,200);
f.setVisible(true);
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
printM1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("preview"))
System.out.println("doing preview");
else if (e.getActionCommand().equals("setting"))
System.out.println("doing setting");
}
});
printM2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("preview"))
System.out.println("doing preview");
else if (e.getActionCommand().equals("setting"))
System.out.println("doing setting");
}
});
}
public static void main(String [] args)
{
new TestMenuBar();
}
}
Container类是所有容器类的父类,Container.add方法用于将组件添加到容器中
Container也是Component的子类,因此也可以作为组件增加到其他容器上
11Window类中有两个子类:Frame类和Dialog类
public Dialog(Frame owner,String title,boolean modal);modal为true时为模态对话框,用户不能操作其他窗口,直到这个窗口被关闭;对话框不能独立存在,必须有一个上级窗口
TestDialog
package myprojects.TestDialog;
import java.awt.*;
import java.awt.event.*;
public class TestDialog
{
TextField tf = new TextField(10);
Button b1 = new Button("模态显示");
Button b2 = new Button("非模态显示");
Frame f = new Frame ("TestDialog");
Button b3 = new Button("确定");
Dialog dlg = new Dialog(f,"Dialog Title",true);
FlowLayout f1 = new FlowLayout();
TestDialog()
{
f.setLayout(f1);
f.add(tf);
f.add(b1);
f.add(b2);
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
dlg.setModal(true);
dlg.setVisible(true);
tf.setText("www.it315.org");
}
});
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
dlg.setModal(false);
dlg.setVisible(true);
tf.setText("www.it315.org");
}
});
f.setBounds(0,0,400,200);
f.setVisible(true);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
dlg.setLayout(f1);
dlg.add(b3);
b3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
dlg.disable();
}
});
dlg.setBounds(0,0,200,250);
}
public static void main(String[] args)
{
new TestDialog();
}
}
dialog类下有一种用于文件存取对话框的子类,这就是FileDialog类,不能在Applet中使用,只能在Application中使用
12布局管理器
一个容器中的各个组件之间的位置和大小关系就称之为布局。
Java语言提供了布局管理器来管理组件在容器中的布局,而不是直接使用位置坐标来设置各个组件的位置和大小。
BorderLayout将容器划分为东、南、西、北、中5个区域,默认为放置在中间区域;
改变容器大小时,北方和南方的组件只能改变宽带,东方和南方的组件只能改变高度,中间组件可以改高度和宽度;
FlowLayout是将组件从左到右,从上到下依次排列。FlowLayout是Panel和applet的默认布局管理器,调用Container.setLayout方法改变;
GridLayout在容器上添加组件时会按从左到右,从上到下的顺序在网格中排列,需要事先指定容器划分成的网格的行、列数;
CardLayout事先将多个组件放在同一容器区域交替显示;
GridBagLayout为复杂布局管理器,功能非常强大,使用时也比较复杂。
取消布局管理器
调用Container.setLayout(null)方法取消布局管理器设置,在这种情况下,可以调用Component.setBounds方法来用绝对坐标设置容器上的每个组件的大小和位置。
不使用布局管理器将会给程序带来一个潜在的问题,当容器大小改变时,所有的组件仍保持原来的位置和大小,将导致整个程序界面比较“难看
13Swing和JFC从AWT过渡到Swing
两者区别:
所有的Swing组件,位于javax.swing包中,他们是构筑在AWT上层的GUI组件,Swing组件2是JComponent类的子类,JComponent又是java.awt.Container的子类。
为保证可移植性,Swing完全用Java语言编写。
Swing体格了比AWT更多的组件库,比如,JTable,JTree,FComboBox
Swing 也增强了AWT中原有组件的功能,例如,与AWT中的Button对应的Swing组件是JButton。
JFC(Java Foundation Class)是指Sun对早期的JDK进行扩展的部分,集合了Swing组件与其他简化开发的API类,包括Swing,java 2D,accessibility,internationalization。
JFrame
JFrame上不能直接增加子部件和设置布局管理器,必须先用JFrame.getContentPane()方法JFrame中自带的JRootPane对象,JRootPane是JFrame唯一的子组件;
JScrollPane
Swing提供了一个JOptionPane类,来处理对话框界面相关事件操作,如关闭对话框是提示进行“确认”或是“取消”操作的方法:showConfirmDialog(JFrame owner,String title,JOptionPane.YES_NO_OPTION);
文本框默认为左对齐的,但我们习惯右对齐,AWT中没有解决对齐问题的方法,但Swing中的JTextField中有setHorizontalAlignment方法可以解决这个问题
Swing中的标准对话框
JOptionPane类提供了若干个showXxDialog静态方法,可用来产生简单的标准对话框。
结合Swing类和布局管理器实现计算器界面的程序:
Calculator
package myprojects.Calculator;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator implements ActionListener
{
JFrame jf = new JFrame("Calculator");
JTextField tf = new JTextField();
public void init()
{
Container c = jf.getContentPane();
tf.setHorizontalAlignment(JTextField.RIGHT);
c.add(tf,"North");
JPanel pn1 = new JPanel();
c.add(pn1,"Center");
pn1.setLayout(new GridLayout(4,4));
JButton b = new JButton("1");
b.addActionListener(this);
pn1.add(b);
b = new JButton("2");
b.addActionListener(this);
pn1.add(b);
b = new JButton("3");
b.addActionListener(this);
pn1.add(b);
b = new JButton("+");
b.addActionListener(this);
pn1.add(b);
b = new JButton("4");
b.addActionListener(this);
pn1.add(b);
b = new JButton("5");
b.addActionListener(this);
pn1.add(b);
b = new JButton("6");
b.addActionListener(this);
pn1.add(b);
b = new JButton("-");
b.addActionListener(this);
pn1.add(b);
b = new JButton("7");
b.addActionListener(this);
pn1.add(b);
b = new JButton("8");
b.addActionListener(this);
pn1.add(b);
b = new JButton("9");
b.addActionListener(this);
pn1.add(b);
b = new JButton("*");
b.addActionListener(this);
pn1.add(b);
b = new JButton("0");
b.addActionListener(this);
pn1.add(b);
b = new JButton(".");
b.addActionListener(this);
pn1.add(b);
b = new JButton("=");
b.addActionListener(this);
pn1.add(b);
b = new JButton("\\");
b.addActionListener(this);
pn1.add(b);
jf.setSize(200,200);
jf.setVisible(true);
}
public static void main(String[] args) {
new Calculator().init();
}
public void actionPerformed(ActionEvent e) {
tf.setText(tf.getText()+e.getActionCommand());
}
}
BoxLayout布局管理器,允许多个组件全部垂直摆放或全部水平摆放。嵌套组合使用多个BoxLayout布局管理器的Panel,可以帮我们实现类似GridBagLayout的功能,但却要比直接使用GridBagLayout简单许多