大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
java做窗口的话,需要用swing技术,之后创建JFrame 等组件,即可完成窗口创建工作。
成都创新互联主要从事成都网站建设、成都网站设计、网页设计、企业做网站、公司建网站等业务。立足成都服务洪湖,10年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18982081108
package inter.frame;import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;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("取消"); m.add(mi1);
m.add(mi2);
mb.add(m);
frame.add(mb, BorderLayout.NORTH);
frame.setSize(300, 300); //设置窗口大小
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置退出时关闭窗口
frame.setVisible(true);//设置窗口可见
} public void initAction() {
mi1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 具体实现代码根据实际要求填写
System.out.println("click");
JOptionPane.showMessageDialog(null, "你点击了确定按钮");
}
});
mi2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 具体实现代码根据实际要求填写
JOptionPane.showMessageDialog(null, "你点击了取消按钮");
}
});
} public static void main(String[] args) {
new MenuTest();//执行菜单创建
}}
package image;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class PanelRunner extends JFrame
{
private static final long serialVersionUID = 1L;
private static void initPanels ( JFrame pr )
{
for ( int i = 0; i 5; i++ )
{
JPanel panel = new JPanel ();
switch (i)
{
case 0:
panel.setBackground (Color.RED);
panel.setPreferredSize (new Dimension (200, 100));
pr.add (panel, BorderLayout.NORTH);
break;
case 1:
panel.setBackground (Color.YELLOW);
panel.setPreferredSize (new Dimension (200, 300));
pr.add (panel, BorderLayout.EAST);
break;
case 2:
panel.setBackground (Color.ORANGE);
panel.setPreferredSize (new Dimension (200, 100));
pr.add (panel, BorderLayout.SOUTH);
break;
case 3:
panel.setBackground (Color.WHITE);
panel.setPreferredSize (new Dimension (200, 300));
pr.add (panel, BorderLayout.WEST);
break;
case 4:
pr.add (panel, BorderLayout.CENTER);
panel.setPreferredSize (new Dimension (200, 100));
panel.setLayout (new GridLayout (1, 2));
for ( int j = 0; j 2; j++ )
{
JPanel subPanel = new JPanel ();
subPanel.setPreferredSize (new Dimension (200, 100));
Color color = j == 0 ? Color.BLUE : Color.GREEN;
subPanel.setBackground (color);
panel.add (subPanel);
}
break;
default:
break;
}
}
}
private static void initFrame ( JFrame pr )
{
pr.setLayout (new BorderLayout ());
pr.setSize (600, 300);
pr.setLocationRelativeTo (null);
pr.setResizable (false);
pr.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
public static void main ( String[] args )
{
SwingUtilities.invokeLater (new Runnable ()
{
@Override
public void run ()
{
PanelRunner pr = new PanelRunner ();
initFrame (pr);
initPanels (pr);
pr.setVisible (true);
}
});
}
}
编写一数据报通信程序 实现简单的聊天功能
聊天内容 和 输入文本 分别为当前聊天的历史信息和当前要传送出去的聊天文本 确定 清空 退出 三个按钮分别实现发送当前聊天文本 清空当前聊天文本和退出系统的功能 import java awt Font;
import java awt event ActionEvent;
import java awt event ActionListener;
import java awt event WindowEvent;
import java awt event WindowListener;
import DatagramPacket;
import DatagramSocket;
import InetAddress;
import SocketException;
import javax swing JButton;
import javax swing JFrame;
import javax swing JLabel;
import javax swing JScrollBar;
import javax swing JScrollPane;
import javax swing JTextArea;
import javax swing JTextField;
public class Frame extends JFrame implements WindowListener{
private JTextArea text;
private JTextField ipText;
private JTextField sendText;
private JButton button;
private JButton button ;
private JButton button ;
private DatagramSocket socket;
private JScrollBar vsBar;
public Frame(){
setTitle( 聊天器 );
setBounds( );
text=new JTextArea();
text setEditable(true);
setLayout(null);
JScrollPane textPanel = new JScrollPane(text);
vsBar = textPanel getVerticalScrollBar();
textPanel setBounds( );
getContentPane() add(textPanel);
JLabel label=new JLabel( 请输入对方IP );
label setFont(new Font( Font BOLD ));
label setBounds( );
getContentPane() add(label);
ipText = new JTextField();
ipText setBounds( );
getContentPane() add(ipText);
button=new JButton();
button setText( 确定 );
button setBounds( );
button setFont(new Font( Font BOLD ));
getContentPane() add(button);
button addActionListener(new send());
button =new JButton( 清空 );
button setBounds( );
button setFont(new Font( Font BOLD ));
getContentPane() add(button );
button addActionListener(new clear());
button =new JButton( 退出 );
button setBounds( );
button setFont(new Font( Font BOLD ));
getContentPane() add(button );
button addActionListener(new exit());
this addWindowListener(this);
sendText = new JTextField();
sendText setBounds( );
getContentPane() add(sendText);
//server();
pack();
setVisible(true);
}
class send implements ActionListener{
public void actionPerformed(ActionEvent e) {
try{
String ip=ipText getText();
InetAddress address=InetAddress getByName(ip);
byte[] data=sendText getText() getBytes();
DatagramPacket dp=new DatagramPacket(data data length address );
String myip=InetAddress getLocalHost() getHostAddress();
text append(myip+ :\n +sendText getText()+ \n );
socket send(dp);
sendText setText(null);
}catch(Exception e ){
System out println(e );
}
}
}
class clear implements ActionListener{
public void actionPerformed(ActionEvent e) {
text setText( );
}
}
class exit implements ActionListener{
public void actionPerformed(ActionEvent e) {
System exit( );
}
}
private void server() {
try {
socket=new DatagramSocket( );
byte[] buf=new byte[ ];
final DatagramPacket dp =new DatagramPacket(buf buf length);
Runnable runnable=new Runnable(){
public void run(){
while(true){
try{
Thread sleep( );
socket receive(dp );
String message=new String(dp getData() dp getLength());
String ip=dp getAddress() getHostAddress();
if(!InetAddress getLocalHost() getHostAddress() equals(ip))
text append(ip+ :\n +message+ \n );
}catch(Exception e){
System out println(e);
}
}
}
};
new Thread(runnable) start();
} catch (SocketException e ) {
e printStackTrace();
}
}
public static void main(String[] args) {
Frame frame=new Frame();
}
public void windowActivated(WindowEvent e) {
// TODO Auto generated method stub
}
public void windowClosed(WindowEvent e) {
// TODO Auto generated method stub
}
public void windowClosing(WindowEvent e) {
// TODO Auto generated method stub
System exit( );
}
public void windowDeactivated(WindowEvent e) {
// TODO Auto generated method stub
}
public void windowDeiconified(WindowEvent e) {
// TODO Auto generated method stub
}
public void windowIconified(WindowEvent e) {
// TODO Auto generated method stub
}
public void windowOpened(WindowEvent e) {
// TODO Auto generated method stub
}
lishixinzhi/Article/program/Java/hx/201311/25580
图片看起来很模糊,隐约看到需要一个登录窗口,那就分享一下以前练习的登录窗口demo吧。
先上效果图:
登录界面
源码如下:
AbsoluteLoginFrame.java
public class AbsoluteLoginFrame extends JFrame {
private static final int LOGIN_WIDTH = 600;
private static final int LOGIN_HEIGHT = 400;
private static final long serialVersionUID = -2381351968820980500L;
public AbsoluteLoginFrame(){
//设置窗口标题
setTitle("登录界面");
//设置一个初始面板,填充整个窗口
JPanel loginPanel = new JPanel();
//设置背景颜色
loginPanel.setBackground(new Color(204, 204, 204));//#CCC
loginPanel.setLayout(null);
JPanel centerPanel = new JPanel();
centerPanel.setBackground(Color.WHITE);
centerPanel.setBounds(114, 70, 360, 224);
centerPanel.setLayout(null);
JLabel jLabel = new JLabel("用户名:");
jLabel.setOpaque(true);
jLabel.setBackground(Color.YELLOW);
jLabel.setBounds(60, 60, 54, 20);
JLabel label = new JLabel("密 码:");
label.setOpaque(true);
label.setBackground(Color.CYAN);
label.setBounds(60, 90, 54, 20);
JTextField textField = new JTextField(15);
textField.setBounds(130, 60, 166, 21);
JPasswordField passwordField = new JPasswordField(15);
passwordField.setBounds(130, 90, 166, 21);
JButton jButton = new JButton("登录");
jButton.setBounds(148, 120, 62, 28);
centerPanel.add(jLabel);
centerPanel.add(label);
centerPanel.add(textField);
centerPanel.add(jButton);
centerPanel.add(passwordField);
loginPanel.add(centerPanel);
getContentPane().add(loginPanel);//将初始面板添加到窗口中
setSize(LOGIN_WIDTH, LOGIN_HEIGHT);//设置窗口大小
setLocation(Screen.getCenterPosition(LOGIN_WIDTH, LOGIN_HEIGHT));//设置窗口位置
setDefaultCloseOperation(EXIT_ON_CLOSE);//设置窗口默认关闭方式
setResizable(false);
setVisible(true);
}
public static void main(String[] args) {
new AbsoluteLoginFrame();
}
}
Screen.java
public class Screen {
private int width;
private int height;
public Screen(){
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenSize = toolkit.getScreenSize();
this.width = screenSize.width;
this.height = screenSize.height;
}
public static Point getCenterPosition(int width, int height){
Screen screen = new Screen();
int x = (screen.getWidth() - width) / 2;
int y = (screen.getHeight() - height) / 2;
return new Point(x, y);
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
}
package window;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Window extends JFrame{
public static int num = 0;
public static void main(String[] args) {
new Window();
}
public Window(){
//设置x y坐标
this.setLocation(400, 500);
//设置窗口大小
this.setSize(512, 512);
//设置窗口可见
this.setVisible(true);
//设置窗口关闭后程序停止
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//设置窗口不可移动
this.setResizable(false);
JButton but1 = new JButton("按钮");
final JTextField text1 = new JTextField(10);