📄 java入门(9) java与gui.htm
字号:
<BR><BR> ____________________________________________________________________
<BR><BR> ____________________________________________________________________
<BR><BR> ____________________________________________________________________
<BR><BR> ____________________________________________________________________
<BR><BR> ____________________________________________________________________
<BR><BR> 2)按钮类中还提供了一个方法,可以修改按钮标签。以下就是一个应用实例:
<BR><BR><BR>button1.setText(“OK”);
<BR><BR><BR><BR> 其中button1是按钮名,setText是方法名,set就是设置,Text就是文本,也就是设置按钮上的文本色。参数是”OK”。这样就会将button1的标签改为”OK”。
<BR><BR> 请使用这个方法修改程序useButton,使得按钮上显示按过的次数,如果没有按下,则仍显示“Beep!”。只需写出修改的部分。
<BR><BR> ____________________________________________________________________
<BR><BR> ____________________________________________________________________
<BR><BR> ____________________________________________________________________
<BR><BR> 练习答案 <BR><BR> 1) 以下就是一个实现实例: <BR><BR> 源程序:lianxi901.java
<BR><BR><BR>import javax.swing.*; <BR>import java.awt.*; <BR>import
java.awt.event.*; <BR>public class lianxi901 extends JApplet <BR>{
<BR>JButton buttonRed; <BR>JButton buttonGreen; <BR>JButton buttonBlue;
<BR>public void init() <BR>{ <BR>JPanel panel1=(JPanel)getContentPane();
<BR>panel1.setLayout(new FlowLayout()); <BR>buttonRed=new JButton(“set
red”); <BR>buttonGreen=new JButton(“set green”); <BR>buttonBlue=new
JButton(“set blue”); <BR>panel1.add(buttonRed);
<BR>panel1.add(buttonGreen); <BR>panel1.add(buttonBlue);
<BR>buttonRed.addActionListener(new ActionListener() <BR>{ <BR>public void
actionPerformed(ActionEvent evt) <BR>{
<BR>buttonRed.setForeground(Color.red);
<BR>buttonGreen.setForeground(Color.red);
<BR>buttonBlue.setForeground(Color.red); <BR>} <BR>});
<BR>buttonGreen.addActionListener(new ActionListener() <BR>{ <BR>public
void actionPerformed(ActionEvent evt) <BR>{
<BR>buttonRed.setForeground(Color.green);
<BR>buttonGreen.setForeground(Color.green);
<BR>buttonBlue.setForeground(Color.green); <BR>} <BR>});
<BR>buttonBlue.addActionListener(new ActionListener() <BR>{ <BR>public
void actionPerformed(ActionEvent evt) <BR>{
<BR>buttonRed.setForeground(Color.blue);
<BR>buttonGreen.setForeground(Color.blue);
<BR>buttonBlue.setForeground(Color.blue); <BR>} <BR>}); <BR>} <BR>}
<BR><BR><BR><BR> 通过构建一个HTML文件,使其包含这个Applet后,以下就是一个运行效果之一:
<BR><BR><BR> 图9-2 练习的输出 <BR><BR> 2) 要实现这个功能很简单,只需做两个修改: <BR><BR> a.
在变量定义处加入一个新的变量定义: <BR><BR><BR>int counter=0;
<BR><BR><BR><BR> b.在button1的addActionListener方法中的actionPerformed中加入一行:
<BR><BR><BR>Button1.setText(String.valueOf(++counter));
<BR><BR><BR><BR> 9.3 使用Label(标签) <BR><BR> 实例说明
<BR><BR> 1.首先,我们使用文字编辑软件输入下源程序。 <BR><BR> 源程序:useLabel.java
<BR><BR><BR>import javax.swing.*; <BR>import java.awt.*; <BR>import
java.awt.event.*; <BR>public class useLabel extends JApplet <BR>{
<BR>JLabel label1; <BR>public void init() <BR>{ <BR>label1=new
JLabel(“This is a java label!”);
<BR>label1.setHorizontalAlignment(SwingConstants.CENTER);
<BR>label1.setVerticalAlignment(SwingConstants.TOP);
<BR>label1.setBackground(Color.red); <BR>label1.setOpaque(true);
<BR>label1.setForeground(Color.white); <BR>label1.setToolTipText(“Hello,I
am a Java Label!”); <BR>panel1.add(label1); <BR>} <BR>}
<BR><BR><BR><BR> 2.使用javac编译这个程序。 <BR><BR> 3.编辑一个显示这个Java Applet的页面:
<BR><BR> 源程序:useLabel.html <BR><BR><BR><html><body>
<BR><applet code="useLabel.class" WIDTH=150 HEIGHT=100>
<BR></applet> <BR></body></html>
<BR><BR><BR><BR> 4.最后使用appletviewer来运行这个程序: <BR><BR><BR>c:javastudy>
appletviewer useLabel.html <BR><BR><BR><BR> 程序输出如下图所示: <BR><BR><BR> 图9-3
程序useLabel的运行结果 <BR><BR> 传授新知
<BR><BR> 我们可以看到这一段程序,与useButton.java十分类似,在类的前面,包含了编写GUI程序要使用的包:javax.swing.*、java.awt.*和java.awt.event.*。
<BR><BR> 接着,通过使用“extends
JApplet”说明useLabel类也是一个小应用程序(Applet)。在init方法中,首先创建了一个容器---Panel,用来放置后面创建的“标签”。我们下面就认真地看一下关于Label的语句:
<BR><BR> 1) <BR><BR><BR>label1=new JLabel(“This is a java label!”);
<BR><BR><BR><BR> 类似的,我们使用new来创建一个Label实例,参数“This is a java
label!”就是标签要显示的内容。如果省略这个标签,将创建一个空标签,什么也不会显示。 <BR><BR> 一些提示:
<BR><BR> 在Java语言中,标签提供了一个setText方法,可以用来设置它显示的内容。 <BR><BR> 2)
<BR><BR><BR>label1.setHorizontalAlignment(SwingConstants.CENTER);
<BR>label1.setVerticalAlignment(SwingConstants.TOP);
<BR><BR><BR><BR> Label类提供两个设置其对齐方式的方法: <BR><BR> §
setHorizontalAlignment:设置水平对齐方式; <BR><BR> 它的有效参数是: <BR><BR> ¨
SwingConstants.LEFT:左对齐;(默认值,也就是不设置时则左对齐) <BR><BR> ¨
SwingConstants.CENTER:居中对齐; <BR><BR> ¨ SwingConstants.RIGHT:右对齐;
<BR><BR> § setVerticalAlignment:设置垂直对齐方式; <BR><BR> 它的有效参数是: <BR><BR> ¨
SwingConstants.TOP:向上对齐; <BR><BR> ¨
SwingConstants.CENTER:居中对齐;(默认值,也就是不设置时居中对齐) <BR><BR> ¨
SwingConstants.BOTTOM:向下对齐; <BR><BR> 3) label1.setBackground(Color.red);
<BR><BR> label1.setOpaque(true);
<BR><BR> setBackground方法用来设置标签的背景色。但是Java的Swing部件默认状态下是不透明的,在这种状态下是无法显示背景色的。因此,我们还需使用setOpaque(true)方法使该部件变为透明的。
<BR><BR> 4) <BR><BR><BR>label1.setForeground(Color.white);
<BR><BR><BR><BR> 与按钮一样,我们可以使用setForeground方法来设置字符颜色。 <BR><BR> 5)
<BR><BR><BR>label1.setToolTipText(“Hello,I am a Java Label!”);
<BR><BR><BR><BR> 在Windows的界面中,许多工具按钮都提供了一个这样的功能:当你将鼠标放在这个按钮上面一会儿,就会出现一个帮助性的提示。在Java语言中,我们可以很简单地使用:
<BR><BR> 部件.setToolTipText(“提示信息”) <BR><BR> 来实现这个功能。 <BR><BR> 一些提示:
<BR><BR> 其实“按钮”部件也可以使用这个方法来显示提示信息。 <BR><BR> 6)
<BR><BR><BR>panel1.add(label1);
<BR><BR><BR><BR> 最后,我们使用容器panel的add方法,将这个标签放置到容器中。 <BR><BR> 自测练习
<BR><BR> 1) 标签Label上显示的文本信息一经定后,就不能在程序中动态修改。________ <BR><BR> a. 错 b.对
<BR><BR> 2) 对于一个Label来说,默认的水平对齐方式是:________。
<BR><BR> a.SwingConstants.CENTER b.SwingConstants.RIGHT
<BR><BR> c.SwingConstants.LEFT <BR><BR> 3)
对于一个Label来说,默认的垂直对齐方式是:________。 <BR><BR> a.SwingConstants.CENTER
b.SwingConstants.BOTTOM <BR><BR> c.SwingConstants.TOP <BR><BR> 4)
要获得一个Label上显示的文本信息,应使用_________方法。 <BR><BR> a.setText b.getText
c.getContent <BR><BR> 5)标签类中提供了一个方法,可以修改标签上显示的文本。以下就是一个 应用实例:
<BR><BR><BR>label1.setText(“new text!”);
<BR><BR><BR><BR> 请编写一个程序,在面板左边显示一个标签,右边显示一个按钮,按钮上显示“Plus
one”,标签显示为“0”。每按一次按钮,标签上显示的数字就加1. <BR><BR> 一些提示:
<BR><BR> 在Java中,我们还可以使用getText来获得标签上显示的文本信息。 <BR><BR> 程序显示如下图所示:
<BR><BR><BR> 图9-4 练习输出
<BR><BR> ____________________________________________________________________
<BR><BR> ____________________________________________________________________
<BR><BR> ____________________________________________________________________
<BR><BR> ____________________________________________________________________
<BR><BR> ____________________________________________________________________
<BR><BR> ____________________________________________________________________
<BR><BR> 练习答案 <BR><BR> 1)a 在程序中可以使用setText方法动态修改Label显示的文本信息。
<BR><BR> 2)c 默认是水平左对齐。 <BR><BR> 3)a 默认是垂直居中对齐。 <BR><BR> 4)b
setText方法是设置文本,getText是获取文本,并没有getContent方法。 <BR><BR> 5)以下就是一个实现该功能的程序实例:
<BR><BR> 源程序:lianxi902.java <BR><BR><BR>import javax.swing.*; <BR>import
java.awt.*; <BR>import java.awt.event.*; <BR>public class lianxi902
extends JApplet <BR>{ <BR>JButton button1; <BR>JLabel label1; <BR>int
counter=0; <BR>public void init() <BR>{ <BR>JPanel
panel1=(JPanel)getContentPane(); <BR>panel1.setLayout(new FlowLayout());
<BR>label1=new JLabel(String.valueOf(counter));
<BR>label1.setIcon("winupd.ico"); <BR>button1=new JButton("Plus one");
<BR>panel1.add(label1); <BR>panel1.add(button1);
<BR>button1.addActionListener(new ActionListener() <BR>{ <BR>public void
actionPerformed(ActionEvent evt) <BR>{
<BR>label1.setText(String.valueOf(++counter)); <BR>} <BR>}); <BR>} <BR>}
<BR><BR><BR><BR> 9.4 复选框与单选按钮 <BR><BR> 实例说明
<BR><BR> 1.首先,我们使用文字编辑软件输入下源程序。 <BR><BR> 源程序:useCheckbox.java
<BR><BR><BR>import javax.swing.*; <BR>import java.awt.*; <BR>import
java.awt.event.*; <BR>public class useCheckbox extends JApplet <BR>{
<BR>JButton button1; <BR>int counter=0; <BR>Checkbox
setbeep,setcounter,red,green,blue; <BR>CheckboxGroup colorSel; <BR>public
void init() <BR>{ <BR>JPanel panel1=(JPanel)getContentPane();
<BR>panel1.setLayout(new FlowLayout()); <BR>setbeep=new Checkbox("Beep
when press button"); <BR>setcounter=new Checkbox("Counter press time");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -