用java程序设计, 编写GUI程序

用java程序设计, 编写GUI程序,第1张

楼上的 搜来的代码 也要改改才能符合楼主的要求吧…… 我两个都写到一起了 import javaawt;import javaxswing;

import javaawtBorderLayout;

import javaawtRectangle;

import javaawteventMouseEvent;

import javaawteventMouseAdapter;

import javaawteventActionListener;

import javaawteventActionEvent;

import javaxswingeventDocumentListener;

import javaxswingeventDocumentEvent;public class FrameTest extends JFrame implements DocumentListener{

BorderLayout borderLayout1 = new BorderLayout();

JPanel jPanel1 = new JPanel();

JPasswordField jPasswordField1 = new JPasswordField();

JTextField jTextField1 = new JTextField();

JMenuBar jMenuBar1 = new JMenuBar();

JMenu jMenu1 = new JMenu();

JMenu jMenu2 = new JMenu();

JMenu jMenu3 = new JMenu();

JMenuItem jMenuItem1 = new JMenuItem();

JMenuItem jMenuItem2 = new JMenuItem();

JLabel jLabel1 = new JLabel();

JLabel jLabel2 = new JLabel(); public FrameTest() {

try {

jbInit();

} catch (Exception exception) {

exceptionprintStackTrace();

}

} private void jbInit() throws Exception {

jMenu1setText("文件");

jMenu2setText("编辑");

jMenu2addMouseListener(new MouseAdapter() {

public void mouseClicked(MouseEvent e) {

JOptionPaneshowMessageDialog(null, "菜单“编辑”被点击");

}

}); jMenu3setText("帮助");

jMenu3addMouseListener(new MouseAdapter() {

public void mouseClicked(MouseEvent e) {

JOptionPaneshowMessageDialog(null, "菜单“帮助”被点击");

}

}); jMenuItem1setText("打开");

jMenuItem1addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

JOptionPaneshowMessageDialog(null, "菜单项“打开”被点击");

}

});

jMenuItem2setText("退出");

jMenuItem2addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

JOptionPaneshowMessageDialog(null, "菜单项“退出”被点击,系统关闭");

Systemexit(0);

}

});

jLabel1setText("密码:");

jLabel1setBounds(new Rectangle(43, 75, 42, 15));

jLabel2setText("显示密码:");

jLabel2setBounds(new Rectangle(35, 128, 82, 15)); jPanel1add(jPasswordField1);

jPanel1add(jTextField1);

jPanel1add(jLabel1);

jPanel1add(jLabel2);

jMenuBar1add(jMenu1);

jMenuBar1add(jMenu2);

jMenuBar1add(jMenu3);

jMenu1add(jMenuItem1);

jMenu1add(jMenuItem2);

jTextField1setText("");

jTextField1setBounds(new Rectangle(156, 123, 135, 25));

jPasswordField1setText("");

jPasswordField1getDocument()addDocumentListener(this);

jPasswordField1setBounds(new Rectangle(156, 70, 135, 25));

jPanel1setLayout(null); thissetJMenuBar(jMenuBar1);

thisgetContentPane()add(jPanel1, javaawtBorderLayoutCENTER);

thissetSize(500, 300);

Dimension screenSize = ToolkitgetDefaultToolkit()getScreenSize();

Dimension frameSize = thisgetSize();

if (frameSizeheight > screenSizeheight) {

frameSizeheight = screenSizeheight;

}

if (frameSizewidth > screenSizewidth) {

frameSizewidth = screenSizewidth;

}

thissetLocation((screenSizewidth - frameSizewidth) / 2,

(screenSizeheight - frameSizeheight) / 2); thissetResizable(false);

thissetVisible(true);

thissetDefaultCloseOperation(EXIT_ON_CLOSE);

} public void changedUpdate(DocumentEvent e) {

jTextField1setText(StringvalueOf(jPasswordField1getPassword()));

} public void insertUpdate(DocumentEvent e) {

jTextField1setText(StringvalueOf(jPasswordField1getPassword()));

} public void removeUpdate(DocumentEvent e) {

jTextField1setText(StringvalueOf(jPasswordField1getPassword()));

}

public static void main(String[] args) {

new FrameTest();

}

}

步骤:

1、 引包

import javaawt; import javaxswing;

2、 继承JFrame

public class Demo3 extends JFrame {}

3、 定义需要的组件

//3定义组件

JButton jb1,jb2,jb3,jb4,jb5; int size=9;

JButton jbs[]=new JButton[size]; //先分配初始空间 4、 创建组件

//4创建组件 jb1=new JButton("中部"); //创建组件

for(int i=0;i<size;i++) {

jbs[i]=new JButton(StringvalueOf(i));

}

5、 设置布局管理器

//设置布局管理器,默认是的边界布局管理器 thissetLayout(new FlowLayout(FlowLayoutLEFT));//流式布局 thisadd(jb1,BorderLayoutCENTER); //边界布局 thissetLayout(new GridLayout(3,3,10,10)); //网格布局

thissetLayout(null); //取消布局管理器

6、 添加组件

//添加组件 thisadd(jb1);

thisadd(jb2);

7、 设置窗体属性

//设置窗体属性 thissetTitle("流式布局案例"); //设置窗体标题 thissetSize(300, 200); //设置窗体大小 thissetLocation(200, 400); //设置初始位置

