java.docx

上传人:b****6 文档编号:4118153 上传时间:2022-11-28 格式:DOCX 页数:14 大小:46.26KB
下载 相关 举报
java.docx_第1页
第1页 / 共14页
java.docx_第2页
第2页 / 共14页
java.docx_第3页
第3页 / 共14页
java.docx_第4页
第4页 / 共14页
java.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

java.docx

《java.docx》由会员分享,可在线阅读,更多相关《java.docx(14页珍藏版)》请在冰豆网上搜索。

java.docx

java

1、实现测试按钮类。

该类要求如下:

(1)窗体中有两个按钮,分别是按钮1和按钮2.

(2)当点击按钮1或者是按钮2的时候,出现你点击按钮1或者是按钮2的消息。

(3)按钮使用流布局。

2、实现文本域程序。

改程序要求如下:

(1)、该程序中有四个文本域。

(2)、四个文本域采用网格布局。

(竖排)

(3)、文本域初始状态分别显示文本域1,2,3,4.

3、实现复选框点击程序。

该程序要求如下:

(1)、有两个选项分别是:

粗体字,和斜体

(2)、点击粗体字体变粗,点击斜体字体改变为斜体。

4、、实现单选框程序。

要求如下:

(1)、分别有三种颜色红、绿,蓝三种颜色

(2)、点击不同颜色的时候,背景分别变为相应的颜色。

5、实现进度条程序。

点击进度条时候进度开始。

事件处理程序练习:

1、实现鼠标点击按钮背景颜色随机改变颜色。

(三种方法实现)

importjava.awt.*;

importjava.awt.event.*;

importjavax.swing.*;

publicclassJButtonDemoextendsJFrame

{

privateJButtonbutton1,button2;

publicJButtonDemo(){

super("测试按钮类");

setSize(250,100);

Containercontainer=getContentPane();

try

{

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

}catch(Exceptione){}

//获取内容面板//设置内容面板的布局管理器

container.setLayout(newGridLayout(1,2));

container.setBackground(Color.YELLOW);

//创建按钮对象

button1=newJButton("按钮1");

//button1.setFont(newFont("Serif",Font.PLAIN,12));

//ImageIconimg1=newImageIcon("wave.gif");

//ImageIconimg2=newImageIcon("stop.gif");

button2=newJButton("按钮2");

//button2.setRolloverIcon(img2);

//button2.setFont(newFont("Serif",Font.PLAIN,12));

//为组件注册监听器

ButtonHandlerhandler=newButtonHandler();

button1.addActionListener(handler);

button2.addActionListener(handler);

//将各种组件添加到内容面板

container.add(button1);

container.add(button2);

setVisible(true);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

publicstaticvoidmain(String[]args)

{

JButtonDemodemo=newJButtonDemo();

demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

privateclassButtonHandlerimplementsActionListener

{

publicvoidactionPerformed(ActionEventevent)

{

JOptionPane.showMessageDialog(JButtonDemo.this,

"你按了:

"+event.getActionCommand());

}

}

}

importjava.awt.*;

importjavax.swing.*;

publicclassJTextArea1extendsJFrame

{

privateJTextAreatextArea1,textArea2,textArea3,textArea4;

publicJTextArea1()

{

super("JTextArea1");

setSize(300,200);

try

{//设置外观

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

}catch(Exceptione){}

//获取内容面板

Containercontainer=getContentPane();

container.setLayout(newGridLayout(4,1,5,5));

//利用不同的构造函数创建对象

textArea1=newJTextArea();

textArea2=newJTextArea(2,8);

textArea3=newJTextArea("3");

textArea4=newJTextArea("4",5,10);

//设置字体

textArea1.setFont(newFont("Serif",Font.PLAIN,12));

textArea2.setFont(newFont("Serif",Font.PLAIN,12));

textArea3.setFont(newFont("Serif",Font.PLAIN,12));

textArea4.setFont(newFont("Serif",Font.PLAIN,12));

//setText()方法会将原来的内容清除

textArea1.setText("JTextArea1");

//append()方法会将设置的字符串接在原来JTextArea内容文字之后.

textArea2.append("JTextArea2");

//设置[Tab]键的跳离距离

textArea4.setTabSize(10);

//自动换行功能

textArea4.setLineWrap(true);

//断行不断字功能

textArea4.setWrapStyleWord(true);

//将组件加入内容面板中

container.add(newJScrollPane(textArea1));

container.add(newJScrollPane(textArea2));

container.add(newJScrollPane(textArea3));

container.add(newJScrollPane(textArea4));

setVisible(true);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

publicstaticvoidmain(String[]args)

{

JTextArea1demo=newJTextArea1();

demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

importjava.awt.*;

importjava.awt.event.*;

importjavax.swing.*;

publicclassJCheckBoxDemoextendsJFrame

{

privateJLabellabel;

privateJCheckBoxbold,italic;

publicJCheckBoxDemo()

{

super("复选框");

setSize(300,100);

try

{//设置外观

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

}catch(Exceptione){}

//获取内容面板

Containercontainer=getContentPane();

//设置内容面板的布局管理器

container.setLayout(newFlowLayout());

//设置内容面板的背景色

container.setBackground(Color.YELLOW);

//创建标签对象,并设置字体

label=newJLabel("请注意观察宋体的变化");

label.setFont(newFont("Serif",Font.PLAIN,14));

container.add(label);

//创建复选框

bold=newJCheckBox("粗体");

italic=newJCheckBox("斜体");

bold.setFont(newFont("Serif",Font.PLAIN,12));

italic.setFont(newFont("Serif",Font.PLAIN,12));

bold.setBackground(Color.YELLOW);

italic.setBackground(Color.YELLOW);

//注册监听器

CheckBoxHandlerhandler=newCheckBoxHandler();

bold.addItemListener(handler);

italic.addItemListener(handler);

container.add(bold);

container.add(italic);

setVisible(true);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

publicstaticvoidmain(String[]args)

{

JCheckBoxDemoapplication=newJCheckBoxDemo();

}

privateclassCheckBoxHandlerimplementsItemListener

{

privateintvalBold=Font.PLAIN;

privateintvalItalic=Font.PLAIN;

publicvoiditemStateChanged(ItemEventevent)

{

if(event.getSource()==bold)

valBold=bold.isSelected()?

Font.BOLD:

Font.PLAIN;

if(event.getSource()==italic)

valItalic=bold.isSelected()?

Font.ITALIC:

Font.PLAIN;

label.setFont(newFont("Serif",valBold+valItalic,14));

}

}

}importjava.awt.*;

importjava.awt.event.*;

importjavax.swing.*;

publicclassJRadioButtonDemoextendsJFrame

{

privateJPanelcolorPanel,buttonPanel;

privateJRadioButtonred,green,blue;

privateButtonGroupbuttonGroup;

publicJRadioButtonDemo()

{

super("单选框");

setSize(300,200);

try

{//设置外观

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

}catch(Exceptione){}

//获取内容面板

Containercontainer=getContentPane();

//创建颜色面板对象

colorPanel=newJPanel();

colorPanel.setBackground(Color.RED);

container.add(colorPanel,BorderLayout.CENTER);

//创建单选按钮

buttonGroup=newButtonGroup();

red=newJRadioButton("红色",true);

green=newJRadioButton("绿色");

blue=newJRadioButton("蓝色");

//设置字体

red.setFont(newFont("Serif",Font.PLAIN,14));

green.setFont(newFont("Serif",Font.PLAIN,14));

blue.setFont(newFont("Serif",Font.PLAIN,14));

buttonGroup.add(red);

buttonGroup.add(green);

buttonGroup.add(blue);

//注册监听器

RadioButtonHandlerhandler=newRadioButtonHandler();

red.addItemListener(handler);

blue.addItemListener(handler);

green.addItemListener(handler);

//创建存放单选按钮的面板

buttonPanel=newJPanel();

buttonPanel.add(red);

buttonPanel.add(green);

buttonPanel.add(blue);

container.add(buttonPanel,BorderLayout.SOUTH);

setVisible(true);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

publicstaticvoidmain(String[]args)

{

JRadioButtonDemoapplication=newJRadioButtonDemo();

}

privateclassRadioButtonHandlerimplementsItemListener

{

publicvoiditemStateChanged(ItemEventevent)

{

if(red.isSelected())

colorPanel.setBackground(Color.red);

elseif(green.isSelected())

colorPanel.setBackground(Color.GREEN);

else

colorPanel.setBackground(Color.BLUE);

}

}

}

importjava.awt.*;

importjavax.swing.*;

importjava.awt.event.*;

publicclassJProgressBarDemoextendsJFrame

{

privateJProgressBarprogressBar;//进度条

privateJButtonstartButton,stopButton;//启动,暂停,重启动按钮

privateTimertimer;//定时器

publicJProgressBarDemo()

{

super("滚动条");

setSize(300,200);

//获取内容面板

Containercontainer=getContentPane();

//设置内容面板的布局管理器

container.setLayout(newFlowLayout(FlowLayout.CENTER));

container.setBackground(Color.YELLOW);

//创建进度条

progressBar=newJProgressBar();

//设置最小值,最大值,初值

progressBar.setMinimum(0);

progressBar.setMaximum(100);

progressBar.setValue(0);

//显示进度条进度文本

progressBar.setStringPainted(true);

//显示进度条边框

progressBar.setBorderPainted(true);

//设置进度条大小,背景色,前景色

progressBar.setPreferredSize(newDimension(250,30));

progressBar.setBackground(Color.WHITE);

progressBar.setForeground(Color.GREEN);

container.add(progressBar);

//创建按钮

startButton=newJButton("开始");

stopButton=newJButton("暂停");

//设置按钮背景颜色

startButton.setBackground(Color.WHITE);

stopButton.setBackground(Color.WHITE);

//设置字体

startButton.setFont(newFont("Serif",Font.PLAIN,14));

stopButton.setFont(newFont("Serif",Font.PLAIN,14));

stopButton.setEnabled(false);

//注册监听器

TimerHandlerhandler=newTimerHandler();

startButton.addActionListener(handler);

stopButton.addActionListener(handler);

container.add(startButton);

container.add(stopButton);

//创建定时器,时间间隔为50毫秒,设置监听器

timer=newTimer(50,handler);

setVisible(true);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

publicstaticvoidmain(Stringargs[])

{

JProgressBarDemoapplication=newJProgressBarDemo();

}

classTimerHandlerimplementsActionListener

{

//处理定时器事件

privateintvalue=0;

publicvoidactionPerformed(ActionEventevent)

{

if(event.getSource()==timer)

{

value=progressBar.getValue();

if(value<100)

{

value++;

progressBar.setValue(value);

}

else

{

timer.stop();

startButton.setEnabled(true);

stopButton.setEnabled(false);

}

}

elseif(event.getSource()==startButton)

{

if(progressBar.getValue()>=100)

progressBar.setValue(0);

timer.start();

startButton.setEnabled(false);

stopButton.setEnabled(true);

}

elseif(event.getActionCommand().equals("暂停"))

{

timer.stop();

stopButton.setText("重启动");

}

elseif(event.getActionCommand().equals("重启动"))

{

timer.restart();

stopButton.setText("暂停");

}

}

}//TimerHandler类结束

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 初中教育 > 政史地

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1