[簡答題]
本題的功能是對下拉菜單項的操作,包括添加和刪除。頁面包括一個下拉菜單、一個文本框和兩個按鈕“刪除”和“添加”,選中下拉菜單的一項后,可以通過“刪除”按鈕從下拉菜單中刪除該項,在文本框中填入字符串后,單擊“添加”按鈕就可以將該項添加到下拉菜單中,所有信息都將顯示在右側的文本域中。
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
請在此輸入您的答案
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

