⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 s08.htm

📁 java图形设计卷2 swing
💻 HTM
📖 第 1 页 / 共 2 页
字号:
            <p> 8.2.5 JLabel属性</p>            <p>&nbsp;</p>            <p> 8.2.6 JLabel事件</p>            <p>&nbsp;</p>            <p> 8.2.7 JLabel类总结</p>            <p>&nbsp;</p>            <p align="center"><b>例8-6 创建JLabel实例</b></p>            <hr noshade size="1"><pre>import java.net.URL;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.border.*;public class Test extends JApplet implements SwingConstants {	public void init() {		Container contentPane = getContentPane();		JComboBox iconTextGap = new JComboBox();		JPanel controlPanel = new JPanel();		ImageIcon icon = new ImageIcon(this.getClass().getResource("ladybug.gif"));		final JLabel label = new JLabel("Lady Bug", icon, CENTER);		label.setFont(new Font("Times-Roman", Font.ITALIC, 20));		iconTextGap.addItem("4"); 		iconTextGap.addItem("10");		iconTextGap.addItem("15");		iconTextGap.addItem("20");		iconTextGap.addItem("25");		controlPanel.add(new JLabel("Icon/Text Gap:"));		controlPanel.add(iconTextGap);		contentPane.setLayout(new BorderLayout());		contentPane.add(controlPanel, "North");		contentPane.add(label, "Center");		iconTextGap.addItemListener(new ItemListener() {			public void itemStateChanged(ItemEvent event) {				JComboBox b = (JComboBox)event.getSource();				String    s = (String)b.getSelectedItem();				int       gap = Integer.parseInt(s);								label.setIconTextGap(gap);			}		});	}}</pre>            <hr noshade size="1">            <p> 8.3 按钮</p>            <p>&nbsp;</p>            <p> 8.4 JButtion</p>            <p>&nbsp;</p>            <p align="center"><b>例8-7 一个按钮简单例子</b></p>            <hr noshade size="1"><pre>import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.util.*;public class Test extends JApplet {	JButton button = new JButton("button ...", 								new ImageIcon(this.getClass().getResource("exclaim.gif")));	int actCnt = 0;	public void init() {		Container contentPane = getContentPane();		contentPane.setLayout(new FlowLayout());		contentPane.add(button);		button.addActionListener(new ActionListener() {			public void actionPerformed(ActionEvent event) {				showStatus(event.getActionCommand() + 							" activated " + actCnt + " times");				actCnt++;			}		});	}}</pre>            <hr noshade size="1">            <p> 8.4.1 JButtion属性</p>            <p>&nbsp;</p>            <p> 8.4.2 JButtion事件</p>            <p>&nbsp;</p>            <p align="center"><b>例8-8 处理JButton事件</b></p>            <hr noshade size="1"><pre>import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;public class Test extends JApplet {	//Icon icon = new ImageIcon("icon.gif");	JButton button = new JButton("button");	public Test() {		Container contentPane = getContentPane();		button.setRolloverIcon(new ImageIcon(this.getClass().getResource("punch.gif")));		button.setIcon(new ImageIcon(this.getClass().getResource("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();		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";		return state;	}}</pre>            <hr noshade size="1">            <p> 8.4.3 JButtion类总结</p>            <p>&nbsp;</p>            <p align="center"><b>例8-9 创建JButton实例</b></p>            <hr noshade size="1"><pre>import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Test extends JApplet {	Icon icon = new ImageIcon("icon.gif");	JButton noargButton = new JButton(),				textButton = new JButton("text"),				textIconButton = new JButton("text", icon),				iconButton = new JButton(icon);	public Test() {		Container contentPane = getContentPane();		contentPane.setLayout(new FlowLayout());		contentPane.add(noargButton);		contentPane.add(textButton);		contentPane.add(iconButton);		contentPane.add(textIconButton);	}}</pre>            <hr noshade size="1">            <p align="center"><b>例8-10 把一个按钮指定为缺省的按钮</b></p>            <hr noshade size="1"><pre>import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Test extends JApplet {	Icon icon = new ImageIcon("icon.gif");	JButton noargButton = new JButton(),				textButton = new JButton("text"),				textIconButton = new JButton("text", icon),				iconButton = new JButton(icon);	public Test() {		Container contentPane = getContentPane();		contentPane.setLayout(new FlowLayout());		contentPane.add(noargButton);		contentPane.add(textButton);		contentPane.add(iconButton);		contentPane.add(textIconButton);	}}</pre>            <hr noshade size="1">            <p>&nbsp;</p>            <p align="center"><b>例8-11 程序方式单击一个按钮</b></p>            <hr noshade size="1"><pre>import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Test extends JApplet {	int clickDuration = 68;	public Test() {		Container contentPane = getContentPane();		JPanel controlPanel = new JPanel();		JPanel buttonPanel = new JPanel();		JButton doClick = new JButton("do click");		final JButton clickMe = new JButton("click me");		final JComboBox comboBox = new JComboBox(new Object[] {				"68", "250", "500", "750", "1000"		});		controlPanel.add(new JLabel("Click Duration:"));		controlPanel.add(comboBox);		buttonPanel.add(doClick);		buttonPanel.add(clickMe);		contentPane.add(controlPanel, BorderLayout.NORTH);		contentPane.add(buttonPanel, BorderLayout.CENTER);		getRootPane().setDefaultButton(doClick);		doClick.addActionListener(new ActionListener() {			public void actionPerformed(ActionEvent e) {				clickMe.doClick(clickDuration);			}		});		comboBox.addItemListener(new ItemListener() {			public void itemStateChanged(ItemEvent e) {				if(e.getStateChange() == ItemEvent.SELECTED) {					clickDuration = Integer.parseInt((String)									comboBox.getSelectedItem());				}			}		});	}}</pre>            <hr noshade size="1">            <p align="center"><b>例8-12 JButton图标</b></p>            <hr noshade size="1"><pre>import javax.swing.*;import javax.swing.border.*;import javax.swing.plaf.basic.*;import java.awt.*;import java.awt.event.*;public class Test extends JApplet {	public void init() {		Container contentPane = getContentPane();		Icon icon = new StringIcon("icon for JButton"),			 rolloverIcon = new StringIcon("rollover"),			 pressedIcon = new StringIcon("pressed"),			 disabledIcon = new StringIcon("disabled"),			 selectedIcon = new StringIcon("selected"),			 rolloverSelectedIcon = 					new StringIcon("rollover selected"),			 disabledSelectedIcon = 			 		new StringIcon("disabled selected");		final JButton button = new JButton();		button.setRolloverEnabled(true);		button.setIcon(icon);		button.setRolloverIcon(rolloverIcon);		button.setRolloverSelectedIcon(rolloverSelectedIcon);		button.setSelectedIcon(selectedIcon);		button.setPressedIcon(pressedIcon);		button.setDisabledIcon(disabledIcon);		button.setDisabledSelectedIcon(disabledSelectedIcon);		JComboBox cb = new JComboBox();		cb.addItem("enabled");		cb.addItem("disabled");		cb.addItemListener(new ItemListener() {			public void itemStateChanged(ItemEvent e) {				if(e.getStateChange() == ItemEvent.SELECTED) {					String item = (String)e.getItem();					if(item.equals("enabled")) {						button.setEnabled(true);					}					else {						button.setEnabled(false);					}				}			}		});		contentPane.setLayout(new FlowLayout());		contentPane.add(cb);		contentPane.add(button);	}}class StringIcon implements Icon {	private String s;	public StringIcon(String s) {		this.s = s;	}	public int getIconWidth() { return 100; }	public int getIconHeight() { return 100; }	public void paintIcon(Component c, Graphics g, int x, int y) {		FontMetrics fm = g.getFontMetrics();		g.setColor(c.getForeground());		g.drawString(s, 10, fm.getHeight());	}}</pre>            <hr noshade size="1">            <p>&nbsp;</p>            <p align="center"><b>例8-13 设置按钮边距</b></p>            <hr noshade size="1"><pre>import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Test extends JApplet {	public Test() {		Container contentPane = getContentPane();		JButton button = new JButton("button");		button.setMargin(new Insets(50,25,10,5));		contentPane.setLayout(new FlowLayout());		contentPane.add(button);	}}</pre>            <hr noshade size="1">            <p align="center"><b>例8-14 按钮助记符</b></p>            <hr noshade size="1"><pre>import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Test extends JApplet {	public Test() {		Container contentPane = getContentPane();		JButton button = new JButton("button With Mnemonic");		button.setMnemonic('M');		contentPane.setLayout(new FlowLayout());		contentPane.add(button);	}}</pre>            <hr noshade size="1">            <p>&nbsp;</p>            <p> 8.4.4 AWT兼容</p>            <p>&nbsp;</p>            <p> 8.5 本章回顾</p>            <p>&nbsp;</p>            <p> [<a href="index.html" target="_self">目录</a>][<a href="s07.htm">上一页</a>][<a href="s09.htm">下一页</a>](飒龙收藏/2002.5.18)             </p>            </td>          </tr>        </tbody>      </table>    </td>  </tr></tbody></table><script language="javascript">bottomprint()</script></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -