码迷,mamicode.com
首页 > 编程语言 > 详细

Java每日学习笔记1

时间:2015-07-08 10:54:42      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:

  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

标签:

原文地址:http://www.cnblogs.com/chengzhipcx/p/4629478.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!