以下是出现此问题的JPanel类:
public class EntryDialog extends JPanel{
private static final long serialVersionUID = 1L;
MainWindow mainWindow;
Image background;
JButton continueresume;
JButton newresume;
JButton settings;
JButton exit;
public EntryDialog(MainWindow mainWindow){
continueresume = new JButton("Continue resume");
newresume = new JButton("New resume");
settings = new JButton("Settings");
exit = new JButton("Exit");
this.mainWindow = mainWindow;
this.background = Utilities.getImage("images\\entryDialogBackground.jpg", true);
this.setLayout(null);
//continueresume button
continueresume.setBounds(Utilities.getScaledWidth(1150),
Utilities.getScaledHeight(150),
Utilities.getScaledWidth(500),
Utilities.getScaledHeight(50));
//newresume button
newresume.setBounds(Utilities.getScaledWidth(1150),
Utilities.getScaledHeight(220),
Utilities.getScaledWidth(500),
Utilities.getScaledHeight(50));
//settings button
settings.setBounds(Utilities.getScaledWidth(1150),
Utilities.getScaledHeight(290),
Utilities.getScaledWidth(500),
Utilities.getScaledHeight(50));
//exit button
exit.setBounds(Utilities.getScaledWidth(1150),
Utilities.getScaledHeight(360),
Utilities.getScaledWidth(500),
Utilities.getScaledHeight(50));
this.add(continueresume);
this.add(newresume);
this.add(settings);
this.add(exit);
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
if(background != null){
g2d.drawImage(background, 0, 0, this);
}
g2d.dispose();
}
这个JPanel被绘制到另一个类的主框架中。发生的事情是“背景”被绘得很好,但按钮不是。一旦我用鼠标将鼠标悬停在按钮上,按钮就会出现,但在此之前它们是不可见的。我已经搜索了几个小时而没有找到解决方案所以任何帮助将不胜感激。谢谢!
答案 0 :(得分:5)
我不得不猜测你的paintComponent方法正在发生这种情况。你正在调用super.paintComponent()将像往常一样绘制所有内容,包括按钮。然后你用g2d.drawImage()绘制它。你应该首先绘制背景。
答案 1 :(得分:3)
/*Use this code as an example first run this and then use it for your requirements*/
package mypack;
import javax.swing.*;
import java.awt.*;
public class backgroundClass extends JPanel {
//Initializing the class Image
Image background;
//Setting up GUI
public backgroundClass(interfaceAndImage iai) {
//Constructing the class "Toolkit" which will be used to manipulate our images.
Toolkit kit = Toolkit.getDefaultToolkit();
//Getting the "background.jpg" image we have in the folder
background = kit.getImage("d:\\123.jpg");
}
//Manipulate Images with JAVA2D API. . creating a paintComponent method.
public void paintComponent(Graphics comp) {
//Constructing the class Graphics2D. Create 2D by casting the "comp" to Graphics2D
Graphics2D comp2D = (Graphics2D)comp;
//creating a graphics2d using the images in the folder and place it in a specific coordinates.
comp2D.drawImage(background, 0, 0, this);
}
}
/ 另一个班级 / 包mypack;
//Java Core Package
import javax.swing.*;
//Java Extension Package
import java.awt.*;
public class interfaceAndImage extends JFrame {
//Constructing the class we created called "backgroundClass" so we can
//use it here in our main program as a parent panel.
backgroundClass bc;
//Initializing our JComponents and the labels of our JButton and JLabel
JPanel panel1, panel2;
JLabel labels[];
JButton choices[];
JTextField inputs[];
String lebelName[] = {"Name:","Age:","Sex:","Address:","Tel. No.:"};
String buttonChoice[] = {"Register","Reset","Cancel"};
//Setting up GUI
public interfaceAndImage() {
//Setting up the Title of the Window
super("How to put a JTextField, JLabel, and JButton above image");
//Set Size of the Window (WIDTH, HEIGHT)
setSize(310,170);
//Exit Property of the Window
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Constructing the class "backgroundClass" and call the keyword "this" so it can
//be recognizable by our main program.
bc = new backgroundClass(this);
//Constructing JPanel 1 and set its layout property including the background property
//which is transparent
panel1 = new JPanel();
panel1.setLayout(new GridLayout(5,2));
//Constructing the class Color and set its property to 0,0,0,0 means no color.
Color trans = new Color(0,0,0,0);
panel1.setBackground(trans); //Setting JPanel 1 background to transparent
//Constructing JPanel 2 and set its layout property
panel2 = new JPanel();
panel2.setLayout(new GridLayout());
//Constructing our JComponents setting its specific array size
labels = new JLabel[5];
inputs = new JTextField[5];
choices = new JButton[3];
//Adding our JPanel 1 and 2 to our class "backgroundClass" which is our parent panel
bc.add(panel1, BorderLayout.NORTH);
bc.add(panel2, BorderLayout.SOUTH);
//Setting up the container ready for the components to be added.
Container pane = getContentPane();
setContentPane(pane);
//Constructing JLabel and JTextField using "for loop" in their desired order
for(int count=0; count<inputs.length && count<labels.length; count++) {
labels[count] = new JLabel(lebelName[count], JLabel.RIGHT);
inputs[count] = new JTextField(30);
//Adding the JLabel and the JTextFied in JPanel 1
panel1.add(labels[count]);
panel1.add(inputs[count]);
}
//Constructing all 4 JButtons using "for loop" and add them in the panel 1
for(int count=0; count<choices.length; count++) {
choices[count] = new JButton(buttonChoice[count]);
panel2.add(choices[count]);
}
//Adding the class "backgroundClass" to our container as parent panel
pane.add(bc);
/**Set all the Components Visible.
* If it is set to "false", the components in the container will not be visible.
*/
setVisible(true);
//Disable window size
setResizable(false);
}
//Main Method
public static void main (String[] args) {
interfaceAndImage iai = new interfaceAndImage();
}
}
答案 2 :(得分:2)
我想你也有一个JFrame放置这个面板。尝试在框架中的所有内容已经初始化后再次调用框架。
我去年遇到了同样的问题,这对我来说已经解决了。