JAVA 选项卡程序的实现

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();
    }

}
原文出自:http://topic.csdn.net/u/20080412/19/96b306dd-234e-4cd7-a2ae-e19eef4e0d1b.html?1622072926

如果想要在选项卡上添加控件,可以把panels.put("General", new General());改为:panels.put("General", panel)

然后在panel上增加控件就可以了。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值