【问题标题】:How to provide communicate from panel to frame in java如何在java中提供从面板到框架的通信
【发布时间】:2017-11-09 09:06:00
【问题描述】:

我在主课中使用了卡片布局。我在该卡片布局中添加了第一个面板。我正在尝试从第一个面板中隐藏框架中的图像。所以我使用了如下代码的界面,

我的主要课程,

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

    }
}

我正在使用 for 接口从面板提供与框架的通信,

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

我在卡片布局中使用下面的面板,

公共类 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;
}

但我在下面一行中遇到 Cannot use this in a static context 错误

MainPage mainPage = new MainPage(HomePage.this);

现在我想将一些信息从面板发送到具有卡片布局的框架。你能建议我这样做吗?提前致谢。

【问题讨论】:

  • 你想做什么?如果HomePage.this 不在static 上下文中,则HomePage.this 将是实例。在这里,this 不存在。如果你需要一个实例,要么将该代码放在 HomePage 的构造函数/方法中,要么简单地声明一个实例 HomePage hp = new HomePage() 并将该实例传递给 MainPage。对我来说,这是一个设计问题,我从来没有把那么多的 GUI 初始化放在一个静态方法中。
  • 使方法createAndShowGUI 不是静态的。将一些信息从面板发送到框架,在框架中创建方法并从面板调用它
  • @JaySmith 感谢您的回复。我无法更改 createAndShowGUI 非静态,因为我在 public static void centeredFrame(javax.swing.JFrame objFrame) {} 方法中使用它
  • 方法createAndShowGUI未在方法centeredFrame中使用

标签: java swing interface jpanel cardlayout


【解决方案1】:

您不能在静态上下文中使用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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-26
    • 2016-05-08
    • 2017-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-07
    • 2014-08-10
    相关资源
    最近更新 更多