gpt4 book ai didi

java - 如何将图片添加到 JTabbedPane - 在空面板布局上?

转载 作者:太空宇宙 更新时间:2023-11-04 07:42:32 25 4
gpt4 key购买 nike

我想在 src 文件夹中的 JTabbed Pane 布局上实现 200 * 200 图片。

我的问题是没有显示任何内容 - 没有错误,没有异常,也没有图片。

我认为我不必声明该目录并将其设置为私有(private),因为它已经包含在 src 文件夹中。

import java.awt.*;
import javax.swing.*;

import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JFrame;



public class Example1
extends JFrame


{
private JTabbedPane tabbedPane;
private JPanel panel1;

public Example1()
{
// NOTE: to reduce the amount of code in this example, it uses
// panels with a NULL layout. This is NOT suitable for
// production code since it may not display correctly for
// a look-and-feel.

setTitle( "Program" );
setSize( 800, 400 );
setBackground( Color.gray );

JPanel topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add( topPanel );

// Create the tab pages
createPage1();


// Create a tabbed pane
tabbedPane = new JTabbedPane();
tabbedPane.addTab( "Tab Page", panel1 );
topPanel.add( tabbedPane, BorderLayout.CENTER );
}

public void createPage1()
{
panel1 = new JPanel();
panel1.setLayout( null );

ImageIcon pic = new ImageIcon("test.png");
JLabel label = new JLabel (pic);
panel1.add(label);
label.setVisible (true);
label.setBounds( 200, 200, 200, 400 );
}

// Main method to get things started
public static void main( String args[] )
{
// Create an instance of the test application
Example1 mainFrame = new Example1();
mainFrame.setVisible( true );


}
}

如果您希望我提供更多信息,请询问。

最佳答案

不要在面板上使用空布局(并使用 setBounds())。

面板没有首选尺寸,因此 Swing 认为没有什么可绘制的。

关于java - 如何将图片添加到 JTabbedPane - 在空面板布局上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15838912/

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