主观

本题的功能是用按钮来控制文字的颜色。窗口中有三个按钮“Yellow”、“Blue”和“Red”,它们分别对应文字标签中文本的颜色为黄色、蓝色和红色,单击任意一个按钮,文字标签中的文本就变成按钮对应的颜色。

import java.awt.*;

import java.awt.event.*;

import javflx.swing.*;

class ButtonPanel extends JPanel implements ActionL-

istener{

public ButtonPanel{

yellowButton=new J Button("Yellow");

blueButton=new JButton("Blue");

redButton=new JButton("Red");

j1=new JLabel("I am from China!");

add(yellowButton);

add(blueButton);

add(redButton);

add(j1);

yellowButtofl.addActionListener(this);

blueButton.addActionListener(this);

redButton.addActionListener(this);

}

public void actionPerformed(ActionEvent evt){

0bject source=evt.getSource;

Color color=getForeground;

if(source= =yellowButton)color=Color.

yellow;

else if(source= =blueButton)color=Color.

blue;

else if(source= =redButton)color=

Color.red;

        

        

}

private JButton yellowButton;

private JButton blueButton;

private JButton redButton;

private JLabel jl;

}

class ButtonFrame. extends JFrame{

public ButtonFrame{

setTitle("exam l6");

setSize(300,200);

addWindowListener(new WindowAdapter{

public void windowClosing(WindowEvent e){

System.exit(O);

}

});

Container contentPane=getContentPane;

contentPane.add(new ButtonPanel);

}

}

public class java2{

public static void main(String[]args){

JFrame. frame=new ButtonFrame;

frame.show; 

}

}

参考答案
您可能感兴趣的试题

本题的功能是定义自已的组件类。窗口中排布着12个按钮,鼠标移动按钮时,按钮背景颜色改变,用鼠标单击 按钮时,后台将显示该按钮对应的字符。

import java.awt.*;

import java.awt.event.*;

import java.util.*;

class java3 extends Frame{

String keys="l23456789*0#";

java3{

super("java3");

addWindowListener(new WindowAdapter{

public void windowClosing(WindowEvent e){

System.exit(O);

}

});

setLayout(new GridLayout(4,3,6,6));

for(int i=0;i)){

KeyButton kb=new KeyButton(keys.charAt

(i));

kb.addkeyListener(this)

kb.setBackground(Color.pink);

kb.setForeground(Color.black);

add(kb);

}

setSize(200,200);

show;

}

class KeyEventHandler extends KeyAdapter{

public void keyTyPed(KeyEvent evt){

System.out.println(evt.getChar)

}

}

public static void main(String[]args){

newjava3;

}

class KeyButton extends Component{

KeyListener keyListener;

boolean highlighted;

char key;

KeyButton(char k){

this.key=k;

addMouseListener(new MouseEventHandler):

}

public void paint(Graphics g){

int W=getSize.width;

int h=getSize.height;

String s=""+key;

FontMetrics fm=g.getFontMetrics;

if(highlighted){

g.setColor(getBackground);

g.fillRoundRect(0,0,w-1,h-1,10,10);

}

g.setColor(getForeground);

g.drawRoundRect(0,0,w-1,h-1,10,10);

g.drawString(s,(w-fm.stringWidth(s))/2,

(h-fm.getHeight)/2+fm.getAscent);

}

class MouseEventHandler extends MouseAdapter{

public void mousePressed(MouseEvent evt){

if(keyListener!=null){

keyListener.keyTyped(

new KeyEvent(KeyButton.this,KeyEvent.KEY_

TYPED,

System.currentTimeMillis,

0,KeyEvent.VK_UNDEFINED,key));

}

}

public void mouseEntered(MouseEvent evt){

highlighted=true;

repaint;

}

public void mouseExited(MouseEvent evt){

highlighted=false;

repaint;

}

}

public synchronized void addKeyListerner(KeyLis-

tener l){

keyListener=AWTEventMuhieaster.add(key-

Listener,1);

}

public synchronized void removeKeyListener(Key-

Listener l){

keyListener=AWTEventMuhicaster.remove

(keyListener,1);

}

}

本题的功能是用按钮来控制文字的颜色。窗口中有三个按钮“Yellow”、“Blue”和“Red”,它们分别对应文字标签中文本的颜色为黄色、蓝色和红色,单击任意一个按钮,文字标签中的文本就变成按钮对应的颜色。

import java.awt.*;

import java.awt.event.*;

import javflx.swing.*;

class ButtonPanel extends JPanel implements ActionL-

istener{

public ButtonPanel{

yellowButton=new J Button("Yellow");

blueButton=new JButton("Blue");

redButton=new JButton("Red");

j1=new JLabel("I am from China!");

add(yellowButton);

add(blueButton);

add(redButton);

add(j1);

yellowButtofl.addActionListener(this);

blueButton.addActionListener(this);

redButton.addActionListener(this);

}

public void actionPerformed(ActionEvent evt){

0bject source=evt.getSource;

Color color=getForeground;

if(source= =yellowButton)color=Color.

yellow;

else if(source= =blueButton)color=Color.

blue;

else if(source= =redButton)color=

Color.red;

        

        

}

private JButton yellowButton;

private JButton blueButton;

private JButton redButton;

private JLabel jl;

}

class ButtonFrame. extends JFrame{

public ButtonFrame{

setTitle("exam l6");

setSize(300,200);

addWindowListener(new WindowAdapter{

public void windowClosing(WindowEvent e){

System.exit(O);

}

});

Container contentPane=getContentPane;

contentPane.add(new ButtonPanel);

}

}

public class java2{

public static void main(String[]args){

JFrame. frame=new ButtonFrame;

frame.show; 

}

}

设计软件结构是在软件生命周期的(  )。

  • A.软件定义期 
  • B.软件开发期
  • C.软件维护期 
  • D.以上3个都不是

下面(  )是合法的标识符。

  • A.$persons 
  • B.2Users
  • C.*point
  • D.this

按运算符的功能划分,运算符”+=”的类型是(  )。

  • A.算术运算符 
  • B.关系运算符
  • C.逻辑运算符 
  • D.赋值运算符

下列关于数据存储方式的叙述中,不正确的是(  )。

  • A.数据的主要存储方式有顺序存储结构和链式存储结构
  • B.顺序存储结构存储空间利用率低,链式存储结构空间利用率高
  • C.顺序存储结构的结点只有自身域,链式存储结构除了自身域还包括指针域
  • D.顺序存储结构可通过计算直接确定数据结构中某个结点的存储地址

SQL语言又称为(  )。

  • A.结构化定义语言 
  • B.结构化控制语言
  • C.结构化查询语言 
  • D.结构化操纵语言
¥

订单号:

遇到问题请联系在线客服

订单号:

遇到问题请联系在线客服