thissetDefaultCloseOperation(JFrameEXIT_ON_CLOSE); //关闭窗体时关闭虚拟机

//禁止用户改变窗体大小

thissetResizable(false); //7显示窗体

thissetVisible(true);

8、 显示窗体

利用数组创建组件:

//定义组件 int size=9;

JButton jbs[]=new JButton[size]; //数组要先分配初始空间 //创建组件

for(int i=0;i<size;i++) { jbs[i]=new JButton(StringvalueOf(i));

}

//添加组件 for(int i=0;i<size;i++) { thisadd(jbs[i]);

首先,进程间通信和是否是控制台/GUI程序应当没有任何关系。实际上,很多 *** 作系统压根就没有控制台/GUI程序的区别。

然后,调用外部程序也不需要(通常意义上的)进程间通信。C标准库就提供了最简陋的函数,用于直接启动另一个外部进程。 *** 作系统通常会提供更完备的API,用于启动另一个程序。

关键代码

31 获取系统注销信息

方法:通过与MicrosoftWin32SessionEndingEventHandler建关关联获取系统注销信息

protected override void OnStart(string[] args)

{

//获取系统注销,关机

MicrosoftWin32SystemEventsSessionEnding += new MicrosoftWin32SessionEndingEventHandler(thisSystemEvents_SessionEnding);

}

protected override void OnStop()

{

// TODO: 在此处添加代码以执行停止服务所需的关闭 *** 作。

//切记在服务停止时,移除事件

MicrosoftWin32SystemEventsSessionEnding -= new MicrosoftWin32SessionEndingEventHandler(thisSystemEvents_SessionEnding);

}

32 检查并启动Windows服务

/// <summary>

/// 启动已暂停或停止的服务

/// </summary>

private void StartService()

{

try

{

foreach (string serviceName in rwCnfgGsServiceNames)

{

ServiceController myService = new ServiceController(serviceName);

ServiceControllerStatus status = myServiceStatus;

switch (status)

{

case ServiceControllerStatusContinuePending:

break;

case ServiceControllerStatusPausePending:

break;

case ServiceControllerStatusStartPending:

break;

case ServiceControllerStatusRunning:

break;

case ServiceControllerStatusPaused:

case ServiceControllerStatusStopped:

{

myServiceStart();

myServiceWaitForStatus(ServiceControllerStatusRunning, new TimeSpan(0, 2, 0));

CommonwLog("完成启动服务: " + myServiceServiceName + "  " + SystemDateTimeNowToString());

}

break;

case ServiceControllerStatusStopPending:

break;

default:

break;

}

}

}

catch (Exception err)

{

CommonwLog(errToString());

}

}

32 检查并启动对应GUI程序

/// <summary>

/// 启动所有要启动的程序

/// </summary>

private void StartProgram()

{

try

{

foreach (string ProgramPath in rwCnfgGsProgramPaths)

{

string fileName = "";

//fileName = SystemIOPathGetFileName(ProgramPath); //文件名

//string ext = SystemIOPathGetExtension(ProgramPath); //扩展名

fileName = SystemIOPathGetFileNameWithoutExtension(ProgramPath);// fileNameReplace(ext, "");

if (!IsExistProcess(fileName))

{

ProcessStartInfo startInfo = new ProcessStartInfo(ProgramPath);

startInfoWindowStyle = ProcessWindowStyleNormal;

ProcessStart(startInfo);

CommonwLog("完成启动程序: " + fileName + ",完整路径:" + ProgramPath + "  " + SystemDateTimeNowToString());

SystemThreadingThreadSleep(3  1000); //间隔3秒;

}

}

}

catch (Exception err)

{

CommonwLog(errToString());

}

}

/// <summary>

/// 检查该进程是否已启动

/// </summary>

/// <param name="processName"></param>

/// <returns></returns>

private bool IsExistProcess(string processName)

{

Process[] MyProcesses = ProcessGetProcesses();

foreach (Process MyProcess in MyProcesses)

{

if (MyProcessProcessNameCompareTo(processName) == 0)

{

return true;

}

}

return false;

}

33 为当前Windows服务设置可与桌面交互选项

为"serviceProcessInstaller1" 的 Committed 事件添加以下 *** 作:

(注意引入 SystemManagement 命名空间)

private void serviceProcessInstaller1_Committed(object sender, InstallEventArgs e)

{

try

{

ConnectionOptions myConOptions = new ConnectionOptions();

myConOptionsImpersonation = ImpersonationLevelImpersonate;

ManagementScope mgmtScope = new SystemManagementManagementScope(@"root\CIMV2", myConOptions);

mgmtScopeConnect();

ManagementObject wmiService = new ManagementObject("Win32_ServiceName='" + serviceInstaller1ServiceName + "'");

ManagementBaseObject InParam = wmiServiceGetMethodParameters("Change");

InParam["DesktopInteract"] = true;

ManagementBaseObject OutParam = wmiServiceInvokeMethod("Change", InParam, null);

}

catch (Exception err)

{

CommonwLog(errToString());

}

}

执行效果: (不再需要手动去设置)

以上就是关于用java程序设计, 编写GUI程序全部的内容,包括:用java程序设计, 编写GUI程序、如何开发JAVA的GUI程序、用户通过GUI如何启动程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

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

原文地址: https://outofmemory.cn/zz/9471708.html

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

发表评论

登录后才能评论

评论列表(0条)

保存