2017-11-09 6 views
0

私はメインレイアウトにcardlayoutを使用しました。私はそのカードレイアウトに最初のパネルを追加しました。私は最初のパネルからフレーム内の画像を隠そうとしています。だから私はJavaのパネルからフレームへの通信を提供する方法

私のメインクラス、私はパネルからフレームに通信を提供するためのインタフェースのために使用してい

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

私が使用して、コードの下のようなインターフェースを使用していました

パブリッククラスメインページは、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; 
} 

が、私は今、私はcardlayoutを持っているフレームにパネルからいくつかの情報を送信したい

MainPage mainPage = new MainPage(HomePage.this); 

ライン

の下、この中で静的文脈エラーでこれを使用することはできません を取得しています。私にこれをするアイディアを提案してもらえますか?前もって感謝します。

+0

は何がしたいん静的コンテキスト外でそのロジックを移動する必要がありますか? 'HomePage.this'は' static'文脈にない場合のインスタンスになります。ここに「これ」は存在しません。インスタンスが必要な場合は、そのコードをHomePageのコンストラクタ/メソッドに入れるか、単にインスタンスHomePage hp = new HomePage()を宣言し、そのインスタンスを 'MainPage'に渡します。私にとっては、これは設計上の問題です。静的メソッドで多くのGUI初期化を行うことはありません。 – AxelH

+0

メソッド 'createAndShowGUI'を静的でないものにします。パネルからフレームに情報を送るには、フレームでメソッドを作成し、パネルから呼び出す。 –

+0

@JaySmith応答をありがとう。 public static void centeredFrame(javax.swing.JFrame objFrame){}メソッド – Olive

答えて

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); 
} 
関連する問題