java自定义窗体_java 自定义窗口

简介

简单 java 核心编程

code

/*

* @Author: your name

* @Date: 2020-11-08 14:44:58

* @LastEditTime: 2020-11-08 14:45:24

* @LastEditors: your name

* @Description: In User Settings Edit

* @FilePath: /java/dialog/AboutDialog.java

*/

package dialog;

import java.awt.BorderLayout;

import javax.swing.JButton;

import javax.swing.JDialog;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

/**

* A sample modal dialog that displays a message and waits for the user to click

* the OK button.

*/

public class AboutDialog extends JDialog {

public AboutDialog(JFrame owner) {

super(owner, "About DialogTest", true);

// add HTML label to center

add(new JLabel("

Core Java


By Cay Horstmann"), BorderLayout.CENTER);

// OK button closes the dialog

var ok = new JButton("OK");

ok.addActionListener(event -> setVisible(false));

// add OK button to southern border

var panel = new JPanel();

panel.add(ok);

add(panel, BorderLayout.SOUTH);

pack();

}

}

package dialog;

import javax.swing.JFrame;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

/**

* A frame with a menu whose File->About action shows a dialog.

*/

public class DialogFrame extends JFrame {

private static final int DEFAULT_WIDTH = 300;

private static final int DEFAULT_HEIGHT = 200;

private AboutDialog dialog;

public DialogFrame() {

setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

// construct a File menu

var menuBar = new JMenuBar();

setJMenuBar(menuBar);

var fileMenu = new JMenu("File");

menuBar.add(fileMenu);

// add About and Exit menu items

// the About item shows the About dialog

var aboutItem = new JMenuItem("About");

aboutItem.addActionListener(event -> {

if (dialog == null) // first time

dialog = new AboutDialog(DialogFrame.this);

dialog.setVisible(true); // pop up dialog

});

fileMenu.add(aboutItem);

// the Exit item exits the program

var exitItem = new JMenuItem("Exit");

exitItem.addActionListener(event -> System.exit(0));

fileMenu.add(exitItem);

}

}

/*

* @Author: your name

* @Date: 2020-11-08 14:44:58

* @LastEditTime: 2020-11-08 14:45:17

* @LastEditors: your name

* @Description: In User Settings Edit

* @FilePath: /java/dialog/DialogTest.java

*/

package dialog;

import java.awt.*;

import javax.swing.*;

/**

* @version 1.35 2018-04-10

* @author Cay Horstmann

*/

public class DialogTest {

public static void main(String[] args) {

EventQueue.invokeLater(() -> {

var frame = new DialogFrame();

frame.setTitle("DialogTest");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

});

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值