java窗体应用程序设计

java窗体应用程序设计,第1张

java做C/s的程序主要是用swing技术

你可以看看netbean,用netbean开发swing效率很高,控件都是拖拽后,自动生成代码的。都是用javaSwing开发

import javaawtFrame;

import javaawteventWindowEvent;

import javaawteventWindowListener;

import javaxswingJOptionPane;

public class Windows1 {

Frame f;

public void display() {

f = new Frame("窗口事件1");

fsetSize(300, 200);

faddWindowListener(new WinClose(f)); // 为窗口f注册窗口事件监听器为WinClose类对象

fsetVisible(true);

}

public static void main(String args[]) {

(new Windows1())display();

}

}

class WinClose implements WindowListener {

private Frame f;

public WinClose(Frame f) {

super();

thisf = f;

}

public void windowClosing(WindowEvent e) {

JOptionPaneshowMessageDialog(f, "退出");

fdispose();

Systemexit(0); // 实现WindowListener接口中的抽象方法,在windowClosing方法中写入语句,结束程序

}

public void windowActivated(WindowEvent arg0) {

// TODO Auto-generated method stub

}

public void windowClosed(WindowEvent arg0) {

// TODO Auto-generated method stub

}

public void windowDeactivated(WindowEvent arg0) {

// TODO Auto-generated method stub

}

public void windowDeiconified(WindowEvent arg0) {

// TODO Auto-generated method stub

}

public void windowIconified(WindowEvent arg0) {

// TODO Auto-generated method stub

}

public void windowOpened(WindowEvent arg0) {

// TODO Auto-generated method stub

}

}

package interframe;import javaawtBorderLayout;

import javaawteventActionEvent;

import javaawteventActionListener;import javaxswingJFrame;

import javaxswingJMenu;

import javaxswingJMenuBar;

import javaxswingJMenuItem;

import javaxswingJOptionPane;public class MenuTest { /

@param args

/

JFrame frame;

JMenuBar mb;

JMenu m;

JMenuItem mi1;

JMenuItem mi2; public MenuTest() {

initFrame();

initAction();

} public void initFrame() {

frame = new JFrame();

mb = new JMenuBar();

m = new JMenu("学生查询");

mi1 = new JMenuItem("确认");

mi2 = new JMenuItem("取消"); madd(mi1);

madd(mi2);

mbadd(m);

frameadd(mb, BorderLayoutNORTH);

framesetSize(300, 300);

framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

framesetVisible(true);

} public void initAction() {

mi1addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

Systemoutprintln("click");

JOptionPaneshowMessageDialog(null, "你点击了确定按钮");

}

});

mi2addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

JOptionPaneshowMessageDialog(null, "你点击了取消按钮");

}

});

} public static void main(String[] args) {

// TODO Auto-generated method stub

new MenuTest();

}}

前面的this指的是方法所在的当前类的对象

后面的指的是WindowListener的对象。可以这么写-----

thisaddWindoeListener(new Windoelistener(){

需要实现接口WindowListener的所有方法//这样写很麻烦,这个接口有好几个方法,通常我们中需要其中的窗口关闭方法。

});

这是通过内部类的方式给frame注册监听器。注册的是窗口事件。

同样的有Acionlistener事件等等。同样的有setActionListener()等等方法。

你这样写thisaddWindowListener(this);

用的是另一种方法注册监听器。Frame类实现WindowListener接口。在Frame类中添加需要实现的方法。后一个this指的是当前对象。因为Frame类实现了WindowListener接口。是WindowListener的子类。WindowListener的对象的实例化必须通过子类来实例化。

java的GUI在开发中很少用了,了解它也有好处。帮你锻炼你的逻辑。

加油哦!

登录的窗口主界面JFrame,调用getContentPane(),获取到面板。

JPanel J=(JPanel)thisgetContentPane();

创建一个带图标的标签。

JLabel jb=new JLabel(new ImageIcon("xxjpg"));

添加该标签,并设置为窗口的大小。

Jadd(jb);

jbsetBounds(0, 0, thisgetWidth(), thisgetHeight());

如果在JFrame中还有其他的覆盖于其上的JPanel,需要将其设为透明,

setOpaque(true);

注:代码为JFrame的构造函数中完成,其This指代窗口当前实例。

以上就是关于java窗体应用程序设计全部的内容,包括:java窗体应用程序设计、Java窗口事件类 我的程序错在哪里啊、Java编写一个简单的窗口程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zz/10636783.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-10
下一篇2023-05-10

发表评论

登录后才能评论

评论列表(0条)

    保存