1. 程式人生 > >a little progress every day

a little progress every day

1,按鈕
此類建立一個標籤按鈕。當按下該按鈕時,應用程式能執行某項動作。它有兩種構造方法:
public Button()
構造一個標籤字串為空的按鈕。
public Button(String label)
構造一個帶指定標籤的按鈕。
當用戶滑鼠單擊按鈕時,AWT事件處理系統將向按鈕傳送一個ActionEvent事件物件,如果應用程式需要對此做出反應,那麼就需要註冊事件監聽程式並實現ActionListener介面。
public void addActionListener(ActionListener l)新增指定的動作偵聽器,以接收發自此按鈕的動作事件。當用戶在此按鈕上按下或釋放滑鼠時,發生動作事件。如果 l 為 null,則不丟擲任何異常,也不執行任何動作。

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/** 
 * @author  Amy  E-mail:
[email protected]
* @date 建立時間:2017年10月9日 上午11:05:20 * @version 1.0 */
public class TestButton { /** * @Title: main * @Description: TODO(使用AWT圖形使用者介面的Button元件例子) * @param args * @return: void * @throws */ public static void main(String[] args) { String s1="You have Pressed the Button"
; String s2="You have released the Button"; Frame frame = new Frame(); Button button = new Button("click me"); TextField tf = new TextField(); button.setForeground(Color.blue); frame.setTitle("button example"); frame.add(tf,BorderLayout.CENTER); frame.add(button,BorderLayout.SOUTH); frame.setVisible(true); frame.pack(); // 窗體關閉 frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e){ System.exit(0); }}); // 增加滑鼠響應事件 button.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { tf.setText(s2); } @Override public void mousePressed(MouseEvent e) { tf.setText(s1); } @Override public void mouseExited(MouseEvent e) { } @Override public void mouseEntered(MouseEvent e) { } @Override public void mouseClicked(MouseEvent e) { } }); } }

執行結果:
這裡寫圖片描述
2,複選框
public class Checkbox extends Component implements ItemSelectable, Accessible
複選框是一個可處於“開”(true) 或“關”(false) 狀態的圖形元件。單擊複選框可將其狀態從“開”更改為“關”,或從“關”更改為“開”。
構造方法有四種:
Checkbox()
使用空字串標籤建立一個複選框。
Checkbox(String label)
使用指定標籤建立一個複選框。
Checkbox(String label, boolean state)
使用指定標籤建立一個複選框,並將它設定為指定狀態。
Checkbox(String label, boolean state, CheckboxGroup group)
構造具有指定標籤的 Checkbox,並將它設定為指定狀態,使它處於指定複選框組中。
Checkbox(String label, CheckboxGroup group, boolean state)
建立具有指定標籤的 Checkbox,並使它處於指定複選框組內,將它設定為指定狀態。
方法摘要:
addItemListener(ItemListener l)
新增指定的項偵聽器,以接收來自此複選框的項事件。
要想對複選框進行相應就需要實現ItemListener內部的itemStateChanged方法。通過ItemEvent的getItemSelectable可以獲得改變的複選框物件,通過getStateChange可以獲得改變後的狀態。

import java.awt.BorderLayout;
import java.awt.Checkbox;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;

import javax.print.DocFlavor.URL;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

/** 
 * @author  Amy  E-mail:[email protected]
 * @date    建立時間:2017年10月9日 下午3:19:06
 * @version 1.0   
 */
public class TestCheckBox  extends JPanel implements ItemListener {

    /**
     * @Title: main
     * @Description: TODO(這裡用一句話描述這個方法的作用)
     * @param args
     * @return:  void 
     * @throws
     */
    /**
     * 
     */

    JCheckBox chin;
    JCheckBox glass;
    JCheckBox hire;
    JCheckBox teeth; 

    StringBuffer choice;
    JLabel labelPicture;

    public TestCheckBox() {

       super(new BorderLayout());

       //建立複選框按鍵,並設定快捷鍵,和選定
       chin = new JCheckBox("chin");
       chin.setMnemonic(KeyEvent.VK_C);
       chin.setSelected(true);

       glass= new JCheckBox("glass");
       glass.setMnemonic(KeyEvent.VK_G);
       glass.setSelected(true);

       hire = new JCheckBox("hire");
       hire.setMnemonic(KeyEvent.VK_H);
       hire.setSelected(true);

       teeth = new JCheckBox("teeth");
       teeth.setMnemonic(KeyEvent.VK_T);
       teeth.setSelected(true);

       //設定一個panel,將複選框放入同一個panel
       JPanel checkPanel = new JPanel(new GridLayout(0,1));
       checkPanel.add(chin);
       checkPanel.add(glass);
       checkPanel.add(hire);
       checkPanel.add(teeth);

       //新增複選框的監聽事件
       chin.addItemListener(this);
       glass.addItemListener(this);
       hire.addItemListener(this);
       teeth.addItemListener(this);

       //將複選框的panel新增到面板的左邊
       add(checkPanel,BorderLayout.WEST);

       //建立圖片顯示區
       labelPicture = new JLabel();
       //將圖片顯示區放到面板中間
       add(labelPicture,BorderLayout.CENTER);

       //設定面板的邊界,使得控制元件能夠與邊界有一定距離
       setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

       //設定初始圖片
       choice = new StringBuffer("cght");
       //顯示更新圖片
       upDatePicture();
    }

