各位大哥,小弟肯求帮忙。
我在一本书上面看到一段程序,但是在本地电脑上面怎么都调试不成功。 表现为图片按钮显示不出来
主要的问题如下:
代码如下:public class JXCInfo {
private JLabel backLabel;
private JDesktopPane desktopPane;
private JPanel sysManagePanel;
private JFrame frame;
private Map<String, JInternalFrame> ifs = new HashMap<String, JInternalFrame>();
// int width=Toolkit.getDefaultToolkit().getScreenSize().width;
// int height=Toolkit.getDefaultToolkit().getScreenSize().height;
public JXCInfo(){
//程序主面板容器
frame=new JFrame("信息管理部进销存系统");
//添加事件监听
frame.getContentPane().setBackground(new Color(170, 188, 120));
frame.addComponentListener(new FrameListener());
frame.getContentPane().setLayout(new BorderLayout());
frame.setBounds(100, 100, 800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
backLabel = new JLabel();// 背景标签
backLabel.setVerticalAlignment(SwingConstants.TOP);
backLabel.setHorizontalAlignment(SwingConstants.CENTER);
updateBackImage(); // 更新或初始化背景图片
desktopPane = new JDesktopPane();
desktopPane.add(backLabel, new Integer(Integer.MIN_VALUE));
frame.getContentPane().add(desktopPane);
JTabbedPane navigationPanel = createNavigationPanel(); // 创建导航标签面板
frame.getContentPane().add(navigationPanel, BorderLayout.NORTH);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Login();
}
});
}
private JTabbedPane createNavigationPanel() { // 创建导航标签面板的方法
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.setFocusable(false);
tabbedPane.setBackground(new Color(211, 230, 192));
tabbedPane.setBorder(new BevelBorder(BevelBorder.RAISED)); JPanel baseManagePanel = new JPanel(); // 基础信息管理面板
baseManagePanel.setBackground(new Color(215, 223, 194));
baseManagePanel.setLayout(new BoxLayout(baseManagePanel,
BoxLayout.X_AXIS));
baseManagePanel.add(createFrameButton("客户信息管理", "KeHuGuanLi"));
baseManagePanel.add(createFrameButton("商品信息管理", "ShangPinGuanLi"));
baseManagePanel.add(createFrameButton("供应商信息管理", "GysGuanLi"));
tabbedPane.addTab(" 基础信息管理 ", null, baseManagePanel, "基础信息管理");
return tabbedPane;
} private JButton createFrameButton(String fName, String cname) {
String imgUrl = "res/ActionIcon/" + fName + ".png";
String imgUrl_roll = "res/ActionIcon/" + fName + "_roll.png";
String imgUrl_down = "res/ActionIcon/" + fName + "_down.png";
Icon icon = new ImageIcon(imgUrl);
Icon icon_roll = null;
if (imgUrl_roll != null)
icon_roll = new ImageIcon(imgUrl_roll);
Icon icon_down = null;
if (imgUrl_down != null)
icon_down = new ImageIcon(imgUrl_down);
Action action = new openFrameAction(fName, cname, icon);
JButton button = new JButton(action);
button.setMargin(new Insets(0, 0, 0, 0));
button.setHideActionText(true);
button.setFocusPainted(false);
button.setBorderPainted(false);
button.setContentAreaFilled(false);
if (icon_roll != null)
button.setRolloverIcon(icon_roll);
if (icon_down != null)
button.setPressedIcon(icon_down);
return button;
}
private JInternalFrame getIFrame(String frameName){
JInternalFrame jf=null;
if(!ifs.containsKey(frameName)){
try{
Class fClass=Class.forName("internalFrame."+frameName);
Constructor constructor=fClass.getConstructor(null);
jf=(JInternalFrame)constructor.newInstance(null);
//将指定的值与此映射中的指定键相关联(可选操作)。
ifs.put(frameName, jf);
}catch(Exception e){
e.printStackTrace();
}
}else
//返回此映射中映射到指定键的值。
jf=ifs.get(frameName);
return jf;
}
// 更新背景图片的方法
private void updateBackImage() {
if (backLabel != null) {
int backw = JXCInfo.this.frame.getWidth();
int backh = frame.getHeight();
backLabel.setSize(backw, backh);
backLabel.setText("<html><body><image width='" + backw
+ "' height='" + (backh - 110) + "' src='"
+ JXCInfo.this.getClass().getResource("welcome.jpg")
+ "'></img></body></html>");
}
}
// 窗体监听器
private final class FrameListener extends ComponentAdapter {
public void componentResized(final ComponentEvent e) {
updateBackImage();
}
}
// 主窗体菜单项的单击事件监听器
protected final class openFrameAction extends AbstractAction {
private String frameName = null;
private openFrameAction() {
}
public openFrameAction(String cname, String frameName, Icon icon) {
this.frameName = frameName;
putValue(Action.NAME, cname);
putValue(Action.SHORT_DESCRIPTION, cname);
putValue(Action.SMALL_ICON, icon);
}
public void actionPerformed(final ActionEvent e) {
JInternalFrame jf = getIFrame(frameName);
// 在内部窗体闭关时,从内部窗体容器ifs对象中清除该窗体。
jf.addInternalFrameListener(new InternalFrameAdapter() {
public void internalFrameClosed(InternalFrameEvent e) {
ifs.remove(frameName);
}
});
if (jf.getDesktopPane() == null) {
desktopPane.add(jf);
jf.setVisible(true);
}
try {
jf.setSelected(true);
} catch (PropertyVetoException e1) {
e1.printStackTrace();
}
}
}
static {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
}}
我在一本书上面看到一段程序,但是在本地电脑上面怎么都调试不成功。 表现为图片按钮显示不出来
主要的问题如下:
代码如下:public class JXCInfo {
private JLabel backLabel;
private JDesktopPane desktopPane;
private JPanel sysManagePanel;
private JFrame frame;
private Map<String, JInternalFrame> ifs = new HashMap<String, JInternalFrame>();
// int width=Toolkit.getDefaultToolkit().getScreenSize().width;
// int height=Toolkit.getDefaultToolkit().getScreenSize().height;
public JXCInfo(){
//程序主面板容器
frame=new JFrame("信息管理部进销存系统");
//添加事件监听
frame.getContentPane().setBackground(new Color(170, 188, 120));
frame.addComponentListener(new FrameListener());
frame.getContentPane().setLayout(new BorderLayout());
frame.setBounds(100, 100, 800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
backLabel = new JLabel();// 背景标签
backLabel.setVerticalAlignment(SwingConstants.TOP);
backLabel.setHorizontalAlignment(SwingConstants.CENTER);
updateBackImage(); // 更新或初始化背景图片
desktopPane = new JDesktopPane();
desktopPane.add(backLabel, new Integer(Integer.MIN_VALUE));
frame.getContentPane().add(desktopPane);
JTabbedPane navigationPanel = createNavigationPanel(); // 创建导航标签面板
frame.getContentPane().add(navigationPanel, BorderLayout.NORTH);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Login();
}
});
}
private JTabbedPane createNavigationPanel() { // 创建导航标签面板的方法
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.setFocusable(false);
tabbedPane.setBackground(new Color(211, 230, 192));
tabbedPane.setBorder(new BevelBorder(BevelBorder.RAISED)); JPanel baseManagePanel = new JPanel(); // 基础信息管理面板
baseManagePanel.setBackground(new Color(215, 223, 194));
baseManagePanel.setLayout(new BoxLayout(baseManagePanel,
BoxLayout.X_AXIS));
baseManagePanel.add(createFrameButton("客户信息管理", "KeHuGuanLi"));
baseManagePanel.add(createFrameButton("商品信息管理", "ShangPinGuanLi"));
baseManagePanel.add(createFrameButton("供应商信息管理", "GysGuanLi"));
tabbedPane.addTab(" 基础信息管理 ", null, baseManagePanel, "基础信息管理");
return tabbedPane;
} private JButton createFrameButton(String fName, String cname) {
String imgUrl = "res/ActionIcon/" + fName + ".png";
String imgUrl_roll = "res/ActionIcon/" + fName + "_roll.png";
String imgUrl_down = "res/ActionIcon/" + fName + "_down.png";
Icon icon = new ImageIcon(imgUrl);
Icon icon_roll = null;
if (imgUrl_roll != null)
icon_roll = new ImageIcon(imgUrl_roll);
Icon icon_down = null;
if (imgUrl_down != null)
icon_down = new ImageIcon(imgUrl_down);
Action action = new openFrameAction(fName, cname, icon);
JButton button = new JButton(action);
button.setMargin(new Insets(0, 0, 0, 0));
button.setHideActionText(true);
button.setFocusPainted(false);
button.setBorderPainted(false);
button.setContentAreaFilled(false);
if (icon_roll != null)
button.setRolloverIcon(icon_roll);
if (icon_down != null)
button.setPressedIcon(icon_down);
return button;
}
private JInternalFrame getIFrame(String frameName){
JInternalFrame jf=null;
if(!ifs.containsKey(frameName)){
try{
Class fClass=Class.forName("internalFrame."+frameName);
Constructor constructor=fClass.getConstructor(null);
jf=(JInternalFrame)constructor.newInstance(null);
//将指定的值与此映射中的指定键相关联(可选操作)。
ifs.put(frameName, jf);
}catch(Exception e){
e.printStackTrace();
}
}else
//返回此映射中映射到指定键的值。
jf=ifs.get(frameName);
return jf;
}
// 更新背景图片的方法
private void updateBackImage() {
if (backLabel != null) {
int backw = JXCInfo.this.frame.getWidth();
int backh = frame.getHeight();
backLabel.setSize(backw, backh);
backLabel.setText("<html><body><image width='" + backw
+ "' height='" + (backh - 110) + "' src='"
+ JXCInfo.this.getClass().getResource("welcome.jpg")
+ "'></img></body></html>");
}
}
// 窗体监听器
private final class FrameListener extends ComponentAdapter {
public void componentResized(final ComponentEvent e) {
updateBackImage();
}
}
// 主窗体菜单项的单击事件监听器
protected final class openFrameAction extends AbstractAction {
private String frameName = null;
private openFrameAction() {
}
public openFrameAction(String cname, String frameName, Icon icon) {
this.frameName = frameName;
putValue(Action.NAME, cname);
putValue(Action.SHORT_DESCRIPTION, cname);
putValue(Action.SMALL_ICON, icon);
}
public void actionPerformed(final ActionEvent e) {
JInternalFrame jf = getIFrame(frameName);
// 在内部窗体闭关时,从内部窗体容器ifs对象中清除该窗体。
jf.addInternalFrameListener(new InternalFrameAdapter() {
public void internalFrameClosed(InternalFrameEvent e) {
ifs.remove(frameName);
}
});
if (jf.getDesktopPane() == null) {
desktopPane.add(jf);
jf.setVisible(true);
}
try {
jf.setSelected(true);
} catch (PropertyVetoException e1) {
e1.printStackTrace();
}
}
}
static {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
}}
2、public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Login();
}
});
}
这段语句调用时,输入正确的用户名跳转到new JXCInfo()里。
baseManagePanel.add(createFrameButton("客户信息管理", "KeHuGuanLi"));
baseManagePanel.add(createFrameButton("商品信息管理", "ShangPinGuanLi"));
baseManagePanel.add(createFrameButton("供应商信息管理", "GysGuanLi"));
String imgUrl_roll = "res/ActionIcon/" + fName + "_roll.png";
String imgUrl_down = "res/ActionIcon/" + fName + "_down.png";
在你的本地有这几个图片吗?
没有的话,当然显示不出来。
开发工具是eclipse
JXCInfo是不是你的项目名啊?
如果是的话你的相对路径就要这要写:
String imgUrl="JXCInfo/res/ActionIcon"+fName+".png";
eclipse和myeclipse中的相对路径都要从项目名称开始写起,这是它们默认的根目录问题,就是从哪个目录开始寻找文件的
它们默认的根目录应该是空作空间,所以要加上项目名找可能进入项目中的文件夹
在相对路径上加上项目名后一定可以
button.setMargin(new Insets(0, 0, 0, 0));
button.setHideActionText(true);
button.setFocusPainted(false);
为什么button.setHideActionText(true);出现错误 显示is undefined for the type button
我的这句话为啥出错JTabbedPane navigationPanel =createNavigationPanel()
createNavigationPanel()
这个方法在那个包里面。。
谢谢楼主