1

我到处搜索,有大量的文档,但都是令人困惑的,一半的测试代码不起作用,所以我问。制作 jlabel 的最简单方法是什么,设置它的位置(使用整数或维度),并将其添加到 JFrame

package com.notelek.notify;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Gui {



    public static void main(String[] args){

    }

    public static void notify(String line1, String line2, String imagepath, int style){
        GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
        int width = gd.getDisplayMode().getWidth();
        int swidth = width - 320;

        JFrame notification = new JFrame();
        notification.setSize(new Dimension(320,64));
        notification.setLocation(swidth, 0);
        notification.setUndecorated(true);
        notification.setVisible(true);

        JPanel main = new JPanel();

        JLabel notifyline1 = new JLabel();
        notifyline1.setText("test");
        notifyline1.setLocation(0, 0);
        main.add(notification);
    }

}
4

2 回答 2

3

您需要将您JLabel的添加到可见容器中,否则它无法显示在屏幕上。

我还猜想您实际上打算将您的添加JPanel到您的JFrame而不是反之,例如:

...
main.add(notifyline1);
...    
notification.add(main);
...
于 2013-01-13T02:13:39.207 回答
3

我认为您的意思是notification.add(main);顺序很重要:

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

public class Gui {

    public static void main(String[] args){
        notify("", "", "", 0);
    }

    public static void notify(String line1, String line2, String imagepath, int style){
        JFrame notification = new JFrame();
        JPanel main = new JPanel();
        JLabel notifyline1 = new JLabel();
        notifyline1.setText("test");
        main.add(notifyline1);
        notification.add(main);
        notification.setSize(new Dimension(320,64));
        notification.setLocationRelativeTo(null);
        notification.setUndecorated(true);
        notification.setVisible(true);
    }
}
于 2013-01-13T02:18:07.170 回答