JLabel正在消失

时间:2015-06-13 00:58:49

标签: java swing jbutton jlabel

我一直在努力寻找答案,我无法找到正确的解决方案,所以我希望你们能够帮助我。 我只是通过覆盖

创建了一个自定义的JButton
  

paintComponent(Graphics g)

使用drawImage。

然后我创建了JLabel,我想用它来编写JButton之上,因为drawImage似乎是在Label上写的,所以我选择了JLabel。 所以我得到的问题是JLabel实际上并没有出现在JButton上。 我正在使用Eclipse,当我按下Run JLabel时,只需弹出按钮并立即消失。

以下是代码:

private boolean initialize() {
    Color c = new Color(78,110,248);
    Color c1 = new Color(178,110,248);
    frame = new JFrame();
    frame.setBounds(100, 100, 800,600);
    frame.setBackground(c1);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    /*Label that I have trouble with. The text should stay on the JButton*/
    lblNewLabel_1 = new JLabel("New label");
    lblNewLabel_1.setEnabled(false);
    lblNewLabel_1.setOpaque(true);
    lblNewLabel_1.setBounds(579, 252, 46, 14);
    frame.getContentPane().add(lblNewLabel_1);


    /*Custom buttons. Class "Button" just overrides paintComponent*/
    btnNewButton = new Button(/*PATH TO JPG*/);
    btnNewButton.setBackground(Color.WHITE);
    btnNewButton.setBounds(427, 213, 357, 90);
    frame.getContentPane().add(btnNewButton);

我试图做lblNewLabel_1.setOpaque(true),但这只是让JLabel保持稍长一些,所以现在我可以实际看到它,但它仍然消失了。 有谁知道如何解决它?

编辑: 我更新了代码,只留下了相关的部分。但是,这是整个代码:

public class Moje {

private JFrame frame;
private Button btnNewButton;
private Label label_1;
private JLabel lblNewLabel;
private JLabel lblNewLabel_1;

/**
 * Launch the application.
 */
public static void main(String[] args) {

    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Moje window = new Moje();
                window.frame.setVisible(true);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public Moje() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private boolean initialize() {
    Color c = new Color(78,110,248);
    Color c1 = new Color(178,110,248);
    frame = new JFrame();
    frame.setBounds(100, 100, 800,600);
    frame.setBackground(c1);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    // Label with customized background. It just overrides paintComponent
    myLabel lblNewLabel = new myLabel("PATH TO JPG");
    lblNewLabel.setBounds(427, 0, 357, 169);
    frame.getContentPane().add(lblNewLabel);

    // RELEVANT LABEL. SOURCE OF THE PROBLEM.
    lblNewLabel_1 = new JLabel("New label");
    lblNewLabel_1.setEnabled(false);
    lblNewLabel_1.setOpaque(true);
    lblNewLabel_1.setBounds(579, 252, 46, 14);
    frame.getContentPane().add(lblNewLabel_1);



    // JButton with customized background. It just overrides paintComponent.
    // THIS IS THE BUTTON, WHERE I NEED MY LABEL TO BE VISIBLE
    btnNewButton = new Button("PATH TO JPG");
    btnNewButton.setBackground(Color.WHITE);
    btnNewButton.setBounds(427, 213, 357, 90);
    frame.getContentPane().add(btnNewButton);

    // JTextPane with customized background. It just overrides paintComponent
    myPanel textPane = new myPanel("PATH TO JPG");
    textPane.setEnabled(false);
    textPane.setEditable(false);
    textPane.setBounds(0, 0, 427, 561);
    frame.getContentPane().add(textPane);

    // Jbutton with customized background. It just overrides paintComponent
    Button button = new Button("C:/Users/Filip/Documents/Programowanie/Java/moj/src/moj/Blue.jpg");
    button.setBackground(Color.WHITE);
    button.setBounds(427, 336, 357, 90);
    frame.getContentPane().add(button);

    // JBUtton with customized background. It just overrides paintComponent
    Button button_1 = new Button("C:/Users/Filip/Documents/Programowanie/Java/moj/src/moj/Blue.jpg");
    button_1.setBackground(Color.WHITE);
    button_1.setBounds(427, 448, 357, 90);
    frame.getContentPane().add(button_1);







}

}

1 个答案:

答案 0 :(得分:0)

我建议您将Button btnNewButton更改为JButton,然后添加:

btnNewButton.add(lblNewLabel_1); // or lblNewLabel, 

你可以修改它。