3

这是有问题的 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 正在绘制到另一个类的主框架上。正在发生的事情是“背景”被画得很好,但按钮却没有。一旦我用鼠标悬停在按钮上,按钮就会出现,但在那之前它们是不可见的。我已经搜索了几个小时并没有找到解决方案,因此我们将不胜感激。谢谢!

4

3 回答 3

5

我不得不猜测它发生在您的 paintComponent 方法中。您正在调用 super.paintComponent() 将像往常一样绘制所有内容,包括按钮。然后你用 g2d.drawImage() 绘制它。你应该先画背景。

于 2012-11-16T21:45:30.810 回答
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();
  }
}
于 2012-11-16T23:10:12.450 回答
2

我想你也有一个 JFrame 来放置这个面板。在将要包含在其中的所有内容都已初始化之后,尝试最后调用框架。

去年我遇到了同样的问题,这为我解决了这个问题。

于 2012-11-16T21:43:03.957 回答