Java每日學習筆記1

來源:互聯網
上載者:User

標籤:

  1. 選項按鈕

JRadioButton radioButton1 = new JRadioButton("Java");// 建立選項按鈕

contentPane.add(radioButton1);// 應用選項按鈕

?

JRadioButton radioButton2 = new JRadioButton("PHP");// 建立選項按鈕

contentPane.add(radioButton2);// 應用選項按鈕

?

JRadioButton radioButton3 = new JRadioButton("C++");// 建立選項按鈕

contentPane.add(radioButton3);// 應用選項按鈕

?

ButtonGroup group = new ButtonGroup();// 建立選項按鈕組

group.add(radioButton1);// 將radioButton1增加到選項按鈕組中

group.add(radioButton2);// 將radioButton2增加到選項按鈕組中

group.add(radioButton3);// 將radioButton

  1. 嵌套表單

package com.pcx.test;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

?

public class TestInternalFrame {

????final int DESKTOP_WIDTH = 480;

????final int DESKTOP_HEIGHT = 360;

????final int FRAME_DISTANCE = 30;

?

????JFrame jf = new JFrame("父視窗");

????// 定義一個虛擬桌面

????private JDesktopPane desktop = new JDesktopPane();

????// 儲存下一個內部視窗的座標點

????private int nextFrameX;

????private int nextFrameY;

????// 定義內部視窗為虛擬桌面的1/2大小

????private int width = DESKTOP_WIDTH / 2;

????private int height = DESKTOP_HEIGHT / 2;

????// 為主視窗定義2個菜單

????JMenu fileMenu = new JMenu("檔案");

????// 定義newAction用於建立菜單和工具按鈕

????Action newAction = new AbstractAction("建立", new ImageIcon("ico/new.png")) {

????????public void actionPerformed(ActionEvent event) {

????????????// 建立內部視窗

????????????final JInternalFrame iframe = new JInternalFrame("新文檔", true, // 可改變大小

????????????????????true, // 可關閉

????????????????????true, // 可最大化

????????????????????true); // 可最小化

????????????iframe.add(new JScrollPane(new JTextArea(8, 40)));

????????????// 將內部視窗添加到虛擬桌面中

????????????desktop.add(iframe);

????????????// 設定內部視窗的原始位置(內部視窗預設大小是0X0,放在0,0位置)

????????????iframe.reshape(nextFrameX, nextFrameY, width, height);

????????????// 使該視窗可見,並嘗試選中它

????????????iframe.show();

????????????// 計算下一個內部視窗的位置

????????????nextFrameX += FRAME_DISTANCE;

????????????nextFrameY += FRAME_DISTANCE;

????????????if (nextFrameX + width > desktop.getWidth())

????????????????nextFrameX = 0;

????????????if (nextFrameY + height > desktop.getHeight())

????????????????nextFrameY = 0;

????????}

????};

????// 定義exitAction用於建立菜單和工具按鈕

????Action exitAction = new AbstractAction("退出", new ImageIcon("ico/exit.png")) {

????????public void actionPerformed(ActionEvent event) {

????????????System.exit(0);

????????}

????};

?

????public void init() {

????????// 為視窗安裝菜單條和工具條

????????JMenuBar menuBar = new JMenuBar();

????????JToolBar toolBar = new JToolBar();

????????jf.setJMenuBar(menuBar);

????????menuBar.add(fileMenu);

????????fileMenu.add(newAction);

????????fileMenu.add(exitAction);

????????toolBar.add(newAction);

????????toolBar.add(exitAction);

????????desktop.setPreferredSize(new Dimension(480, 360));

????????// 將虛擬桌面添加到頂級JFrame容器中

????????jf.add(desktop);

????????jf.add(toolBar, BorderLayout.NORTH);

????????jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

????????jf.pack();

????????jf.setVisible(true);

????}

?

????public static void main(String[] args) {

????????new TestInternalFrame().init();

????}

}

  1. 設定標籤中的字型

int size=14;
label.setFont(new Font("Serif",Font.PLAIN,size));

  1. 如何在jlabel中設定文本的換行

String info="<html>你的ID號是:"+user.getId()+"<br/>"+"你的使用者名稱是:"+user.getName()+"</html>";

Label.settext(info);

  1. 類型轉換異常

    public class Test {
    ?public static void main(String[] args){
    ??Animal cat=new Cat();
    ??Animal dog=new Dog();
    ??Object b=new Animal();
    ??Object d=new Cat();
    ??displayObject(cat);
    ??displayObject(dog);
    ????// Cat c=(Cat)b; //Animal cannot be cast to Cat
    ???// Cat cc=d;//cannot convert from Object to Cat
    ?????Cat ee=(Cat)d;
    ?cat.output();//if the method output() is undefined in class Cat,then the output would be "I am an animal"
    ?// a.speak(); //the method speak() is undefined for the type Animal
    ?}
    ??public static void??displayObject(Object obj){
    ???if(obj instanceof Cat)
    ????((Cat) obj).speak();
    ???else if(obj instanceof Dog)
    ????((Dog)obj).speak();
    ??}
    }
    class Animal{
    ?void?output(){
    ??System.out.println("I am an animal");
    ?}?
    }
    ?class Cat extends Animal{
    ??void speak(){
    ???System.out.println("
    喵喵~");
    ??}
    ?void?output(){
    ???System.out.println("I am a cat");
    ??}
    ??
    ?}
    ?
    ?class Dog extends Animal{
    ??void speak(){
    ???System.out.println("
    汪汪~");
    ??}
    ?void?output(){
    ???System.out.println("I am a dog");
    ???}
    ?}

    通過該程式,我有如下理解:

    1.父類對象可以強制轉換為子類對象,但是前提是此父類對象為子類對象執行個體化的結果。

    e.g. Fruit fruit=new Apple();

    ?????Apple a=(Apple)fruit;//ok

    e.g. 假設Apple類繼承於Fruit類,

    ?????Object fruit=new Fruit();

    ?????Object apple=(Apple)fruit;//wrong

    2.子類的執行個體向父類轉型是為了實現多態。

    ??

  2. 圖片文本最佳的封裝方案

background2 = new JLabel(new ImageIcon(

????????????????MainUI.class.getResource("/com/nt/image/main.gif")));

????????Random rd = new Random();

????????stream = MainUI.class.getResourceAsStream("/com/nt/file/"

????????????????+ rd.nextInt(19) + ".txt");

????????try {

????????????read = new BufferedReader(new InputStreamReader(stream, "GBK"));

  1. 在panel裡面設定背景圖片

Java每日學習筆記1

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.