微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 JFrame 上添加图像?

如何解决如何在 JFrame 上添加图像?

我正在尝试创建一个带有文本和图像的 JFrame 窗口。但是,我不断收到空指针异常。这是我的代码,有人可以帮忙吗?没有什么很复杂的,这只是给我一个例子。谢谢!

import pyspark.sql.functions as F

df2 = df.groupBy('id','tran_date').agg(
    F.coalesce(
        F.max(F.when(F.col('effc_date') < F.col('tran_date'),F.col('effc_date'))),F.min(F.when(F.col('effc_date') >= F.col('tran_date'),F.col('effc_date')))
    ).alias('effc_date')
)

df2.show()
+---+----------+----------+
| id| tran_date| effc_date|
+---+----------+----------+
| 12|2020-02-01|2019-02-01|
| 34|2020-02-01|2020-02-15|
| 40|2020-02-01|2019-02-15|
+---+----------+----------+

我收到的错误如下:

import java.awt.FlowLayout; // specifies how comments are arranged
import javax.swing.JFrame; // provides basic window features
import javax.swing.JLabel; // displays text and images
import javax.swing.SwingConstants; // common constants used with Swing
import javax.swing.Icon; // interface used to manipulate images
import javax.swing.ImageIcon; // loads images

class LabelFrame extends JFrame{

private final JLabel label1; // JLabel with just text
private final JLabel label2; // JLabel constructed with text and icon
private final JLabel label3; // JLabel with added text and icon

// LabelFrame constructor adds JLabels to JFrame
LabelFrame() {
    
    super("Testing label");
    setLayout(new FlowLayout()); // set frame layout
    
    // JLabel constructor with a string argument
    label1 = new JLabel("Label with text");
    label1.setToolTipText("This is label1");
    add(label1); // add label1 to JFrame
    
    // JLabel constructor with string,Icon and alignment arguments
    Icon bug = new ImageIcon(getClass().getResource("bug1.png"));
    label2 = new JLabel("Label with text and icon",bug,SwingConstants.LEFT);
    label2.setToolTipText("This is label2");
    add(label2); // add labe2 to JFrame
    
    label3 = new JLabel(); // JLabel constructor with no arguments
    label3.setText("Label with icon and text at botton");
    label3.setHorizontalTextPosition(SwingConstants.CENTER);
    label3.setVerticalTextPosition(SwingConstants.BottOM);
    label3.setToolTipText("This is label3");
    add(label3); // add label3 to JFrame
    
}
}
public class LabelTest {

public static void main(String[] args) {
    
    LabelFrame labelFrame = new LabelFrame();
    labelFrame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
    labelFrame.setSize(260,180);
    labelFrame.setVisible(true);

}

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。