あるクラスの Jpanel を別のクラスの Jpanel の上に表示しようとしています。回りくどい方法でこれを行うことはできますが、長期的には後悔することはわかっています. フレーム クラスにはボタン付きのメイン Jpanel が含まれており、その上に別の Jpanel をボタン付きで一時的に配置したいと考えています。コードを単純化しようとしました。コードの最初のチャンクの下にある ShopButton メソッドと Shop クラスに注意してください。それが私が焦点を当てようとしているものです。Shop JPanel を既存の「フレーム」(これをクラスと呼んでいます) JPanel の上に配置しようとしています。
package sonomaroller;
//Bunch of imports i didnt include
import javax.swing.text.StyleConstants;
public class frame extends JPanel implements Runnable {
//buttons
private JButton Button4;
//images
private Image Sonoma;
private Image pic;
//booleans
private boolean loaded;
public boolean inTown = true;
public boolean isShopping = false;
//variables
//strings
//arrays
public Integer[] attributes = {0,1,2}; //attk - 0, def - 1 , magic - 2
//new jpanel
JTextPane field = new JTextPane();
JScrollPane sp = new JScrollPane(field);
//calling another class making it public
public Shop shopping = new Shop();
public void run(){
}
public frame(){
loadpics();
attackButton();
magicButton();
travelButton();
shopButton();
textField();
}
public void paint(Graphics g){
super.paint(g);
g.drawString(firstName +givenName+ secondName,225,40);
g.drawRect(100, 50, 350, 275);
if(loaded){
g.drawImage(Sonoma,101,51,350,275,null);
}
if(isShopping==true){
shopping.paint(g);
}
}
public void shopButton(){
//for shop button
Button4= new JButton("Shop");
Button4.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent Event) {
loaded=false;
isShopping=true;
repaint();
}
});
Button4.setBounds(130, 485, 90, 30);
add(Button4);
}
public void loadpics(){
Sonoma = new ImageIcon("C:\\Users\\Camtronius\\Documents\\NetBeansProjects\\SonomaRoller\\src\\sonomaroller\\Sonoma.png").getImage();
System.out.println("i was called?");
loaded = true;
repaint();
}
}
__ _ __ _ _ __ _ _ __ _ _ __ _ __ _ _ __ _ ___ _ショップクラス_ _
public class Shop extends JPanel {
public JButton CloseButton;
public Shop(){
CloseButton();
setVisible(true);
setLayout(null);
}
public void paint(Graphics g){
System.out.println("shop is working");
g.setColor(Color.WHITE);
g.fillRect(100, 50, 350, 275);
g.drawRect(100, 50, 350, 275);
}
public void CloseButton(){
//for
CloseButton= new JButton("Exit Shop");
CloseButton.addActionListener( new ActionListener() {
@Override
public void actionPerformed(ActionEvent Event) {
System.out.println("am i alive?");
}
});
CloseButton.setBounds(100, 100, 90, 30);
add(CloseButton);
}
}