首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用jbutton打开新窗口时遇到问题

使用jbutton打开新窗口时遇到问题
EN

Stack Overflow用户
提问于 2013-12-10 22:19:35
回答 1查看 134关注 0票数 0

我修改了我的代码,但仍然面临着问题,我想通过点击按钮在MyPanel2里面打开到MyPanel,我该怎么做,这是我给出的代码,这是在点击Myplanel按钮后弹出的。

代码语言:javascript
复制
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);

    }
}
EN

回答 1

Stack Overflow用户

发布于 2013-12-10 23:16:58

代码语言:javascript
复制
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);
}

现在在按钮事件处理程序中

代码语言:javascript
复制
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();
        }
    });
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20496898

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档