JLabel 正在消失
JLabel is disappearing
我一直在到处寻找答案,但我找不到合适的解决方案,所以我希望你们能够帮助我。
我只是通过覆盖
创建了一个自定义 JButton
paintComponent(Graphics g)
使用 drawImage。
然后我创建了 JLabel,我想用它在 JButton 上写,因为 drawImage 似乎是在 Label 上写,所以我选择了 JLabel。
所以我遇到的问题是 JLabel 实际上没有出现在 JButton 上。
我正在使用 Eclipse,当我按下 运行 时,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);
}
}
我建议您将 Button btnNewButton
更改为 JButton
,然后添加:
btnNewButton.add(lblNewLabel_1); // or lblNewLabel,
你可以修改它。
我一直在到处寻找答案,但我找不到合适的解决方案,所以我希望你们能够帮助我。 我只是通过覆盖
创建了一个自定义 JButtonpaintComponent(Graphics g)
使用 drawImage。
然后我创建了 JLabel,我想用它在 JButton 上写,因为 drawImage 似乎是在 Label 上写,所以我选择了 JLabel。 所以我遇到的问题是 JLabel 实际上没有出现在 JButton 上。 我正在使用 Eclipse,当我按下 运行 时,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);
}
}
我建议您将 Button btnNewButton
更改为 JButton
,然后添加:
btnNewButton.add(lblNewLabel_1); // or lblNewLabel,
你可以修改它。