    private void upDatePicture() {
       ImageIcon ii = createImageIcon(File.separator+"images"+
              File.separator+"geek"+
              File.separator+"geek-"+
              choice.toString()+".gif");
       labelPicture.setIcon(ii);  
    }

    private ImageIcon createImageIcon(String string) {
       java.net.URL url = TestCheckBox.class.getResource(string);
       if(url != null)
           return new ImageIcon(url);
       else
           System.out.println("image "+string +"not exist!");
       return null;
    }

    public static void createAndShowGUI()
    {
       JFrame frame = new JFrame("複選框測試");
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       JComponent panel = new TestCheckBox();

       frame.add(panel);
       frame.pack();
       frame.setLocationRelativeTo(null);
       frame.setVisible(true);

    }

    public static void main(String[] args) {

       SwingUtilities.invokeLater(new Runnable() {

           @Override
           public void run() {
              createAndShowGUI();
           }
       });
    }

    //接受處理複選框點選事件
    @Override
    public void itemStateChanged(ItemEvent e) {
       //獲取改變的複選按鍵
       Object source = e.getItemSelectable();
       int index = -1;
       char c = '-';
       if (source==chin) {
           index = 0;
           c = 'c';
       } else if(source == glass){
           index = 1;
           c = 'g';
       }else if(source == hire){
           index = 2;
           c = 'h';
       }else if(source == teeth)
       {
           index = 3;
           c = 't';
       }

       //判斷改變的按鍵的改變後的狀態
       if(e.getStateChange() == ItemEvent.DESELECTED)
           c = '-';


       choice.setCharAt(index, c);

       upDatePicture();
    }




}

執行結果:
這裡寫圖片描述

3,單選框
public class JRadioButton extends JToggleButton implements Accessible
實現一個單選按鈕,此按鈕項可被選擇或取消選擇,並可為使用者顯示其狀態。與 ButtonGroup 物件配合使用可建立一組按鈕,一次只能選擇其中的一個按鈕。(建立一個 ButtonGroup 物件並用其 add 方法將 JRadioButton 物件包含在此組中。)

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

/** 
 * @author  Amy  E-mail:[email protected]
 * @date    建立時間:2017年10月9日 下午6:04:32
 * @version 1.0   
 */
public class SingleBoxDemo {

    /**
     * @Title: main
     * @Description: TODO(這裡用一句話描述這個方法的作用)
     * @param args
     * @return:  void 
     * @throws
     */
    public static void main(String[] args) {
        SingleBox sb=new SingleBox();
        sb.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        sb.setVisible(true);
        sb.setTitle("SingleBox");
    }
    }
    class SingleBox extends JFrame{
        private JLabel label;
        private JPanel buttonPanel;
        private static final int DEFAULT_SIZE=36;
        private ButtonGroup group;
        public SingleBox(){
        setSize(400,300);
        label=new JLabel("Wasting yout time is equals to wasting yout life");
        label.setFont(new Font("Serif",Font.PLAIN,DEFAULT_SIZE));
        add(label,BorderLayout.CENTER);
        group=new ButtonGroup();
        buttonPanel=new JPanel();
        addRadioButton("Small",8);
        addRadioButton("Mediun",12);
        addRadioButton("Large",18);
        addRadioButton("Extra large",36);
        add(buttonPanel,BorderLayout.SOUTH);    
        }

    public void addRadioButton(String name,final int size){
            boolean selected=size==DEFAULT_SIZE;
            JRadioButton Button=new JRadioButton(name,selected);
            group.add(Button);
            buttonPanel.add(Button);
            ActionListener actionListener = new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    label.setFont(new Font("Serif",Font.PLAIN, size));
                }
            };
            Button.addActionListener(actionListener);
    }
    }

執行結果:
這裡寫圖片描述

相關推薦

a little progress every day

1,按鈕 此類建立一個標籤按鈕。當按下該按鈕時,應用程式能執行某項動作。它有兩種構造方法: public Button() 構造一個標籤字串為空的按鈕。 public Button(String label)

Learn a linux command every day--day1:cd命令

