期末要做项目了,几个问题问下大家1)java swing中 按alt键来回切换菜单栏和其他板块(比如 windows的笔记本等等)2) java swing中 菜单左上角的小图标 怎么设置, 怎么将.png、.gif等图片转成 image类3)java swing中 cardlayout具体怎么使用,希望能提供一小段代码4) 最后能讲下比较容易实现的 加密算法 的工作原理

解决方案 »

  1.   

    用javax swing有点烦,该显示的组件不能显示
      

  2.   

    cardlayout
    我很少用这个哦..
    书上有说啊..
    一次只能显示一个组件..
      

  3.   

    1)设置窗口alt键监听事件,来控制切换可以实现
    3)cardlayout布局,可以使用事件来切换它所管理的每个组件
    4)最简单的加密解密就是使用异或一个整数来加密,解密也同样异或这个整数
      

  4.   

    切换你用个事件,按下alt,焦点会向下走一步。。你在JDK中查下有个image类,它里面有你需要的代码。。cardLayout是卡片布局了,怎么用就是比如说你有一叠扑克牌,使用卡片布局将他们装起来,当你点击一个按钮(“第一张牌”)时,它会显示第一张牌,当你点击(“后一张”)时,它会显示后面一张牌,比如A之后是2.。。加密算法简单的就是异或了,,我没用过加密算法,对于密码什么的,都是用password的。。
      

  5.   

    这个是以前写的。。关于卡片布局的。import java.awt.BorderLayout;
    import java.awt.CardLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.Box;
    import javax.swing.BoxLayout;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingConstants;public class CardLayoutManager extends JFrame implements ActionListener {
      /**
       * 设置左右两个面板,leftPanel存放五个按钮,rightPanel存放是个标签
       */
      private JPanel leftPanel, rightPanel;  /**
       * 设置右边面板的布局管理器为卡片布局
       */
      private CardLayout cl = new CardLayout();  private Container container = this.getContentPane();  public CardLayoutManager(String title) {
        super(title);
        this.setSize(500, 600);
        container.setLayout(new BorderLayout());
        container.add(buildLeftPanel(), BorderLayout.WEST);
        container.add(buildRightPanel());
      }  /**
       * 构造左边按钮面板
       * 
       * @return
       */
      /*public JPanel buildLeftPanel() {
        if (leftPanel == null) {
          leftPanel = new JPanel();
          leftPanel.setLayout(new GridLayout(5, 1, 10, 0));
          
          leftPanel.add(buildBtn("第一张"));
          leftPanel.add(buildBtn("前一张"));
          leftPanel.add(buildBtn("后一张"));
          leftPanel.add(buildBtn("第3张"));
          leftPanel.add(buildBtn("最后一张"));
        }
        return leftPanel;
      }*/
      
      public Box buildLeftPanel(){
        Box box = new Box(BoxLayout.Y_AXIS);
        box.add(box.createHorizontalStrut(30)); 
        box.add(buildBtn("第一张"));
        box.add(box.createHorizontalStrut(30)); 
        box.add(buildBtn("前一张"));
        box.add(box.createHorizontalStrut(30)); 
        box.add(buildBtn("后一张"));
        box.add(box.createHorizontalStrut(30)); 
        box.add(buildBtn("第3张"));
        box.add(box.createHorizontalStrut(30)); 
        box.add(buildBtn("最后一张"));
        box.add(box.createHorizontalStrut(30)); 
        return box;
      }
      /**
       * 构造右边标签面板
       * 
       * @return
       */
      public JPanel buildRightPanel() {
        if (rightPanel == null) {
          rightPanel = new JPanel();
          rightPanel.setBackground(Color.BLUE);
          rightPanel.setLayout(cl);
          rightPanel.add(buildJLabel("红桃尖", "icons/0.gif"), "1");
          rightPanel.add(buildJLabel("红桃贰", "icons/1.gif"), "2");
          rightPanel.add(buildJLabel("红桃叁", "icons/2.gif"), "3");
          rightPanel.add(buildJLabel("红桃肆", "icons/3.gif"), "4");
          rightPanel.add(buildJLabel("红桃伍", "icons/4.gif"), "5");
          rightPanel.add(buildJLabel("红桃六", "icons/5.gif"), "6");
          rightPanel.add(buildJLabel("红桃柒", "icons/6.gif"), "7");
          rightPanel.add(buildJLabel("红桃捌", "icons/7.gif"), "8");
          rightPanel.add(buildJLabel("红桃玖", "icons/8.gif"), "9");
          rightPanel.add(buildJLabel("红桃拾", "icons/9.gif"), "10");
          rightPanel.add(buildJLabel("小鬼", "icons/10.gif"), "11");
          rightPanel.add(buildJLabel("大鬼", "icons/11.gif"), "12");    }
        return rightPanel;
      }  private JLabel buildJLabel(String name, String icon) {
        //构造jlabel对象,设置字体居中显示
        JLabel lbl = new JLabel(name, JLabel.CENTER);
        //给标签对象加载图片
        lbl.setIcon(new ImageIcon(icon));
        //设置字体颜色
        lbl.setForeground(Color.RED);
        //设置标签上的字体风格为粗体,大小为30
        lbl.setFont(new Font(name, Font.ITALIC, 30));
        //设置标签上的文本居中
        lbl.setHorizontalTextPosition(SwingConstants.CENTER);
        //设置标签图片位于标签对象的底部
        lbl.setVerticalTextPosition(SwingConstants.BOTTOM);
        return lbl;
      }  private JButton buildBtn(String name) {
        JButton btn = new JButton(name);
        btn.addActionListener(this);
        return btn;
      }  public void actionPerformed(ActionEvent e) {
        String name = e.getActionCommand();
        if (name.equals("第一张")) {
          cl.first(rightPanel);
        } else if (name.equals("前一张")) {
          cl.previous(rightPanel);
        } else if (name.equals("后一张")) {
          cl.next(rightPanel);
        } else if (name.equals("第3张")) {
          cl.show(rightPanel, "3");
        } else if (name.equals("最后一张")) {
          cl.last(rightPanel);
        }
      }  /**
       * @param args
       */
      public static void main(String[] args) {
        new CardLayoutManager("卡片布局").setVisible(true);
      }}
      

  6.   

    关于CardLayout上边已经有代码了
    我就不写了
    上边是经典的翻牌
    就是用CardLayout完成的关于alt+的问题我只实现ALT + H
    其他的相信你能举一反三的
    /**
    1)java swing中 按alt键来回切换菜单栏和其他板块(比如 windows的笔记本等等) 2) java swing中 菜单左上角的小图标 怎么设置, 怎么将.png、.gif等图片转成 image类 3)java swing中 cardlayout具体怎么使用,希望能提供一小段代码 4) 最后能讲下比较容易实现的 加密算法 的工作原理 
    */import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class AboutSwing {
    public static void main(String[] args) {
    new AboutSwingFrame();
    }
    }class AboutSwingFrame extends JFrame {
    private Action saveAction;
    private Action saveAsAction;
    private JCheckBoxMenuItem readonlyItem;
    private JPopupMenu popup;

    private JPanel mainPanel = new JPanel();
    private JPanel firstPanel = new JPanel();
    private JPanel secondPanel = new JPanel();
     
    private JLabel before = new JLabel("加密前的数字");
    private JLabel after = new JLabel("加密后的数字");
    JButton confuse = new JButton("加密");
    JTextField beTF = new JTextField(10);
    JTextField afTF = new JTextField(10);

    public AboutSwingFrame() {
    JMenu fileMenu = new JMenu("File");
    JMenuItem newItem = fileMenu.add(new TestAction("New"));
    JMenuItem openItem = fileMenu.add(new TestAction("Open"));
    openItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK));
    fileMenu.addSeparator();

    saveAction = new TestAction("Save");
    JMenuItem saveItem = fileMenu.add(saveAction);
    saveItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK));

    saveAsAction = new TestAction("Save As");
    JMenuItem saveAsItem = fileMenu.add(saveAsAction);
    fileMenu.addSeparator();

    fileMenu.add(new AbstractAction("Exit") {
    public void actionPerformed(ActionEvent event) {
    System.exit(0);
    }
    });

    readonlyItem = new JCheckBoxMenuItem("Read-only");
    readonlyItem.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent event) {
    boolean saveOK = !readonlyItem.isSelected();
    saveAction.setEnabled(saveOK);
    saveAsAction.setEnabled(saveOK);
    }
    });

    ButtonGroup group = new ButtonGroup();

    JRadioButtonMenuItem insertItem = new JRadioButtonMenuItem("Insert");
    insertItem.setSelected(true);
    JRadioButtonMenuItem overtypeItem = new JRadioButtonMenuItem("Overtype");

    group.add(insertItem);
    group.add(overtypeItem);

    Action cutAction = new TestAction("Cut");
    cutAction.putValue(Action.SMALL_ICON,new ImageIcon("cut.gif"));
    Action copyAction = new TestAction("Copy");
    copyAction.putValue(Action.SMALL_ICON, new ImageIcon("copy.gif"));
    Action pasteAction = new TestAction("Paste");
    pasteAction.putValue(Action.SMALL_ICON, new ImageIcon("paste.gif"));

    JMenu editMenu = new JMenu("Edit");
    editMenu.add(cutAction);
    editMenu.add(copyAction);
    editMenu.add(pasteAction);

    JMenu optionMenu = new JMenu("Options");

    optionMenu.add(readonlyItem);
    optionMenu.addSeparator();
    optionMenu.add(insertItem);
    optionMenu.add(overtypeItem);

    editMenu.addSeparator();
    editMenu.add(optionMenu);

    JMenu helpMenu = new JMenu("Help");
    helpMenu.setMnemonic('H');

    JMenuItem indexItem = new JMenuItem("Index");
    indexItem.setMnemonic('I');
    helpMenu.add(indexItem);

    Action aboutAction = new TestAction("About");
    aboutAction.putValue(Action.MNEMONIC_KEY, new Integer('A'));
    helpMenu.add(aboutAction);

    JMenuBar menuBar = new JMenuBar();
    setJMenuBar(menuBar);

    menuBar.add(fileMenu);
    menuBar.add(editMenu);
    menuBar.add(helpMenu);

    popup = new JPopupMenu();
    popup.add(cutAction);
    popup.add(copyAction);
    popup.add(pasteAction);

    JPanel panel = new JPanel();
    panel.setComponentPopupMenu(popup);
    add(panel);

    panel.addMouseListener(new MouseAdapter(){});

    confuse.addActionListener(new ConfuseHandler());
    firstPanel.setLayout(new GridLayout(3,1));
    JPanel p1 = new JPanel();
    JPanel p2 = new JPanel();
    JPanel p3 = new JPanel();
    p1.add(before);
    p1.add(after);
    p2.add(beTF);
    p2.add(afTF);
    p3.add(confuse);
    firstPanel.add(p1);
    firstPanel.add(p2);
    firstPanel.add(p3);
    mainPanel.add(firstPanel);
    mainPanel.setLayout(new GridLayout(2,1));
    add(mainPanel);

    setSize(300,250);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    }

    private class ConfuseHandler implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    String str1 = beTF.getText();
    Integer temp =  Integer.parseInt(str1);
    Integer result = temp*100/4+20;
    String str2 = String.valueOf(result);
    afTF.setText(str2);
    }
    }
    }class TestAction extends AbstractAction {
    public TestAction(String name) {
    super(name);
    }

    public void actionPerformed(ActionEvent event) {
    System.out.println(getValue(Action.NAME)+"selected.");
    }
    }
      

  7.   

    11楼的谢了,分不会忘的alt切换的问题还是不会啊,alt+‘char’的快键键那我知道,关系应该不大吧