gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 06:26:20 24 4
gpt4 key购买 nike

我修改了我的代码,但仍然遇到问题,我想通过单击按钮将 MyPanel2 打开到 MyPanel,我该怎么做,这是我给出的代码,单击 Myplanel 按钮后弹出打开..

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);

}
}

最佳答案

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();
}
});

关于java - 使用 jbutton 打开新窗口时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20496898/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com