2013-12-10 67 views
0

我修改了我的代碼,但仍然面臨問題,我想MyPanel2裏面打開MyPanel通過單擊按鈕,我該怎麼做,這裏是我的代碼給出這是彈出後打開點擊Myplanel的按鈕..使用jbutton打開一個新窗口有麻煩

import java.awt.CardLayout; 
import java.awt.Dimension; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.BorderFactory; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 
import javax.swing.UIManager; 

import org.jpedal.PdfDecoder; 
import org.jpedal.examples.viewer.Viewer; 
import org.jpedal.gui.GUIFactory; 
import org.jpedal.utils.LogWriter; 

public class Button extends Viewer { 

    private MyPanel panel1; 
    private MyPanel2 panel2; 

    private void displayGUI() { 
     JFrame frame = new JFrame("eBookReader Button"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     JPanel contentPane = new JPanel(); 
     contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 
     contentPane.setLayout(new CardLayout()); 
     panel1 = new MyPanel(contentPane); 
     contentPane.add(panel1, "Panel 1"); 
     frame.setContentPane(contentPane); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String args[]) { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       new Button().displayGUI(); 
      } 
     }); 
    } 
} 

class MyPanel extends JPanel { 

    private JButton jcomp4; 
    private JPanel contentPane; 

    public MyPanel(JPanel panel) { 

     contentPane = panel; 

     jcomp4 = new JButton("openNewWindow"); 

     // adjust size and set layout 
     setPreferredSize(new Dimension(315, 85)); 
     setLayout(null); 
     jcomp4.setLocation(0, 0); 
     jcomp4.setSize(315, 25); 

     jcomp4.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       try { 
        UIManager.setLookAndFeel(UIManager 
          .getSystemLookAndFeelClassName()); 
       } catch (Exception e1) { 
        LogWriter.writeLog("Exception " + e1 
          + " setting look and feel"); 
       } 
       MyPanel2 a = new MyPanel2(); 
       a.setupViewer(); 

      } 
     }); 

     add(jcomp4); 
    } 
} 

class MyPanel2 extends Viewer { 
    public MyPanel2() { 

     // tell user we are in multipanel display 
     currentGUI.setDisplayMode(GUIFactory.MULTIPAGE); 

     // enable error messages which are OFF by default 
     PdfDecoder.showErrorMessages = true; 

    } 

    public MyPanel2(int modeOfOperation) { 

     // tell user we are in multipanel display 
     currentGUI.setDisplayMode(GUIFactory.MULTIPAGE); 

     // enable error messages which are OFF by default 
     PdfDecoder.showErrorMessages = true; 

     commonValues.setModeOfOperation(modeOfOperation); 

    } 
} 
+0

哪裏是當前GUI來的? – robbmj

+0

在你的'jcomp4'按鈕動作中你改變了'CardLayout'的卡,它不會創建一個新窗口,你有什麼問題呢? – alex2410

+0

你是否希望它只是與按鈕的小對話框,一旦點擊你想顯示更大的MyPanel2? – robbmj

回答

2
private void displayGUI() { 
    JFrame frame = new JFrame("eBookReader Button"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    JPanel contentPane = new JPanel(); 
    contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 
    contentPane.setLayout(new CardLayout()); 
    panel1 = new MyPanel(contentPane); 
    contentPane.add(panel1, "Panel 1"); 
    frame.setContentPane(contentPane); 
      // we need to increase the size of the panel so when we switch views we can see the viewer 
    frame.setPreferredSize(new Dimension(2000, 700)); 
    frame.pack(); 
    frame.setLocationByPlatform(true); 
    frame.setVisible(true); 
} 

現在在按鈕的事件處理程序

jcomp4.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      try { 
       UIManager.setLookAndFeel(UIManager 
         .getSystemLookAndFeelClassName()); 
      } catch (Exception e1) { 
       LogWriter.writeLog("Exception " + e1 
         + " setting look and feel"); 
      } 
      MyPanel2 a = new MyPanel2(); 
          // inform the viewer of where it is to be displayed 
      a.setRootContainer(contentPane); 
          // hide the curently visible panel 
      MyPanel.this.setVisible(false); 
          // show the viewer 
      a.setupViewer(); 
     } 
    }); 
+0

Thanx robbmj ..它有幫助,我修改了我的代碼,但我想MyPanel2裏面到Mypanel ..你能幫我嗎? – Bond007

+0

當然,我會給它一個去 – robbmj

+0

我已經編輯了我的代碼在這裏...請幫我那 – Bond007