问答题
本题中使用了选项卡,窗口中有一个选项卡,总共有“系统”、“声卡”、“显卡”、“网卡”和“帮助”5项选项面。单击各个选项的文字标签后,所选中的选项将为当前选项。“系统”选项上有3个复选按钮,分别控制“声卡”、“显卡”和“网卡”三个选项,选中某个按钮后,它所指示的选项就可用,否则不可用。 import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class java3 extends JFrame JTabbedPane config=new JTabbedPane(); public java3() super("java3"); setSize(500,300); setDefaultCloseOperation(EXIT_ON_CLOSE); JPanel configPane=new JPanel(); configPane.setLayout(new BoxLayout(configPane.BoxLayout.Y_AXIS)); JTextArea question=new JTextArea("下面的哪个选项\n"+"你想设置"); question.setEditable(false); question.setMaximumSize(new Dimension(300,50)); question.setAlignmentX(0.0f); question.setBackground(configPane.getBackground()); JCheckBox audioCB=new JCheckBox("声卡",true); JCheckBox nicCB=new JCheckBox("网卡",true); JCheckBox tvCB=new JCheckBox("显示卡",false); configPane.add(Box.createVerticalGlue()); configPane.add(question); configPane.add(audioCB); configPane.add(nicCB); configPane.add(tvCB); configPane.add(Box.createVerticalGlue()); JLabel audioPane=new JLabel("声卡页面"); JLabel nicPane=new JLabel("网卡页面"); JLabel tvPane=new JLabel("显示卡页面"); JLabel helpPane=new JLabel("帮助信息"); audioCB.addItemListener(new TabManager(audioPane)); nicCB.addItemListener(new TabManager(nicPane)); tvCB.addItemListener(new TabManager(tvPane)); config.addTab("系统",null,configPane,"Choose Installed Options"); config.addTab("声卡",null,audioPane,"Audio system configuration"); config.addTab("网卡",null,nicPane,"Networking configuration"); config.addTab("显示卡",null,tvPane,"Video system configuration"); config.addTab("帮助",null,helpPane,"How DoI..."); getContentPane().add(config,BorderLayout.CENTER); class TabManger implements ActionListener Component tab; public TabManager(Component tabToManage) tab=tabToManage; public void ItemStateChanged(ItemEvent ie) int index=config.indexOfComponent(tab); if(index!=-1) config.setEnabledAt(index,ie.getStateChange()==ItemEvent.SELECTED); this.repaint() ; public static void main(String args[]) java3 sc=new java3(); sc.setVisible(true);
【参考答案】
第1处:Class TabManager implements ItemListener 第2处:public......(↓↓↓ 点击下方‘点击查看答案’看完整答案、解析 ↓↓↓)
点击查看答案&解析