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();//跟蹤文本和圖片寫到該區域的位置*************************************
scrollPane = new JScrollPane(text);
//注意下面對JComboBox的巧用***********************************************************************
String[] str_name = { "宋體", "黑體", "Dialog", "Gulim" };
String[] str_Size = { "12", "14", "18", "22", "30", "40" };
String[] str_Style = { "常規", "斜體", "粗體", "粗斜體" };
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();//創建一個可以容納多個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); //設置文本區的換行策略。88888*********************************
send = new JButton("發送");
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));//設置按鈕間距*************************888
box_3.add(record); box_3.add(Box.createHorizontalStrut(8)); //設置按鈕間距*************************888
box_3.add(saveRecord); box_3.add(Box.createHorizontalStrut(8));//設置按鈕間距*************************888
box_3.add(image);
box.setBorder(BorderFactory.createLineBorder(new Color(102,102,0),5));//設置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));//設置輸入窗口邊框線*******************
text.setBorder(BorderFactory.createLineBorder(new Color(102,102,0),8));//設置輸入窗口邊框線*******************
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));//設置輸入窗口邊框線*******************
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("常規")) {
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;
}
}
一: 首先弄清題目的意思
A.需要的主要組件列表:
1. ?創建一個窗口,窗口標題叫Information
2. ?3個標簽, 用于顯示文字 Name Number Class
3. ?3個文本框, 用于填寫信息
4. ?1個按鈕, ?文字是確認
5. ?1個文本域
B.業務邏輯
1. 當點擊按鈕確認的時候, 把 文本框的信息顯示到文本域
C.設計的主要技術
JLabel , JButton, JTextField ...等, 都是swing的組件 , ?所以應該使用swing進行創建
二: ?確定使用的布局
swing雖然重寫了大部分的組件, 但是布局, 依舊沿襲awt技術
分析圖片上的布局:
至少有2種方法可以實現,?
方法一: 絕對布局 , 優點: ?配合可視化GUI拖曳, 可以完美的實現圖上的組件的位置
但是缺點也是致命的, 不同的操作系統平臺下, 可能會出現位置的移動,
只適合開發平臺, 移植效果差 . ?所以不推薦使用
方法二: 靈活的表格布局, 配合流式布局 , 所有操作系統下,顯示效果都比較統一.?
三: 效果圖
四: 參考代碼
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");//設置窗口標題
setSize(320,?360);//設置窗口大小
setLocationRelativeTo(null);//設置窗口居中
setDefaultCloseOperation(EXIT_ON_CLOSE);//設置關閉時退出虛擬機
getContentPane().setLayout(new?FlowLayout());//設置窗口布局為流式布局
JPanel?jp?=?new?JPanel(new?GridLayout(4,?2));//設置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("確認");
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);//設置文本域的大小
jta.setEditable(false);//設置文本域不可編輯
jta.setBackground(jp.getBackground());//設置文本域的背景色和面板一樣
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);//創建窗口,被設置為可見
}
}
五: 拓展
雖然圖形界面的實現方法是多樣的, ?我們一定要根據具體情況, 選擇一個比較優化的 合理的, 符合業務邏輯的實現方法
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
public class LoginFrame {
private JFrame jFrame = null; // @jve:decl-index=0:visual-constraint="293,25"
private JPanel jContentPane = null;
private JPanel jPanel = null;
private JLabel jLabel = null;
private JLabel jLabel1 = null;
private JTextField jTextField = null;
private JTextField jTextField1 = null;
private JButton jButton = null;
/**
* This method initializes jFrame
*
* @return javax.swing.JFrame
*/
private JFrame getJFrame() {
if (jFrame == null) {
jFrame = new JFrame();
jFrame.setSize(new Dimension(330, 222));
jFrame.setTitle("登陸");
jFrame.setContentPane(getJContentPane());
}
return jFrame;
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJPanel(), null);
}
return jContentPane;
}
/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel() {
if (jPanel == null) {
jLabel1 = new JLabel();
jLabel1.setBounds(new Rectangle(45, 76, 54, 22));
jLabel1.setText("密 碼:");
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(47, 39, 53, 25));
jLabel.setText("用戶名:");
jPanel = new JPanel();
jPanel.setLayout(null);
jPanel.setBounds(new Rectangle(2, 7, 305, 167));
jPanel.add(jLabel, null);
jPanel.add(jLabel1, null);
jPanel.add(getJTextField(), null);
jPanel.add(getJTextField1(), null);
jPanel.add(getJButton(), null);
}
return jPanel;
}
/**
* This method initializes jTextField
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField() {
if (jTextField == null) {
jTextField = new JTextField();
jTextField.setBounds(new Rectangle(106, 40, 154, 24));
}
return jTextField;
}
/**
* This method initializes jTextField1
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField1() {
if (jTextField1 == null) {
jTextField1 = new JTextField();
jTextField1.setBounds(new Rectangle(105, 76, 154, 23));
}
return jTextField1;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(187, 103, 66, 25));
jButton.setText("登陸");
}
return jButton;
}
}
新聞名稱:界面java代碼下載 java主界面代碼
文章網址:http://vcdvsql.cn/article22/ddopsjc.html
成都網站建設公司_創新互聯,為您提供網站排名、網站維護、面包屑導航、虛擬主機、企業建站、網站策劃
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