new peid nbsp 進行 所在 test 執行 環境變量 inux Linux cd 命令可以說是Linux中最基本的命令語句,用於切換目錄,是shell的內置命令,要進行其他操作,都是建立在使用 cd 命令上的。 所以,學習Linux 常用命令,首先就要學好 cd

【英語】A positive view of every day

If your life feels like it is lacking the power that you want and the motivation that you need, sometimes all you have to do is shift your point

every day a practice —— morning

In 25 years, Panda Express has transformed from a single restaurant in a southern California mall to a 2000-location empire around the world. &n

every day a practice —— morning(2)

Two years at sea have fostered a close relationship between the two fellow sailors as they cross the globe, through warm weather and cold. 兩年的環球航海生活,

every day a practice —— morning(3)

"WeChat does not store any chat histories. They are stored only on users' phones, computers or other devices," Tencent said in a statement on its own WeCha

every day a practice —— morning(4)

If there’s one thing New Yorkers love more than discovering a new secret remedy, it’s telling other New Yorkers about it.與發現新的祕方相比,紐約人更熱衷的事情是去和其他人分享祕方。 &n

every day a practice —— morning(5)

Huawei has not been accused of wrongdoing. As an administrative subpoena, the Treasury document does not indicate that the Chinese company is part of

every day a practice —— morning(7)

It is probably because Willow was the last link to her parents and a pastime that goes back to her own childhood. It really does feel like the end of an er

“糞便銀行”:救人拿錢兩不誤 A Poop Bank in Massachusetts Will Pay You $40 Every Day

“糞便銀行”:救人拿錢兩不誤 如果你年齡小於50歲,排便規律,而且願意每天去美國麻省麥德福德跑一趟,那麼沒準你可以每天得到40美元的外快,而你需要做的事情只是生產便便。  要想得到這筆收入,請拜訪“開放生物群”(OpenBiome)。它是全美唯一的獨立非營利性“糞便銀行”,初建於2012年,由麻省理工學

A little issue in Mathematical Thought from Ancient to Modern Times, Vol. 3

enc classes 技術 min member put clas hat 技術分享 At P985 of the book, says But there are cuts that are not determined by rational numbers. I

A. Little C Loves 3 I Codeforces Round #511 (Div. 2) 【數學】

pos == force line for 最大 header 應該 XML 題目: Little C loves number ?3? very much. He loves all things about it. Now he has a positive

a little riak book

a little riak book 的無聊總結 <pre name="code" class="python">#!/bin/bash # Riak HTTP interface stays true to their intent: 1xx Informat

Money a little tight?-----Have a staycation

The staycation Some people love to travel.and they do it all the time.others hate to travel.preferring a quiet holiday at home on the couch.You mi

Codeforces Round #511 (Div. 2).A. Little C Loves 3 I(水題)

A. Little C Loves 3 I time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output

【題解】codeforces1047A[Codeforces Round #511 (Div. 2)]A.Little C Loves 3 I 數學知識

題目連結 Description Little C loves number «3» very much. He loves all things about it. Now he has a positive integer n. He wants to sp

牛客練習賽34 A little w and Soda

題目連結 兩元一瓶汽水,兩個汽水瓶可以換一瓶汽水。也就是說一個汽水瓶的價值是 1 元錢。 最後可以借一個空瓶,所以最後手裡不會剩下空瓶。那麼一開始能花掉多少錢,最後 就能夠喝到多少汽水。 所以奇數會剩下一塊錢花不出去,偶數最後不會有剩餘。答案為奇數-1,偶數直接輸 出。 #inclu

I want to know a little more.

序:如何保證kafka全域性訊息有序?   比如,有100條有序資料,生產者傳送到kafka叢集,kafka的分片有4個,可能的情況就是一個分片儲存0-25,一個儲存25-50......這樣訊息在kafka中儲存是區域性有序了。嚴格說,kafka是無法保證全域性訊息有序的

Tweet A Little Bit Tweet

最近寫了幾篇部落格,但有時不自然就流露除了吐槽或者念念碎的蛛絲馬跡,或者突然冒出了一大段似是而非的話。回過頭去看,自己都覺得慚愧。當然,像條理不清、論據不足、詞不達意、個人色彩濃厚,以及不夠流暢、不夠精煉如此等等問題也很嚴重,希望能夠慢慢錘鍊和改善,到這兒似乎又要學學如何書寫,如何寫作了:-D。

Learn a Little Bit Math Again

工作以後,做Web開發,做DBA,數學似乎沒有太多的用武之地,其他的學科如物理、化學、 生物就更不用說了,時間一長,它們就會很快被遺忘;若不是需要解決一些實際問題,恐怕很難再撿起來。當然,若是對此有興趣就另當別論,比如這位Matrix67,北大中文系應用學專業,卻是一位數學宅,認為生活中的數學無處不