java中作出GUI有标签但是无图片显示的问题,代码如下,已经不止一次碰到此类问题。我用的是1.6_14

packageMyDesktopPane;importjavax.swing.*;importjava.awt.event.*;importjava.awt.*;publ... package MyDesktopPane;
import javax.swing.*;

import java.awt.event.*;
import java.awt.*;
public class MyDesktopPane extends JFrame implements ActionListener{
final static JDesktopPane desktopPane=new JDesktopPane();
public MyDesktopPane()
{
super("MyDesktopPane.java:DesktopPane测试");
JMenuBar menuBar=new JMenuBar();
JMenu menu=new JMenu("新增窗口");
JMenuItem menultem=new JMenuItem("内部框架窗口");
menu.add(menultem);
menuBar.add(menu);
setJMenuBar(menuBar);
getContentPane().add(desktopPane);
menultem.addActionListener(this);
setSize(350,200);
show();
}
public void actionPerformed(ActionEvent e)
{
JInternalFrame inFrame=new JInternalFrame("内部框架(圆环)",true,true,true,true);
Container c=inFrame.getContentPane();
CirclePanel circlePanel=new CirclePanel();
JLabel label=new JLabel("圆环");
c.add(circlePanel,BorderLayout.CENTER);
c.add(label,BorderLayout.WEST);
int w=circlePanel.getImageWidthHeight().width+150;
int h=circlePanel.getImageWidthHeight().height+50;
inFrame.setSize(w,h);
inFrame.reshape(100, 50, w, h);
inFrame.setOpaque(true);
desktopPane.add(inFrame);
}
public static void main(String args[])
{
MyDesktopPane app=new MyDesktopPane();
app.addWindowListener(new MyWindowListener());
}
}
class CirclePanel extends JPanel
{
private ImageIcon imglcon;
public CirclePanel()
{
imglcon=new ImageIcon("circle.gif");
}
public void paintComponent(Graphics g)
{
imglcon.paintIcon(this, g, 0, 0);
}
public Dimension getImageWidthHeight()
{
return new Dimension(imglcon.getIconWidth(),imglcon.getIconHeight());
}
}
以上代码可以运行并跳出可选择BUTTON,但是图像没有显示,另外MyWindowListener是这样的
package MyDesktopPane;
import java.awt.*;
import java.awt.event.*;
class MyWindowListener extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(1);
}
}
另外昨天我也照书做了个comboBox,也无对应的图片显示,代码如下,当然叶永道了MyWindowListener这个windows监听
c.setLayout(new FlowLayout());
comboBox.setMaximumRowCount(3);
comboBox.addItemListener(this);
c.add(comboBox);
c.add(label);
setSize(300,100);
show();
}
public void itemStateChanged(ItemEvent e)
{
label.setIcon(icon[comboBox.getSelectedIndex()]);
}
public static void main(String args[])
{
MyComboBox app=new MyComboBox();
app.addWindowListener(new MyWindowListener());
}
}
展开
 我来答
章晓健
2009-10-24
知道答主
回答量:1
采纳率:0%
帮助的人:0
展开全部
在super()后加上 流式布局setLayout(new FlowLayout()) 这条语句就可以看到图片了
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
daay1986
2009-10-23 · TA获得超过6018个赞
知道大有可为答主
回答量:2208
采纳率:0%
帮助的人:1454万
展开全部
public class MyDesktopPane extends JFrame implements ActionListener {

final static JDesktopPane desktopPane = new JDesktopPane();

public MyDesktopPane() {
super("MyDesktopPane.java:DesktopPane测试");
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("新增窗口");
JMenuItem menultem = new JMenuItem("内部框架窗口");
menu.add(menultem);
menuBar.add(menu);
setJMenuBar(menuBar);
getContentPane().add(desktopPane);
menultem.addActionListener(this);
setSize(350, 200);
setVisible(true);
}

public void actionPerformed(ActionEvent e) {
JInternalFrame inFrame = new JInternalFrame("内部框架(圆环)", true, true, true, true);
Container c = inFrame.getContentPane();
CirclePanel circlePanel = new CirclePanel();
JLabel label = new JLabel("圆环");
c.add(circlePanel, BorderLayout.CENTER);
c.add(label, BorderLayout.WEST);
int w = circlePanel.getImageWidthHeight().width + 150;
int h = circlePanel.getImageWidthHeight().height + 50;
inFrame.setSize(w, h);
inFrame.reshape(100, 50, w, h);
inFrame.setOpaque(true);
desktopPane.add(inFrame);
inFrame.setVisible(true);
}

public static void main(String args[]) {
MyDesktopPane app = new MyDesktopPane();
app.addWindowListener(new MyWindowListener());
}
}

class CirclePanel extends JPanel {

private ImageIcon imglcon;

public CirclePanel() {
try {
imglcon = new ImageIcon(new URL("https://gss0.bdstatic.com/70cFsjip0QIZ8tyhnq/img/logo-zhidao.gif"));
} catch (MalformedURLException ex) {
Logger.getLogger(CirclePanel.class.getName()).log(Level.SEVERE, null, ex);
}
}

public void paint(Graphics g) {
g.drawImage(imglcon.getImage(), 0, 0, this);
}

public Dimension getImageWidthHeight() {
return new Dimension(imglcon.getIconWidth(), imglcon.getIconHeight());
}
}

class MyWindowListener extends WindowAdapter {

public void windowClosing(WindowEvent e) {
System.exit(1);
}
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式