java做窗口的話,需要用swing技術(shù),之后創(chuàng)建JFrame 等組件,即可完成窗口創(chuàng)建工作。
成都創(chuàng)新互聯(lián)主要從事成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)洪湖,10年網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):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; //定義一個窗口架構(gòu)
JMenuBar mb;//定義窗口的菜單工具欄
JMenu m; //定義菜單
JMenuItem mi1;//定義菜單的內(nèi)容
JMenuItem mi2; //定義菜單的內(nèi)容
public MenuTest() {
initFrame();
initAction();
}
public void initFrame() {
frame = new JFrame();
mb = new JMenuBar();
m = new JMenu("學(xué)生查詢");
mi1 = new JMenuItem("確認(rèn)");
mi2 = new JMenuItem("取消"); m.add(mi1);
m.add(mi2);
mb.add(m);
frame.add(mb, BorderLayout.NORTH);
frame.setSize(300, 300); //設(shè)置窗口大小
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設(shè)置退出時關(guān)閉窗口
frame.setVisible(true);//設(shè)置窗口可見
} public void initAction() {
mi1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 具體實現(xiàn)代碼根據(jù)實際要求填寫
System.out.println("click");
JOptionPane.showMessageDialog(null, "你點擊了確定按鈕");
}
});
mi2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 具體實現(xiàn)代碼根據(jù)實際要求填寫
JOptionPane.showMessageDialog(null, "你點擊了取消按鈕");
}
});
} public static void main(String[] args) {
new MenuTest();//執(zhí)行菜單創(chuàng)建
}}
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);
}
});
}
}
編寫一數(shù)據(jù)報通信程序 實現(xiàn)簡單的聊天功能
聊天內(nèi)容 和 輸入文本 分別為當(dāng)前聊天的歷史信息和當(dāng)前要傳送出去的聊天文本 確定 清空 退出 三個按鈕分別實現(xiàn)發(fā)送當(dāng)前聊天文本 清空當(dāng)前聊天文本和退出系統(tǒng)的功能 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
圖片看起來很模糊,隱約看到需要一個登錄窗口,那就分享一下以前練習(xí)的登錄窗口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(){
? //設(shè)置窗口標(biāo)題
? setTitle("登錄界面");
? //設(shè)置一個初始面板,填充整個窗口
? JPanel loginPanel = new JPanel();
? //設(shè)置背景顏色
? 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);//設(shè)置窗口大小
? setLocation(Screen.getCenterPosition(LOGIN_WIDTH, LOGIN_HEIGHT));//設(shè)置窗口位置
? setDefaultCloseOperation(EXIT_ON_CLOSE);//設(shè)置窗口默認(rèn)關(guān)閉方式
? 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(){
//設(shè)置x y坐標(biāo)
this.setLocation(400, 500);
//設(shè)置窗口大小
this.setSize(512, 512);
//設(shè)置窗口可見
this.setVisible(true);
//設(shè)置窗口關(guān)閉后程序停止
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//設(shè)置窗口不可移動
this.setResizable(false);
JButton but1 = new JButton("按鈕");
final JTextField text1 = new JTextField(10);