日期:2014-05-20  浏览次数:20610 次

JAVA中“选项卡”功能是怎样实现的?
JAVA中“选项卡”功能是怎样实现的?
就是像在桌面上右键菜单中点“属性”时弹出来的那种界面,
有“主题”、“桌面”、“屏保”之类的那种选项卡,
可以在一个选项卡中点“应用”来保存结果,也可以最后一起点“确定”来保存。
这一系列的功能是怎样实现的?
如果没有详细的实现过程,就帮忙把涉及到的方法给出来就可以。
多谢了!

------解决方案--------------------
呵呵,给你写了一个以假乱真的,自己完善一下吧。供参考:
Java code

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class TabbedDialog extends JDialog implements ActionListener {
    
    private Map<String, JPanel> panels;
    private MainPane mainPane;
    private JButton ok = new JButton("OK");
    private JButton cancel = new JButton("Cancel");
    private JButton apply = new JButton("Apply");
    
    private class MainPane extends JPanel {

        JTabbedPane tabs;

        MainPane() {
            setLayout(new BorderLayout(6, 6));
            tabs = new JTabbedPane();
            add(BorderLayout.CENTER, tabs);
            add(BorderLayout.SOUTH, new Buttons());
        }
        
        public Component addTab(String title, Component component) {
            return tabs.add(title, component);
        }
        
        public Insets getInsets() {
            return new Insets(6, 6, 6, 6);
        }
    }
    
    private class General extends JPanel {
        public void paint(Graphics g) {
            g.drawString("System:", 120, 50);
            g.drawString("Microsoft Windows XP Professional", 160, 62);
        }
        //自己完成...
    }

    private class ComputerName extends JPanel {
        //自己完成...
    }
    
    private class Hardware extends JPanel {
        //自己完成...
    }
    
    private class Advanced extends JPanel {
        //自己完成...
    }
    
    private class SystemRestore extends JPanel {
        //自己完成...
    }
    
    private class AutomaticUpdates extends JPanel {
        //自己完成...
    }
    
    private class Remote extends JPanel {
        //自己完成...
    }
    
    private class Buttons extends JPanel {
        Buttons() {
            setLayout(new FlowLayout(FlowLayout.RIGHT, 6, 0));
            add(ok);
            add(cancel);
            add(apply);
        }
    }
    
    public void actionPerformed(ActionEvent e) {
        if (!e.getActionCommand().equals("Cancel"))
            save();
        if (!e.getActionCommand().equals("Apply"))
            exit();
    }
    
    private void exit() {
        System.exit(0);
    }

    private void save() {
        //自己完成...
        JOptionPane.showMessageDialog(this, "Settings saved");
    }

    public TabbedDialog() {
        init();
        
        setTitle("System Properties");
        setSize(420, 480);
        
        mainPane = new MainPane();
        setContentPane(mainPane);
        setVisible(true);
        
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                exit();
            }
        });
        
        ok.addActionListener(this);
        cancel.addActionListener(this);
        apply.addActionListener(this);
        
        installTabs();
    }
    
    private void init() {
        panels = new LinkedHashMap<String, JPanel>();
        panels.put("General", new General());
        panels.put("Computer Name", new ComputerName());
        panels.put("Hardware", new Hardware());
        panels.put("Advanced", new Advanced());
        panels.put("System Restore", new SystemRestore());
        panels.put("Automatic Updates", new AutomaticUpdates());
        panels.put("Remote", new Remote());
    }
    
    private void installTabs() {
        for (String s : panels.keySet())
            mainPane.addTab(s, panels.get(s));
    }
    
    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch(Exception e) { }
        new TabbedDialog();
    }

}