yezongbo
发贴: 8
积分: 0
|
于 2005-09-24 15:13
button的界面
import java.awt.*; import java.awt.event.*; import javax.swing.*;
public class button extends WindowAdapter implements ActionListener { JButton b1 = null; JButton b2 = null; JFrame f; public button() { JFrame f = new JFrame("EventDemo6"); Container contentPane = f.getContentPane(); contentPane.setLayout(new GridLayout(1,2)); b1 = new JButton("按我有声音喔"); b2 = new JButton("按我可开新窗口"); b1.addActionListener(this); b2.addActionListener(this); contentPane.add(b1); contentPane.add(b2); f.pack(); f.show(); f.addWindowListener(this); }
public void actionPerformed(ActionEvent e) { if((e.getActionCommand()).equals("按我有声音喔")) //getActionCommand()方法会返回按钮上的文字字符串。 Toolkit.getDefaultToolkit().beep(); if((e.getActionCommand()).equals("按我可开新窗口")) { file newF = new file(); newF.setSize(200,200); newF.setVisible(true); f.setVisible(false); } } public void windowClosing(WindowEvent e) { System.exit(0); } public static void main(String args[]) { new button(); } }
新窗口的界面import java.awt.event.*; import java.awt.*; import javax.swing.*;
public class file extends JFrame{ JLabel label ; public file(){ Container container=getContentPane(); JLabel label =new JLabel("label"); container.add(label); } public static void main(String [] args) {file c=new file(); c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
|