实例见例14。1
import javax.swing.*;//注意:是javax
import java.awt.*;
import java.awt.event.*;
public class TestSWING extends JFrame{
public TestSWING(){
super("An Application using Swing");
Container contentPane=getContentPane();
Icon icon=new ImageIcon("swing.gif","An advanced GIF of Duke on a swing");
JLabel label=new JLabel("Swing!",icon,SwingConstants.CENTER);
contentPane.add(label,BorderLayout.CENTER);
}
public static void main(String args[]){
final JFrame f=new TestSWING();
f.setBounds(100,100,300,250);
f.setVisible(true);
f.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
f.addWindowListener(new WindowAdapter(){
public void windowClosed(WindowEvent e){
System.exit(0);
}
}
);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;//javax中,javax.swing.event.*
public class TestSwing3 extends JApplet{
JButton button=new JButton("button");
/*J*/Icon icon=new ImageIcon("icon.gif");
public TestSwing3(){
Container contentPane=getContentPane();
button.setRolloverIcon(new ImageIcon("punch.gif"));
button.setIcon(new ImageIcon("open_hand.gif"));
contentPane.setLayout(new FlowLayout());
contentPane.add(button);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("action!");
}
}
);
button.addChangeListener(new ChangeListener(){
public void stateChanged(ChangeEvent e){
System.out.println(getButtonState());
}
}
);
}
private String getButtonState(){
ButtonModel model=button.getModel();//button的getModel()方法
String state="Button State: ";
state+=model.isSelected() ? "selected":"deselected";
state += model.isPressed() ? ", pressed" : ", not pressed";
state += model.isArmed() ? ", armed" : ", disarmed";
state += model.isRollover() ? ", rollover" : ", not rollover";//java中也有 :?运算符
return state;
}
}
This is a JApplet for javax.Swing.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestSwing4 extends JApplet {
public TestSwing4() {
Container contentPane = getContentPane();
ButtonGroup group = new ButtonGroup();
JToggleButton[] buttons = new JToggleButton[] {
new JToggleButton(new ImageIcon("ballot_box.gif")),
new JToggleButton(new ImageIcon("palette.gif")),
new JToggleButton(new ImageIcon("light_bulb1.gif")),
new JToggleButton(new ImageIcon("dining.gif")),
new JToggleButton(new ImageIcon("scissors.gif")),
new JToggleButton(new ImageIcon("tricycle.gif")),
};//赋值初始化
Box box = Box.createVerticalBox();//Box??
for(int i=0; i < buttons.length; ++i) {
group.add(buttons[i]);
box.add(Box.createVerticalStrut(5));
box.add(buttons[i]);
}
box.add(Box.createVerticalStrut(5));
contentPane.add(box);
}
}
见例14。7
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TestSwing5 extends JApplet {
public void init() {
Container contentPane = getContentPane();
Icon newIcon = new ImageIcon("new.gif", "Create a new document");
Icon openIcon = new ImageIcon("open.gif","Open an existing document");
JMenuBar mb = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem newItem = new JMenuItem(newIcon);
JMenuItem openItem = new JMenuItem("Open ...", openIcon);
JMenuItem saveItem = new JMenuItem("Save");
JMenuItem saveAsItem = new JMenuItem("Save As ...");
JMenuItem exitItem = new JMenuItem("Exit", 'x');
fileMenu.add(newItem);
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.add(saveAsItem);
fileMenu.addSeparator();
fileMenu.add(exitItem);
MenuItemListener listener = new MenuItemListener(this);//自定义MenuItem的Listener
newItem.addActionListener(listener);
openItem.addActionListener(listener);
saveItem.addActionListener(listener);
saveAsItem.addActionListener(listener);
exitItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
mb.add(fileMenu);
setJMenuBar(mb);
}
}
class MenuItemListener implements ActionListener {
private JApplet applet;
public MenuItemListener(JApplet applet) {
this.applet = applet;//this
}
public void actionPerformed(ActionEvent e) {
JMenuItem item = (JMenuItem)e.getSource();
ImageIcon icon = (ImageIcon)item.getIcon();
if(icon != null)
System.out.println(icon.getDescription());
else
System.out.println(item.getText());
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestSwing6 extends JApplet {
public TestSwing6() {
Container contentPane = getContentPane();
JToolBar tb = new JToolBar();
JComboBox fontCombo = new JComboBox(),
fontSizeCombo = new JComboBox();//逗号表达式,同一时刻建立多个组合框
JButton newButton = new JButton(new ImageIcon("new.gif")),
openButton = new JButton(new ImageIcon("open.gif")),
cutButton = new JButton(new ImageIcon("cut.gif")),
copyButton = new JButton(new ImageIcon("copy.gif")),
pasteButton = new JButton(new ImageIcon("paste.gif"));
fontCombo.addItem("Helvetica");//字体组合框添加项
fontCombo.addItem("Palatino");
fontCombo.addItem("Courier");
fontCombo.addItem("Times");
fontCombo.addItem("Times-Roman");
fontSizeCombo.addItem("10");
fontSizeCombo.addItem("12");
fontSizeCombo.addItem("14");
fontSizeCombo.addItem("16");
fontSizeCombo.addItem("18");
tb.add(newButton);
tb.add(openButton);
tb.addSeparator();
tb.add(cutButton);
tb.add(copyButton);
tb.add(pasteButton);
tb.addSeparator();
tb.add(fontCombo);
tb.add(fontSizeCombo);
contentPane.setLayout(new BorderLayout());
contentPane.add(tb, BorderLayout.NORTH);
}
}