import?java.awt.*;

10多年的米東網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。營銷型網(wǎng)站的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整米東建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。成都創(chuàng)新互聯(lián)公司從事“米東網(wǎng)站設(shè)計”,“米東網(wǎng)站推廣”以來,每個客戶項目都認(rèn)真落實執(zhí)行。
import?java.awt.event.*;
import?javax.swing.*;
public?class?JListDemo?extends?JFrame?{
private JPanel topPanel;
private JList listbox;
public?JListDemo(){
setTitle(?"Simple?ListBox?Application"?);
setSize(?300,?100?);
setBackground(?Color.gray?);
topPanel?=?new?JPanel();
topPanel.setLayout(?new?BorderLayout()?);
getContentPane().add(?topPanel?);
String listData[]?=
{
"Item?1",
"Item?2",
"Item?3",
"Item?4"
};
listbox?=?new?JList(?listData?);
topPanel.add(?listbox,?BorderLayout.CENTER?);
}
public?static?void?main(?String?args[]?)?{
JListDemo?mainFrame =?new?JListDemo();
mainFrame.setVisible(?true?);
}
}
一: 首先弄清題目的意思
A.需要的主要組件列表:
1. ?創(chuàng)建一個窗口,窗口標(biāo)題叫Information
2. ?3個標(biāo)簽, 用于顯示文字 Name Number Class
3. ?3個文本框, 用于填寫信息
4. ?1個按鈕, ?文字是確認(rèn)
5. ?1個文本域
B.業(yè)務(wù)邏輯
1. 當(dāng)點擊按鈕確認(rèn)的時候, 把 文本框的信息顯示到文本域
C.設(shè)計的主要技術(shù)
JLabel , JButton, JTextField ...等, 都是swing的組件 , ?所以應(yīng)該使用swing進行創(chuàng)建
二: ?確定使用的布局
swing雖然重寫了大部分的組件, 但是布局, 依舊沿襲awt技術(shù)
分析圖片上的布局:
至少有2種方法可以實現(xiàn),?
方法一: 絕對布局 , 優(yōu)點: ?配合可視化GUI拖曳, 可以完美的實現(xiàn)圖上的組件的位置
但是缺點也是致命的, 不同的操作系統(tǒng)平臺下, 可能會出現(xiàn)位置的移動,
只適合開發(fā)平臺, 移植效果差 . ?所以不推薦使用
方法二: 靈活的表格布局, 配合流式布局 , 所有操作系統(tǒng)下,顯示效果都比較統(tǒng)一.?
三: 效果圖
四: 參考代碼
import?java.awt.*;
import?java.awt.event.*;
import?javax.swing.*;
public?class?FrameDemo?extends?JFrame?{
//申明需要的組件
private?final?JTextField?jtf1,jtf2,jtf3;
private?final?JTextArea?jta;
public?FrameDemo()?{
setTitle("Information");//設(shè)置窗口標(biāo)題
setSize(320,?360);//設(shè)置窗口大小
setLocationRelativeTo(null);//設(shè)置窗口居中
setDefaultCloseOperation(EXIT_ON_CLOSE);//設(shè)置關(guān)閉時退出虛擬機
getContentPane().setLayout(new?FlowLayout());//設(shè)置窗口布局為流式布局
JPanel?jp?=?new?JPanel(new?GridLayout(4,?2));//設(shè)置jp面板為表格布局4行2列
//第一行
JPanel?jp01?=?new?JPanel();
JLabel?jl1?=?new?JLabel("Name:");
jp01.add(jl1);
JPanel?jp1?=?new?JPanel();
jtf1?=?new?JTextField(8);
jp1.add(jtf1);
//第二行
JPanel?jp02?=?new?JPanel();
JLabel?jl2?=?new?JLabel("Number:");
jp02.add(jl2);
JPanel?jp2?=?new?JPanel();
jtf2?=?new?JTextField(8);
jp2.add(jtf2);
//第三行
JPanel?jp03?=?new?JPanel();
JLabel?jl3?=?new?JLabel("Class:");
jp03.add(jl3);
JPanel?jp3?=?new?JPanel();
jtf3?=?new?JTextField(8);
jp3.add(jtf3);
//第四行
JPanel?jp04?=?new?JPanel();
JLabel?jl4?=?new?JLabel("");
jp04.add(jl4);
JPanel?jp4?=?new?JPanel();
JButton?jb?=?new?JButton("確認(rèn)");
jp4.add(jb);
jp.add(jp01);
jp.add(jp1);
jp.add(jp02);
jp.add(jp2);
jp.add(jp03);
jp.add(jp3);
jp.add(jp04);
jp.add(jp4);
getContentPane().add(jp);
jta?=?new?JTextArea();
jta.setColumns(20);//設(shè)置文本域的大小
jta.setEditable(false);//設(shè)置文本域不可編輯
jta.setBackground(jp.getBackground());//設(shè)置文本域的背景色和面板一樣
getContentPane().add(jta);
jb.addActionListener(new?ActionListener()?{//給按鈕添加事件
public?void?actionPerformed(ActionEvent?e)?{//點擊按鈕,顯示信息到文本域
String?name?=?jtf1.getText();
String?number?=?jtf2.getText();
String?clazz?=?jtf3.getText();
jta.setText("You?name?is?"+name+"?number?is?"+number+"?class?is?"+clazz);
}
});
}
public?static?void?main(String[]?args)?{
new?FrameDemo().setVisible(true);//創(chuàng)建窗口,被設(shè)置為可見
}
}
五: 拓展
雖然圖形界面的實現(xiàn)方法是多樣的, ?我們一定要根據(jù)具體情況, 選擇一個比較優(yōu)化的 合理的, 符合業(yè)務(wù)邏輯的實現(xiàn)方法
package hao;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.io.File;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
public class ChatPanel extends JPanel {
private static final long serialVersionUID = 1L;
JButton send,record,saveRecord,image;
JTextArea inputArea;
JTextPane text;//注意用法****************************************************************************
JComboBox fontName = null, fontSize = null, fontStyle = null, fontColor = null,fontBackColor = null;
public StyledDocument doc = null; JScrollPane scrollPane;JPanel textChat;
JButton music;
public ChatPanel() {
setLayout(new BorderLayout());
text = new JTextPane();
text.setEditable(false);
doc = text.getStyledDocument();//跟蹤文本和圖片寫到該區(qū)域的位置*************************************
scrollPane = new JScrollPane(text);
//注意下面對JComboBox的巧用***********************************************************************
String[] str_name = { "宋體", "黑體", "Dialog", "Gulim" };
String[] str_Size = { "12", "14", "18", "22", "30", "40" };
String[] str_Style = { "常規(guī)", "斜體", "粗體", "粗斜體" };
String[] str_Color = { "黑色", "紅色", "藍色", "黃色", "綠色" };
String[] str_BackColor = { "無色", "灰色", "淡紅", "淡藍", "淡黃", "淡綠" };
fontName = new JComboBox(str_name);
fontSize = new JComboBox(str_Size);
fontStyle = new JComboBox(str_Style);
fontColor = new JComboBox(str_Color);
fontBackColor = new JComboBox(str_BackColor);
fontName.setBackground(new Color(255,153,255));
fontSize.setBackground(new Color(255,153,255));
fontStyle.setBackground(new Color(255,153,255));
fontColor.setBackground(new Color(255,153,255));
fontBackColor.setBackground(new Color(255,153,255));
Box box = Box.createVerticalBox();//創(chuàng)建一個可以容納多個Box組件的Box*******************************
Box box_1 = Box.createHorizontalBox();
Box box_2 = Box.createHorizontalBox();
Box box_4 = Box.createHorizontalBox();
box.add(box_1);
box.add(box_2);
box.add(box_4);
JLabel b1= new JLabel("字體~~"), b2 = new JLabel("樣式~~"),b3 = new JLabel("字號~~"),b4 = new JLabel("顏色~~"),b5 = new JLabel("背景~~");
b1.setBackground(new Color(255,153,255));
b2.setBackground(new Color(255,153,255));
b3.setBackground(new Color(255,153,255));
b4.setBackground(new Color(255,153,255));
b5.setBackground(new Color(255,153,255));
box_1.add(b1);
box_1.add(fontName);
box_1.add(Box.createHorizontalStrut(8));
box_1.add(b2);
box_1.add(fontStyle);
box_1.add(Box.createHorizontalStrut(8));
box_1.add(b3);
box_1.add(fontSize);
box_2.add(Box.createHorizontalStrut(8));
box_2.add(b4);
box_2.add(fontColor);
box_2.add(Box.createHorizontalStrut(8));
box_4.add(b5);
box_4.add(fontBackColor);
textChat = new JPanel();
textChat.setLayout(new BorderLayout());
textChat.setBackground(new Color(255,153,255));
inputArea = new JTextArea(3, 20);
inputArea.setLineWrap(true); //設(shè)置文本區(qū)的換行策略。88888*********************************
send = new JButton("發(fā)送");
record=new JButton("顯示記錄");
saveRecord=new JButton("儲存記錄");
image=new JButton("表情");
send.setBackground(new Color(255,153,255));
record.setBackground(new Color(255,153,255));
saveRecord.setBackground(new Color(255,153,255));
image.setBackground(new Color(255,153,255));
Box box_3 = Box.createHorizontalBox();
box_3.add(send); box_3.add(Box.createHorizontalStrut(8));//設(shè)置按鈕間距*************************888
box_3.add(record); box_3.add(Box.createHorizontalStrut(8)); //設(shè)置按鈕間距*************************888
box_3.add(saveRecord); box_3.add(Box.createHorizontalStrut(8));//設(shè)置按鈕間距*************************888
box_3.add(image);
box.setBorder(BorderFactory.createLineBorder(new Color(102,102,0),5));//設(shè)置Box的邊框線********************
box_3.setBorder(BorderFactory.createLineBorder(new Color(102,102,0),5));
textChat.add(box,BorderLayout.NORTH);
textChat.add(inputArea,BorderLayout.CENTER);
textChat.add(box_3, BorderLayout.SOUTH);
inputArea.requestFocus(true);
inputArea.setBorder(BorderFactory.createLineBorder(new Color(102,102,0),5));//設(shè)置輸入窗口邊框線*******************
text.setBorder(BorderFactory.createLineBorder(new Color(102,102,0),8));//設(shè)置輸入窗口邊框線*******************
JPanel audioPanel = new JPanel();//最上面的邊框************************************************************************
audioPanel.setBackground(new Color(255,153,255));
audioPanel.setLayout(new GridLayout(1,1));
music = new JButton("想聽就聽");
music.setPreferredSize(new Dimension(320,50));
music.setBorder(BorderFactory.createLineBorder(Color.BLACK,10));//設(shè)置輸入窗口邊框線*******************
audioPanel.add(music);
add(audioPanel, BorderLayout.NORTH);
add(scrollPane,BorderLayout.CENTER);
add(textChat, BorderLayout.SOUTH);
}
void insertIcon(ImageIcon image) {
text.setCaretPosition(doc.getLength());
text.insertIcon(image);
insert(new MessageStyle());//?????????????????????????????????????????????????????????????????????????????/
}
public void insert(MessageStyle attrib) {
try {
doc.insertString(doc.getLength(), attrib.getText() + "\n", attrib.getAttrSet());//寫完后接著換行************
} catch (BadLocationException e) {
e.printStackTrace();
}
}
public MessageStyle getMessageStyle(String line) {
MessageStyle att = new MessageStyle();
att.setText(line);
att.setName((String) fontName.getSelectedItem());
att.setSize(Integer.parseInt((String) fontSize.getSelectedItem()));
String temp_style = (String) fontStyle.getSelectedItem();
if (temp_style.equals("常規(guī)")) {
att.setStyle(MessageStyle.GENERAL);
}
else if (temp_style.equals("粗體")) {
att.setStyle(MessageStyle.BOLD);
}
else if (temp_style.equals("斜體")) {
att.setStyle(MessageStyle.ITALIC);
}
else if (temp_style.equals("粗斜體")) {
att.setStyle(MessageStyle.BOLD_ITALIC);
}
String temp_color = (String) fontColor.getSelectedItem();
if (temp_color.equals("黑色")) {
att.setColor(new Color(0, 0, 0));
}
else if (temp_color.equals("紅色")) {
att.setColor(new Color(255, 0, 0));
}
else if (temp_color.equals("藍色")) {
att.setColor(new Color(0, 0, 255));
}
else if (temp_color.equals("黃色")) {
att.setColor(new Color(255, 255, 0));
}
else if (temp_color.equals("綠色")) {
att.setColor(new Color(0, 255, 0));
}
String temp_backColor = (String) fontBackColor.getSelectedItem();
if (!temp_backColor.equals("無色")) {
if (temp_backColor.equals("灰色")) {
att.setBackColor(new Color(200, 200, 200));
}
else if (temp_backColor.equals("淡紅")) {
att.setBackColor(new Color(255, 200, 200));
}
else if (temp_backColor.equals("淡藍")) {
att.setBackColor(new Color(200, 200, 255));
}
else if (temp_backColor.equals("淡黃")) {
att.setBackColor(new Color(255, 255, 200));
}
else if (temp_backColor.equals("淡綠")) {
att.setBackColor(new Color(200, 255, 200));
}
}
return att;
}
}
import?java.awt.Dimension;
import?java.awt.Toolkit;
import?java.awt.event.ActionEvent;
import?java.awt.event.ActionListener;
import?javax.swing.JButton;
import?javax.swing.JFrame;
import?javax.swing.JLabel;
import?javax.swing.JOptionPane;
import?javax.swing.JPasswordField;
import?javax.swing.JTextField;
public?class?Test26?{
public?static?void?main(String[]?args)?{
final?String?userName?=?"abc";
final?String?passwrod?=?"123";
JFrame?jFrame?=?new?JFrame("登陸界面");
Dimension?dimension?=?Toolkit.getDefaultToolkit().getScreenSize();
jFrame.setBounds(((int)dimension.getWidth()?-?200)?/?2,?((int)dimension.getHeight()?-?300)?/?2,?200,?150);
jFrame.setResizable(false);
jFrame.setLayout(null);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel?label1?=?new?JLabel("姓名");
label1.setBounds(10,?10,?100,?30);
jFrame.add(label1);
JLabel?label2?=?new?JLabel("密碼");
label2.setBounds(10,?40,?100,?30);
jFrame.add(label2);
final?JTextField?text1?=?new?JTextField();
text1.setBounds(50,?15,?130,?20);
jFrame.add(text1);
final?JPasswordField?text2?=?new?JPasswordField();
text2.setBounds(50,?45,?130,?20);
jFrame.add(text2);
JButton?button?=?new?JButton("Login");
button.setBounds(10,?75,?170,?40);
button.addActionListener(new?ActionListener()?{
@Override
public?void?actionPerformed(ActionEvent?e)?{
if(userName.equals(text1.getText())??passwrod.equals(text2.getText()))?{
JOptionPane.showMessageDialog(null,?"登陸成功誤",?"提示",?JOptionPane.INFORMATION_MESSAGE);
}?else?{
JOptionPane.showMessageDialog(null,?"錯誤",?"提示",?JOptionPane.ERROR_MESSAGE);
text1.setText("");
text2.setText("");
}
}
});
jFrame.add(button);
jFrame.setVisible(true);
}
}
我有一個微信公眾號,經(jīng)常會分享一些Java技術(shù)相關(guān)的干貨,還有一些學(xué)習(xí)資源。
如果你喜歡我的分享,可以用微信搜索“Java團長”或者“javatuanzhang”關(guān)注。
import?java.awt.*;
import?java.awt.event.*;
import?java.io.*;
import?javax.swing.*;
public?class?ReadBook?extends?JFrame?{
JTextArea?jta;
JTextField?jtf;
JButton?jb;
public?ReadBook()?{
jta?=?new?JTextArea();
jtf?=?new?JTextField(30);
jtf.setText("文件保存路徑如c:\\ab.txt");
jb?=?new?JButton("保存文字");
JPanel?jp?=?new?JPanel();
jp.add(jtf);
jp.add(jb);
add(jta);
add(jp,?BorderLayout.SOUTH);
setBounds(500,?100,?500,?380);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
jb.addActionListener(new?ActionListener()?{
@Override
public?void?actionPerformed(ActionEvent?e)?{
//-------------核心代碼---------
String?path?=?jtf.getText();
File?f?=?new?File(path);
String?txt?=?jta.getText().replaceAll("\n",?"\r\n");
try?{
BufferedWriter?bw?=?new?BufferedWriter(new?FileWriter(f));
bw.write(txt);//寫入文件中
bw.close();
}?catch?(Exception?e1)?{
e1.printStackTrace();
}
//-------------核心代碼---------
}
});
}
public?static?void?main(String[]?args)?{
new?ReadBook();
}
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
class LoginFrm extends JFrame implements ActionListener
{
JLabel lbl1=new JLabel("用戶名");
JLabel lbl2=new JLabel("密碼");
JTextField txt=new JTextField(15);
JPasswordField pf=new JPasswordField();
JButton btn1=new JButton("確定");
JButton btn2=new JButton("取消");
public LoginFrm()
{
this.setTitle("登陸");
JPanel jp=(JPanel)this.getContentPane();
jp.setLayout(new GridLayout(3,2,10,10));
jp.add(lbl1);jp.add(txt);
jp.add(lbl2);jp.add(pf);
jp.add(btn1);jp.add(btn2);
btn1.addActionListener(this);
btn2.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==btn1)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:MyDB","","");
Statement cmd=con.createStatement();
ResultSet rs=cmd.executeQuery("select * from loginAndpassword where login='"+txt.getText()+"' and password='"+pf.getText()+"'");
if(rs.next())
{
JOptionPane.showMessageDialog(null,"登陸成功!");
}
else
JOptionPane.showMessageDialog(null,"用戶名或密碼錯誤!");
} catch(Exception ex){}
if(ae.getSource()==btn2)
{
txt.setText("");
pf.setText("");
}
}
}
public static void main(String arg[])
{
JFrame.setDefaultLookAndFeelDecorated(true);
LoginFrm frm=new LoginFrm();
frm.setSize(400,200);
frm.setVisible(true);
}
}
分享文章:java界面常用代碼 java主界面代碼
文章起源:http://chinadenli.net/article22/hgphcc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機網(wǎng)站建設(shè)、服務(wù)器托管、動態(tài)網(wǎng)站、做網(wǎng)站、網(wǎng)站維護、定制網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)