
[简答题] 本题的功能是对下拉菜单项的操作,包括添加和删除。页面包括一个下拉菜单、一个文本框和两个按钮“删除”和“添加”,选中下拉菜单的一项后,可以通过“删除”按钮从下拉菜单中删除该项,在文本框中填入字符串后,单击“添加”按钮就可以将该项添加到下拉菜单中,所有信息都将显示在右侧的文本域中。 import java.awt.*; import java.awt.event.*; public class java2 extends java.applet.Applet imple- ments hemListener,ActionListener {Choice choice; TextField text; TextArea area; Button add,del; public void init() . {choice:new Choice(); text=new TextField(8); area:new TextArea(6,15); choice.add("音乐天地"); choice.add("武术天地"); choice.add("象棋乐园"); choice.add("交友聊天"); add=new Button("添加"); del=new Button("删除"); add.addActionListener(this); del.addActionListener(this); choice.addItemListener(this); add(choice); add(del);add(text);add(add);add(area); } public void itemStateChanged(hemEvent e) {String name= ; int index=choice.getSelectedIndex(); area.setText("\n"+index+":"+name); } public void actionPerformed(ActionEvent e) {if(e.getSource()= =add||e.getSource()= = text) {String name=text.getText(); if(name.length()>0) {choice.add(name); choice.select(name); area.append("\n添加"+name); } } else if(e.getSource()= =del) {choice.remove( ); area.append("\n删除"+choice.getSelectedItem ()); } } }
请在此输入您的答案
6[简答题] 本题使用下拉菜单来控制字体,窗口中有一个标签和一个下拉菜单,当选中下拉菜单中的任一项字体时,标签上字符串的字体就随之改变。 import java.awt.*; import java.awt.event.*; import javax.swing.*; class ComboBoxFrame extends JFrame { public ComboBoxFrame(){ setTitle("java2"); setSize(300,200); addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); style=new JComboBox(): style.setEditable(true); style.addhem("Serif"); style.addItem("SansSerif"); style.addhem("Monospaced"); style.addhem("Dialog"); style.addhem("Dialoglnput"); style.addActionListener(this); JPanel p=new JPanel(); P.add(style); getContentPane().add(p,"South"); panel=new ComboBoxTestPanel(); getContentPane().add(panel,"Center"); } public void actionPerformed(ActionEvent evt){ JComboBox source=(JComboBox) ; String item=(String)source.getSelectedhem(): panel.setStyle(item); } private ComboBoxTestPanel panel; private JComboBox style; } class ComboBoxTestPanel extends JPanel{ public ComboBoxTestPanel(){ setStyle("Serif"); } public void setStyle(String s){ setFont(new Font(S,Font.PLAIN,12)); repaint(); } public void paintComponent(Graphics g){ super.paintComponent(g); 9.drawString("Welcome to China!",0,50); } } public class java2{ public static void main(String[]args){ JFrame frame=new ComboBoxFrame(); frame.show(); } }
请在此输入您的答案
7[简答题] 本题中,生成一个窗口,该窗口的长、宽为屏幕长、宽的一半,并且窗口的大小不能改变。 import java.awt.*; import javax.swing.*; public class java2 { public.static void main(String[]args) { FrameSize frame=new FrameSize(); frame.setDefaultCloseoperation(JFrame.EXIT ON_CLOSE); frame.show(); } } class FrameSize extends JFrame { public FrameSize() { setTitle("java2"); Toolkit tk=Toolkit.getDefaultToolkit(); Dimension screenSize= ; int screenHeight=screenSize.height; int screenWidth=screenSize.width; setSize(screenWidth/2,sereenHeight/2); ; }
请在此输入您的答案
8[简答题]
本题中定义了一个简单的计算器,可以进行基本的四则运算。程序中包含16个按钮用来表示0~9、+、-、 *、/、一运算符和小数点,程序顶部的文本框用来显示操作数以及结果。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class java2{
public static void main(String[]args){
try{
UIManager.setLookAndFeel(UIManager.getSys-
temLookAndFeelClassName());
}
catch(Exception e){}
JFrame frame=new CalculatorFrame();
frame.show();
}
}
class CalculatorPanel extends JPanel implements Ac-
tionListener{
private JTextField display;
private JButton btn;
private double arg=0;
private String op="=";
private boolean start=true;
public CalculatorPanel(){
setLayout(new BorderLayout());
display=new JTextField("0");
display.setEditable(false);
add(display,"North");
JPanel P=new JPanel();
P.setLayout(new GridLayout(4,4));
String buttons="789/456*123-0.=+":
for(int i=0;i
正在阅读:
2017年全国计算机等级考试《二级JAVA》冲刺试题:简单应用07-16
我的读书希望作文800字01-06
2019年哈尔滨市中考语文试题:2019年青海海南中考语文试题11-08
5.12国际护士节演讲稿天使有爱没有怨12-17
九年级关于叙事的作文02-27
中国人事考试网:2016年海南结构工程师成绩查询网站03-28
竞选搞笑演讲稿大全|竞选演讲稿【大全】02-27
2020年10月黑龙江自考报名时间:9月1日-9月11日11-08
2020劳动节初一作文600字12-17