gpt4 book ai didi

java - 将图像添加到 JFrame 时出现问题

转载 作者:行者123 更新时间:2023-11-30 06:37:51 25 4
gpt4 key购买 nike

我在将图片添加到 JFrame 时遇到问题,某些内容可能丢失或写错了。这是类(class):

主类:

public class Tester

{
public static void main(String args[])
{
BorderLayoutFrame borderLayoutFrame = new BorderLayoutFrame();
borderLayoutFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
borderLayoutFrame.setSize(600,600);
borderLayoutFrame.setVisible(true);
}
}

public class BorderLayoutFrame extends JFrame implements ActionListener
{
private JButton buttons[]; // array of buttons to hide portions
private final String names[] = { "North", "South", "East", "West", "Center" };
private BorderLayout layout; // borderlayout object
private PicPanel picture = new PicPanel();

// set up GUI and event handling

public BorderLayoutFrame()
{
super( "Philosofic Problem" );
layout = new BorderLayout( 5, 5 ); // 5 pixel gaps
setLayout( layout ); // set frame layout
buttons = new JButton[ names.length ]; // set size of array

// create JButtons and register listeners for them

for ( int count = 0; count < names.length; count++ )
{
buttons[ count ] = new JButton( names[ count ] );
buttons[ count ].addActionListener( this );
}
add( buttons[ 0 ], BorderLayout.NORTH ); // add button to north
add( buttons[ 1 ], BorderLayout.SOUTH ); // add button to south
add( buttons[ 2 ], BorderLayout.EAST ); // add button to east
add( buttons[ 3 ], BorderLayout.WEST ); // add button to west
add( picture, BorderLayout.CENTER ); // add button to center
}

// handle button events

public void actionPerformed( ActionEvent event )
{

}

}

我尝试将图像添加到布局的中心。

这是图像类:

public class PicPanel extends JPanel
{
Image img;
private int width = 0;
private int height = 0;

public PicPanel()
{
super();
img = Toolkit.getDefaultToolkit().getImage("table.jpg");
}
public void paintComponent(Graphics g)
{
super.paintComponents(g);
if ((width <= 0) || (height <= 0))
{
width = img.getWidth(this);
height = img.getHeight(this);
}
g.drawImage(img,0,0,width,height,this);
}
}

求助,请问是什么问题?谢谢

顺便说一句:我正在使用 eclipse,图像应该在哪个目录中?

最佳答案

您发布的代码存在几个问题:

  • 您应该在 BorderLayoutFrame 类中使用 getContentPane().add() 而不是简单地使用 add()
  • 您真的应该使用 SwingUtilities.invokeLater() 从测试器类启动您的 JFrame。像这样:

 SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
System.setProperty("DEBUG_UI", "true");

BorderLayoutFrame blf = new BorderLayoutFrame();
blf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
blf.setSize(600,600);
blf.setVisible(true);
}
});
  • 不要使用 Toolkit 加载图像!在下面的代码中,如果“Table.jpg”与 PicPanel 在同一个包中,图像将正确加载。

public PicPanel() {
super();
try {
rUrl = getClass().getResource("Table.jpg");
if (rUrl != null) {
img = ImageIO.read(rUrl);
}
} catch (IOException ex) {
Logger.getLogger(PicPanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
  • PicPanel.PaintComponent() 中,您调用 super.paintComponents() 是“s”吗?
  • PicPanel.PaintComponent() 中,您不需要所有宽度/高度的东西,只需这样做:

    g.drawImage(img, 0, 0, getWidth(), getHeight(), this);

同时避免调用 super.paintComponent,因为您正在绘制图像,为什么要让面板绘制呢?

我对你的东西的最终实现:

public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
System.setProperty("DEBUG_UI", "true");

BorderLayoutFrame blf = new BorderLayoutFrame();
blf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
blf.setSize(600,600);
blf.setVisible(true);
}
});
}

}

class BorderLayoutFrame extends JFrame implements ActionListener
{
private final BorderLayout layout;
private final JButton[] buttons;
private final String names[] = {"North", "South", "East", "West", "Center"};

public BorderLayoutFrame() {
super( "Philosofic Problem" );
layout = new BorderLayout( 5, 5 );
getContentPane().setLayout( layout );
buttons = new JButton[ names.length ];

for (int i=0; i<names.length; i++)
{
buttons[i] = new JButton(names[i]);
buttons[i].addActionListener(this);
}

getContentPane().add(buttons[0], BorderLayout.NORTH);
getContentPane().add(buttons[1], BorderLayout.SOUTH);
getContentPane().add(buttons[2], BorderLayout.EAST);
getContentPane().add(buttons[3], BorderLayout.WEST);
getContentPane().add(new PicPanel(), BorderLayout.CENTER);
}

public void actionPerformed(ActionEvent e) {
// ignore
}

}

class PicPanel extends JPanel
{
private URL rUrl;
private BufferedImage img;



public PicPanel() {
super();
try {
rUrl = getClass().getResource("UtilBtn.png");
if (rUrl != null) {
img = ImageIO.read(rUrl);
}
} catch (IOException ex) {
Logger.getLogger(PicPanel.class.getName()).log(Level.SEVERE, null, ex);
}
}

@Override
protected void paintComponent(Graphics g) {
//super.paintComponent(g);

g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
}

}

关于java - 将图像添加到 JFrame 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3006162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com