如何在java中提供面板到帧的通信

时间:2017-11-09 09:06:01

标签: java swing interface jpanel cardlayout

我在我的班级使用了cardlayout。我在该cardlayout中添加了第一个面板。我试图从第一个面板隐藏图像。所以我使用了类似下面的代码,

我的主要课程,

public class HomePage implements OptionMenuListener{

    private static void createAndShowGUI() {

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Already there
        frame.setUndecorated(true);
        frame.setLocationRelativeTo(null);

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new CardLayout(20, 20));

        File file = new File(jsonFilePath);
        if(!file.exists()) {
            LoginPage login = new LoginPage();
            contentPane.add(login, Constants.CARD_LOGIN);
            ConfigueBranch configureBranch = new ConfigueBranch(false);
            contentPane.add(configureBranch, Constants.CARD_CONFIGURE_BRANCH);
            ConfigureSystem configureSystem = new ConfigureSystem(false);
            contentPane.add(configureSystem, Constants.CARD_CONFIGURE_SYSTEM);
            ConfigureCustomer configureCustomer = new ConfigureCustomer(false);
            contentPane.add(configureCustomer, Constants.CARD_CONFIGURE_CUSTOMER);
        }
        MainPage mainPage = new MainPage(HomePage.this);
        contentPane.add(mainPage, Constants.CARD_MAINPAGE);
//      SettingsPage configureExpinContainer = new SettingsPage();
//      contentPane.add(configureExpinContainer, Constants.CARD_SETTINGS_PAGE);

        JPanel buttonPanel = new JPanel();
        buttonPanel.setBorder(new EmptyBorder(50, 10, 10, 10));

        Image image = MyUtil.loadImage("/logo.png"); // transform it
        Image newimg = image.getScaledInstance(244, 80, java.awt.Image.SCALE_SMOOTH);
        ImageIcon icon = new ImageIcon(newimg); // transform it back
        JLabel label = new JLabel("", icon, JLabel.LEFT);
        label.setFont(new java.awt.Font("Arial", Font.BOLD, 24));
        label.setOpaque(true);
        label.setForeground(Color.BLACK);
        label.setBounds(0, 60, 300, 200);
        buttonPanel.add(label);
        frame.add(contentPane, BorderLayout.CENTER);
        frame.add(buttonPanel, BorderLayout.PAGE_START);
        frame.pack();
        frame.setSize(1000, 700);
        centeredFrame(frame);
        frame.setVisible(true);
        frame.setResizable(false);
    }

    public static void centeredFrame(javax.swing.JFrame objFrame) {
        Dimension objDimension = Toolkit.getDefaultToolkit().getScreenSize();
        int iCoordX = (objDimension.width - objFrame.getWidth()) / 2;
        int iCoordY = (objDimension.height - objFrame.getHeight()) / 2;
        objFrame.setLocation(iCoordX, iCoordY);
    }

    public static void main(String... args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

    @Override
    public void onMenuSelect(boolean isShow) {

    }
}

我正在使用界面来提供与面板框架的通信,

public interface OptionMenuListener {
    void onMenuSelect(boolean isShow);
}

我在cardlayout中使用以下面板,

公共类MainPage扩展JPanel {

JButton inputOutputFilesBtn, syncBtn, tsBtn, settingsBtn;
public MainPage(HomePage homePage){
    homePage.onMenuSelect(true);
    init();
}

public void init(){
    JTabbedPane jtbExample = new JTabbedPane();
    JPanel jplInnerPanel1 = createInnerPanel("No device connected");
    jtbExample.addTab("Input and Output Files", jplInnerPanel1);
    jtbExample.setSelectedIndex(0);
    JPanel jplInnerPanel2 = createInnerPanel("No device connected");
    jtbExample.addTab("Sync", jplInnerPanel2);
    JPanel jplInnerPanel3;
    if(configuredSystem.equalsIgnoreCase("Expeditors")) {
        jplInnerPanel3 = createInnerPanel("No device connected");
        jtbExample.addTab("TS", jplInnerPanel3);
    }
    JPanel jplInnerPanel4 = new SettingsPage();
    jtbExample.addTab("Settings", jplInnerPanel4);
    JPanel jplInnerPanel5 = new LogoutPage();
    jtbExample.addTab("Logout", jplInnerPanel5);
    this.setLayout(new BorderLayout());
    this.setPreferredSize(new Dimension(620, 400));
    this.add(jtbExample, BorderLayout.CENTER);
    add(jtbExample);

}

protected JPanel createInnerPanel(String text) {
    JPanel jplPanel = new JPanel();
    JLabel jlbDisplay = new JLabel(text);
    jlbDisplay.setHorizontalAlignment(JLabel.CENTER);
    jplPanel.setLayout(new GridLayout(1, 1));
    jplPanel.add(jlbDisplay);
    return jplPanel;
}

但是我在这下面的行无法在静态上下文中使用错误

MainPage mainPage = new MainPage(HomePage.this);

现在我想从面板向具有cardlayout的帧发送一些信息。你能建议我这样做吗?提前谢谢。

1 个答案:

答案 0 :(得分:0)

您不能在静态上下文中使用this,因为this表示正在执行方法的类实例。在这里,您处于静态环境中,因此您没有实例。

private static void createAndShowGUI() {
    MainPage mainPage = new MainPage(HomePage.this);
}

您需要声明一个变量

private static void createAndShowGUI() {
    HomePage homePage = /*initialize or get it from somewhere */
    MainPage mainPage = new MainPage(homePage);
}

或者将该逻辑移出静态上下文

private void createAndShowGUI() {
    MainPage mainPage = new MainPage(HomePage.this);
}