一个转到新面板的按钮

时间:2015-09-07 19:03:06

标签: java

我创建了一个位于主页面的按钮,当用户点击它时,它会改变panal(主要的想法是它改变了背景和面板上的所有内容,并为其添加新内容)但是我失败!,虽然我尝试了button.setBounse(..),但我也无法调整按钮的位置 无论如何,有人可以帮助我做这两件事吗?

public class mainClass {
private static  JButton start;
static BackgroundPanel bp = null;
static JFrame mainf = null;
public static void main(String[] args) throws IOException {
     mainf = new JFrame ("سين جيم");


    // background

    BufferedImage mFrame = ImageIO.read(new File("B1.png"));
    bp = new BackgroundPanel(mFrame);
    mainf.add(bp);
    bp.setLayout(new GridBagLayout());

    // Hi string 
    JLabel hi = new JLabel ("أهلا وسهلا");
    Font fs = hi.getFont();
    hi.setFont(fs.deriveFont(50f));
    bp.add(hi);

    // button 
    JPanel another =  new JPanel();
    start = new JButton ( " لنبدأ");
    bp.add(start);
    start.addActionListener(new ActionListener() { 
          public void actionPerformed(ActionEvent e) { 
              bp.removeAll();
              BufferedImage mFrame2= null;
            try {
                mFrame2 = ImageIO.read(new File("B2.png"));
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
              bp = new BackgroundPanel(mFrame2);
              bp.setLayout(new GridBagLayout());

              JLabel hi1= new JLabel ("worked");
              bp.add(hi1);


          } 
        } );


    // end of frame
    mainf.pack();
    mainf.setVisible(true);
}

}

1 个答案:

答案 0 :(得分:0)

好吧,要更改主面板,您不必创建新的Frame对象并设置它的主面板。例如,您可以为bp编写一个setter,并在您的动作侦听器方法中调用此方法。如果您的面板尺寸不同,您可以轻松更改框架的尺寸......