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 に答える