swing程序设计基础:
学习swing程序设计一个星期,总结这一周的工作。
最近使用的包有java.awt; javax.swing; java.awt.event这几个包。
一:首先介绍几个类(只介绍部分)
(1) 在java.swing包中有一个叫Jcomponent的类;是Container的直接子类,这个类很重要。这个类的子类
(以及间接 子类)主要有:
重要组件类: 菜单(菜单,菜单项,菜单条);按钮;文本框;文本区;密码框;单选按钮;下拉列表等等;
主要中层容器类:JPanel(面板) ; JScrollPane(滚动窗体);JLayeredPane(分层窗体);JSplitPane(拆分窗体);
(2)在java.swing包中有一个很重要的类,几乎有关Swing的设计都有它。这个类就是底层容器类 JFrame 它是
Frame(也是一个底层容器,功能较少一点)的直接子类;而Frame的直接父类是java.awt包中的window类;
JFrame类就是我们所说的窗口;一切组件(包括中层容器)都要加到这里才有用;所以他是最重要的组件类;
查看API了解里面的类;
附图:
二: 窗口的简单实现:
(1)下面是一个很简单的窗口,里面什么都没有。
public class Demoswing {
public static void main(String[] args) {
JFrame window1=new JFrame("窗口1");
JFrame window2=new JFrame("窗口2");
//Container con=window1.getContentPane();//设置颜色;
window1.getContentPane().setBackground(Color.blue);//默认是白色;
window1.setBounds(60,100,188,108);//设置窗口的初始位置和大小;
window2.setBounds(160,100,188,108);//同上
window1.setVisible(true);//设置窗体是否可见,默认是不可见的;
window2.setVisible(true);
window1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//设置close后窗口状态:
window2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//这个状态是退出程序;
}
}
(2)下面这个窗口里面有菜单,菜单项,菜单条组件
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
import static javax.swing.JFrame.*;
public class DemoSwing {
public static void main(String[] args) {
new windowMenu("带菜单的窗口",200,300,200,190);
}
}
class windowMenu extends JFrame
{
private JMenuBar menubar;
private JMenu menu,submenu;
private JMenuItem item1,item2;
public windowMenu(){}
public windowMenu(String s,int x,int y,int w,int h)
{
init(s);
setLocation(x, y);
setSize(w,h);
setVisible(true);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
private void init(String s) {
setTitle(s);
menubar=new JMenuBar();
menu=new JMenu("菜单");
submenu =new JMenu("软件项目");
item1=new JMenuItem("java话题",new ImageIcon("C:/a.png"));
item2=new JMenuItem("动漫话题",new ImageIcon("b.gif"));
item1.setAccelerator(KeyStroke.getKeyStroke('A'));
item2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK));//
menu.add(item1);
menu.addSeparator();
menu.add(item2);
menu.add(submenu);
submenu.add(new JMenuItem("汽车营销系统"),new ImageIcon("c.gif"));
submenu.add(new JMenuItem("农场信息系统"),new ImageIcon("d.gif"));
menubar.add(menu);
setJMenuBar(menubar);
System.out.println("over");
}
}
(3)下面这个窗体里面有很多常用的组件:JTextField;JFextArea;JButton;JComboBox等组件
package swingdate3;
import java.awt.*;
import javax.swing.*;
public class Demotest {
public static void main(String[] args) {
ComponentInWindow win=new ComponentInWindow();
win.setBounds(100, 100, 310, 260);
win.setTitle("常用组件");
}
}
class ComponentInWindow extends JFrame
{
private JTextField text;
private JButton button;
private JCheckBox checkBox1,checkBox2,checkBox3;
private JRadioButton radio1,radio2;
private ButtonGroup group;//注意这种按钮的使用;
private JComboBox comBox;
private JTextArea area;
public ComponentInWindow()
{
this.init();
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void init()
{
this.setLayout(new FlowLayout());
this.add(new JLabel("文本框"));
text=new JTextField(10);
add(text);
add(new JLabel("按钮"));
button=new JButton("确定");
add(button);
add(new JLabel("选择框"));
checkBox1=new JCheckBox("喜欢音乐");
checkBox2=new JCheckBox("喜欢旅游");
checkBox3=new JCheckBox("喜欢篮球");
add(checkBox1);
add(checkBox2);
add(checkBox3);
add(new JLabel("单选按钮"));
group=new ButtonGroup();
radio1=new JRadioButton("男");
radio2=new JRadioButton("女");
group.add(radio1);
group.add(radio2);
add(radio1);
add(radio2);
add(new JLabel("下拉列表"));
comBox=new JComboBox();
comBox.addItem("音乐天地");
comBox.addItem("武术天地");
comBox.addItem("象棋乐园");
add(comBox);
add(new JLabel("文本区"));
area=new JTextArea(6,12);
add(new JScrollPane(area));//JScrollPane是滚动窗体,一个中层容器;
add(new JLabel("按钮"));
add(new JButton("确定"));
return ;
}
}
这个解释一下:
1,JScrollPane是一个中层容器,他的对象是一个滚动窗体。一般使用他来存放文件区。
三:介绍监听器
在Java开发中,对于事件的处理非常重要,比如响应键盘的输入、鼠标的点击、窗口的移动等等都要涉及到Java事件的应 用。监听器系统包括:事件,事件源(组件),监听器,事件处理 4部分组成;处理不同的事件源使用不同的监听器来处理;
常见的监听器有ActionListener,ItemListener,MouseListener,MouseMotionListener等等;现在只学习了前面三种;
(1)ActionListener事件处理;事件源有按钮,单选按钮,文本框,密码框,菜单项
实例:
package DEmotest;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Democlass {
public static void main(String[] args) {
WindowActionEvent win=new WindowActionEvent();
PoliceListener po=new PoliceListener();
win.setMyCommandListener(po);
System.out.println("over");
}
}
class WindowActionEvent extends JFrame
{
private JTextArea area;
private JTextField text;
private JButton button ;
private MyCommandListener mycommand;
public WindowActionEvent()
{
init();
this.setBounds(100, 100, 460, 360);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setTitle("处理ActionEvent事件");
}
void init()
{
setLayout(new FlowLayout());
area=new JTextArea(9,30);
text=new JTextField(10);
button=new JButton("确定");
add(text);
add(button);
add(area);
}
void setMyCommandListener(MyCommandListener mycommand1)
{
this.mycommand=mycommand1;
mycommand.setJTextArea(area);
mycommand.setJTextField(text);
button.addActionListener(mycommand);
text.addActionListener(mycommand);
}
}
interface MyCommandListener extends ActionListener
{
void setJTextField(JTextField text);
void setJTextArea(JTextArea area);
}
class PoliceListener implements MyCommandListener
{
private JTextField text;
private JTextArea area;
public void actionPerformed(ActionEvent e) {
String str=text.getText();
area.append(str+" "+str.length()+"\n");
}
@Override
public void setJTextField(JTextField text) {
this.text=text;
}
@Override
public void setJTextArea(JTextArea area) {
this.area=area;
}
}
/*这个程序写的还是比较好的;
* 首先利用了有关接口技术:在SetMyCommandListener(MyCommandLIstener Listener)函数中利用接口作为参数,很 好的实现了接口技术;
* 实现了MyCommandLIstener接口的类都可以向这个函数传递参数,结果程序得到了很好的扩展;
* */
附图:
(2)ItemEvent事件处理;事件源有下拉列表,选择框;
package itemEvent;
import javax.swing.*;
import java.awt.*;
import java.io.*;
public class WindowOperation extends JFrame
{
JTextArea area;
JComboBox box;
JTextField one,two;
JButton button;
ComputerListener computerListener;
OperatorListener operatorListener;
public WindowOperation()
{
init();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void init()
{
setLayout(new FlowLayout());
area=new JTextArea(9,30);
box=new JComboBox();
box.addItem("选择运算符");
one=new JTextField(7);
two=new JTextField(7);
button=new JButton("确定");
String a[]={"+","-","*","/"};
for(int i=0;i<a.length;i++)
box.addItem(a[i]);
computerListener=new ComputerListener();
operatorListener=new OperatorListener();
operatorListener.set(box);
operatorListener.setListener(computerListener);
computerListener.setJTxtField(one,two);
computerListener.setarea(area);
button.addActionListener(computerListener);
box.addItemListener(operatorListener);
add(one);
add(box);
add(two);
add(button);
add(new JScrollPane(area));
}
}
package itemEvent;
import java.awt.event.*;
import javax.swing.JComboBox;
import javax.swing.JTextArea;
import javax.swing.JTextField;
class ComputerListener implements ActionListener
{
JTextField textone,texttwo;
JTextArea area;
String fuhao;
void setJTxtField(JTextField one,JTextField two)
{
textone=one;
texttwo=two;
}
void setarea(JTextArea a)
{
area=a;
}
void setfuhao (String name)
{
fuhao=name;
}
public void actionPerformed(ActionEvent e) {
double number1=Double.parseDouble(textone.getText()) ;
double number2=Double.parseDouble(texttwo.getText()) ;
double result=0;
try{
if(fuhao.equals("+"))
{
result=number1+number2;
}
else if(fuhao.equals("-"))
{
result=number1-number2;
}
else if(fuhao.equals("*"))
{
result=number1*number2;
}
else if(fuhao.equals("/"))
{
result=number1/number2;
}
area.append(number1+" "+fuhao+" "+number2+" "+"="+result+"\n");
}
catch(Exception ec)
{
area.append("请输入数字字符"+"\n");
}
}
}
class OperatorListener implements ItemListener
{
ComputerListener Work;
JComboBox box;
void set(JComboBox bo)
{
box=bo;
}
void setListener(ComputerListener w)
{
Work=w;
}
public void itemStateChanged(ItemEvent e) {
String fuhao=box.getSelectedItem().toString();
Work.setfuhao(fuhao);
}
}
package itemEvent;
import javax.swing.*;
public class Demotest {
public static void main(String[] args) {
WindowOperation win=new WindowOperation();
win.setBounds(200, 300, 460,360);
win.setTitle("简单计数器");
}
}
附上:
(3)MouseEvent事件处理;全部组件都可以发现这种事件;
实例:
package DEMOjava;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class myMouseListener implements MouseListener
{
JTextField text;
JTextArea area;
public void settext(JTextField b)
{
text=b;
}
public void settextarea(JTextArea a)
{
area=a;
}
public void mouseClicked(MouseEvent e) {
if(e.getSource() instanceof JButton)
{
if(e.getClickCount()==2)
area.append(text.getText()+"\n");
}
}
@Override
public void mousePressed(MouseEvent e) {
// TODO 自动生成的方法存根
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
package DEMOjava;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class WindowMouse extends JFrame
{
JTextArea textarea;
JTextField text;
JButton button;
myMouseListener Listener;
public WindowMouse()
{
init();
this.setBounds(600, 260, 300, 300);
this.setVisible(true);
this.setTitle("示例窗口:");
}
private void init()
{
this.setLayout(new FlowLayout());
textarea=new JTextArea(9,13);
text=new JTextField(8);
button=new JButton("确定");
Listener=new myMouseListener();
Listener.settext(text);
Listener.settextarea(textarea);
button.addMouseListener(Listener);
add(text);
add(button);
add(new JScrollPane(textarea));
}
}
package DEMOjava;
import javax.swing.JFrame;
public class UseMouse {
public static void main(String[] args) {
WindowMouse win=new WindowMouse();
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
附图:
以上是这一段时间的学习记录;