期末要做项目了,几个问题问下大家1)java swing中 按alt键来回切换菜单栏和其他板块(比如 windows的笔记本等等)2) java swing中 菜单左上角的小图标 怎么设置, 怎么将.png、.gif等图片转成 image类3)java swing中 cardlayout具体怎么使用,希望能提供一小段代码4) 最后能讲下比较容易实现的 加密算法 的工作原理
解决方案 »
- 还是线程问题
- sos!!出现个小问题,点击注册 就弹出了个 下载窗口,迅雷提示下载!是什么原因啊??
- 正则表达符同时匹配两个以上字符的问题
- 请问sun官网上的Sun ONE Studio 5下载地址在哪里啊?
- sqlServer2005sql 跟新文件
- 字符串分割问题
- 一个简单的类型转换问题,请高手帮忙解决!---thanks
- vector中排序的问题,请指教
- Full thread dump Classic VM (Linux_JDK_1.2.2_RC2, native threads),Why?
- 为什么输入null不抛出异常呢?
- 编写程序,求1+3+7+15+31…………+(2的10次方-1)在线等,菜鸟问题
- 一个关于静态函数调用,提高效率的小问题。请指教!
我很少用这个哦..
书上有说啊..
一次只能显示一个组件..
3)cardlayout布局,可以使用事件来切换它所管理的每个组件
4)最简单的加密解密就是使用异或一个整数来加密,解密也同样异或这个整数
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);
}}
我就不写了
上边是经典的翻牌
就是用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.");
}
}