上次说了,如果不添加事件,点击窗口右上方的X,窗口是不会关闭的,现在要说一下,这是在awt下Frame的情况,如果是使用swing下的JFrame,则不需要添加事件即可直接关闭按钮,awt的功能,swing基本上可以完全取代,而且用swing的组件实现的窗口外观上也比awt的组件实现的窗口要美观一些,如下图所示:
下面会主要使用swing,举一些例子
例1:
import java.awt.*;
import javax.swing.*;
public class san {
public static void main(String[] args) {
a1 a = new a1();
}
}
class a1 {
JFrame f = new JFrame("举例");
JPanel p = new JPanel();// 面板
a1() {
/* 按钮 */
JButton b1 = new JButton("按 钮1");
JButton b2 = new JButton("按 钮2");
JButton b3 = new JButton("按 钮3");
JButton b4 = new JButton("按 钮4");
p.add(b1);
p.add(b2);
p.add(b3);
p.add(b4);
/* 标签 */
JLabel jb1 = new JLabel("标签1");
JLabel jb2 = new JLabel("标签2");
JLabel jb3 = new JLabel("标签3");
JLabel jb4 = new JLabel("标签4");
p.add(jb1);
p.add(jb2);
p.add(jb3);
p.add(jb4);
/* 单选按钮 */
JRadioButton j1 = new JRadioButton("天下风云出我辈");
JRadioButton j2 = new JRadioButton("一入江湖岁月催");
JRadioButton j3 = new JRadioButton("皇图霸业谈笑中");
JRadioButton j4 = new JRadioButton("不胜人生一场醉");
j1.setSelected(true); // 让单选按钮处于被选中状态
j2.setSelected(true);
p.add(j1);
p.add(j2);
p.add(j3);
p.add(j4);
p.setBackground(Color.cyan);// 背景颜色
f.add(p);
f.setSize(200, 300);
f.setVisible(true);
f.setLocation(300, 200);
}
}程序运行结果为:
本程序主要用到的有按钮、标签和单选按钮
例2:
import java.awt.*;
import javax.swing.*;
public class asd {
public static void main(String[] args) {
a2 a = new a2();
}
}
class a2 {
JFrame f = new JFrame("举例");
JPanel p = new JPanel();
a2() {
/* 下拉列表框 */
Choice citext;
citext = new Choice();
citext.add("C语言");
citext.add("数据结构");
citext.add("java");
citext.add("编译原理");
citext.add("计算机组成原理");
citext.add("操作系统");
p.add(citext);
/* 单选框 */
JCheckBox bq1 = null;
bq1 = new JCheckBox("读书");
bq1.setSelected(true); // 选中单选框
p.add(bq1);
/* 文本框 内部文字可改变 */
JTextField JT1, JT2, JT3, JT4;
JT1 = new JTextField(" 金麟岂是池中物 ");
JT2 = new JTextField(" 一遇风云便化龙 ");
JT3 = new JTextField(" 九霄龙吟惊天变 ");
JT4 = new JTextField(" 风云际会浅水游 ");
p.add(JT1);
p.add(JT2);
p.add(JT3);
p.add(JT4);
/* 文本域 内部文字可改变 */
TextArea te;
te = new TextArea("人道渺渺。仙道莽莽。鬼道乐兮。当人生门。 ··· 诸天气荡荡,我道日兴隆。", 10, 25);
p.add(te);
p.setBackground(Color.cyan);// 背景颜色
f.add(p);
f.setSize(240, 300);
f.setVisible(true);
f.setLocation(300, 200);
}
}
程序运行结果为:
像那四句诗和下边那句话所在的位置出都是可以编辑的,当然,也仅仅是可以编辑,本程序所用到的有下拉列表、单选框、文本框和文本域
例3:
import java.applet.*;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.*;
public class TX {
public static void main(String[] args) {
tupian_yinyue tp = new tupian_yinyue();
}
}
class tupian_yinyue {
tupian_yinyue() {
JFrame f = new JFrame();
JPanel pan = new JPanel();
/*背景音乐*/
try {
URL cb;
File f1 = new File("C:\\Users\\CC\\Desktop\\秦时明月-谁主沉浮.wav"); // 引号里面的是音乐文件所在的绝对路径
cb = f1.toURL();
AudioClip aau;
aau = Applet.newAudioClip(cb);
aau.play();
aau.loop();
} catch (MalformedURLException e) {
e.printStackTrace();
}
/*背景图片 */
ImageIcon i1 = new ImageIcon("C:\\Users\\CC\\Desktop\\1.gif");//图片的绝对路径
ImageIcon i2 = new ImageIcon("C:\\Users\\CC\\Desktop\\java.jpg");
JButton b1 = new JButton(i1);
JButton b2 = new JButton(i2);
pan.add(b1);
pan.add(b2);
f.add(pan);
f.setSize(340, 400);
f.setVisible(true);
f.setLocation(300, 200);
}
}
程序运行结果为:
图片和音乐是我的本地的文件,其后都是图片和音乐所在的绝对路径
音乐应为wav格式,我的资源里有格式转换器
这个程序是将两个图片(一个gif格式,一个jpg格式)放到按钮上,然后把按钮放到面板上,最终把面板放到框架上
上面那些,一都是简单的知识,下面会举一个稍微深一点的例子
例4:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class san {
public static void main(String[] args) {
a1 a = new a1();
}
}
class a1 {
JFrame f = new JFrame("举例");
JPanel p = new JPanel();// 面板
a1() {
/************************ 点击按钮打开新窗口 *************************************/
JButton but = new JButton("传送门");
p.add(but);
but.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFrame frame = new JFrame("天下");
ImageIcon i1 = new ImageIcon("C:\\Users\\CC\\Desktop\\2.gif");//图片的绝对路径
JButton b1 = new JButton(i1);
JButton b2 = new JButton("道可道,非常道");
b2.setFont(new Font("隶书", Font.PLAIN, 100));//设置字体类型和大小
frame.setLayout(new GridLayout(2, 6, 20, 20));
frame.add(b2, "North");
frame.add(b1, "Center");
frame.setVisible(true);
frame.setBounds(0, 0, 1000, 600);//窗口大小
frame.setVisible(true);
// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
/**************************************************************************/
p.setBackground(Color.cyan);// 背景颜色
f.add(p);
f.setSize(200, 300);
f.setVisible(true);
f.setLocation(1000, 200);
}
}本程序是给按钮添加了事件,点击按钮,会打开新的窗口,在新的窗口中,有两个按钮,一个按钮上是几个字,一个按钮上是一个动态图片,
程序运行结果如下:
点击传送门按钮后,出现的新窗口界面为:
需要注意的是,此时如果点击关闭新窗口的右上方的 X ,一开始的窗口也会关闭,如果想让新窗口关闭时不影响旧的窗口,可以将倒数第12行的代码注释即可。
【所有代码均在windows系统下eclipse环境JDK 1.7下运行通过】
(如有错误,敬请指正)
原文地址:http://blog.csdn.net/u012421456/article/details/32322303