[簡答題]
本題的功能是對下拉菜單項(xiàng)的操作,包括添加和刪除。頁面包括一個下拉菜單、一個文本框和兩個按鈕“刪除”和“添加”,選中下拉菜單的一項(xiàng)后,可以通過“刪除”按鈕從下拉菜單中刪除該項(xiàng),在文本框中填入字符串后,單擊“添加”按鈕就可以將該項(xiàng)添加到下拉菜單中,所有信息都將顯示在右側(cè)的文本域中。
import java.a(chǎn)wt.*;
import java.a(chǎn)wt.event.*;
public class java2 extends java.a(chǎn)pplet.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.a(chǎn)dd("音樂天地");
choice.a(chǎn)dd("武術(shù)天地");
choice.a(chǎn)dd("象棋樂園");
choice.a(chǎn)dd("交友聊天");
add=new Button("添加");
del=new Button("刪除");
add.a(chǎn)ddActionListener(this);
del.a(chǎn)ddActionListener(this);
choice.a(chǎn)ddItemListener(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.a(chǎn)dd(name);
choice.select(name);
area.a(chǎn)ppend("\n添加"+name);
}
}
else if(e.getSource()= =del)
{choice.remove( );
area.a(chǎn)ppend("\n刪除"+choice.getSelectedItem
());
}
}
}
請?jiān)诖溯斎肽拇鸢?BR> 6[簡答題] 本題使用下拉菜單來控制字體,窗口中有一個標(biāo)簽和一個下拉菜單,當(dāng)選中下拉菜單中的任一項(xiàng)字體時,標(biāo)簽上字符串的字體就隨之改變。 import java.a(chǎn)wt.*; import java.a(chǎn)wt.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.a(chǎn)ddhem("Serif"); style.a(chǎn)ddItem("SansSerif"); style.a(chǎn)ddhem("Monospaced"); style.a(chǎn)ddhem("Dialog"); style.a(chǎn)ddhem("Dialoglnput"); style.a(chǎn)ddActionListener(this); JPanel p=new JPanel(); P.a(chǎn)dd(style); getContentPane().a(chǎn)dd(p,"South"); panel=new ComboBoxTestPanel(); getContentPane().a(chǎn)dd(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,F(xiàn)ont.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(); } }
請?jiān)诖溯斎肽拇鸢?BR> 7[簡答題] 本題中,生成一個窗口,該窗口的長、寬為屏幕長、寬的一半,并且窗口的大小不能改變。 import java.a(chǎn)wt.*; 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); ; }
請?jiān)诖溯斎肽拇鸢?BR> 8[簡答題] 本題中定義了一個簡單的計(jì)算器,可以進(jìn)行基本的四則運(yùn)算。程序中包含16個按鈕用來表示0~9、+、-、 *、/、一運(yùn)算符和小數(shù)點(diǎn),程序頂部的文本框用來顯示操作數(shù)以及結(jié)果。 import java.a(chǎn)wt.*; import java.a(chǎn)wt.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
請?jiān)诖溯斎肽拇鸢?BR> 6[簡答題] 本題使用下拉菜單來控制字體,窗口中有一個標(biāo)簽和一個下拉菜單,當(dāng)選中下拉菜單中的任一項(xiàng)字體時,標(biāo)簽上字符串的字體就隨之改變。 import java.a(chǎn)wt.*; import java.a(chǎn)wt.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.a(chǎn)ddhem("Serif"); style.a(chǎn)ddItem("SansSerif"); style.a(chǎn)ddhem("Monospaced"); style.a(chǎn)ddhem("Dialog"); style.a(chǎn)ddhem("Dialoglnput"); style.a(chǎn)ddActionListener(this); JPanel p=new JPanel(); P.a(chǎn)dd(style); getContentPane().a(chǎn)dd(p,"South"); panel=new ComboBoxTestPanel(); getContentPane().a(chǎn)dd(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,F(xiàn)ont.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(); } }
請?jiān)诖溯斎肽拇鸢?BR> 7[簡答題] 本題中,生成一個窗口,該窗口的長、寬為屏幕長、寬的一半,并且窗口的大小不能改變。 import java.a(chǎn)wt.*; 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); ; }
請?jiān)诖溯斎肽拇鸢?BR> 8[簡答題] 本題中定義了一個簡單的計(jì)算器,可以進(jìn)行基本的四則運(yùn)算。程序中包含16個按鈕用來表示0~9、+、-、 *、/、一運(yùn)算符和小數(shù)點(diǎn),程序頂部的文本框用來顯示操作數(shù)以及結(jié)果。 import java.a(chǎn)wt.*; import java.a(chǎn)wt.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

