2016-11-16 22 views
0

メインJFrameの子としてJInternalFrame(または動作するもの)を開きます。 TopButtonsクラス内にイベントリスナーがあり、すべてのボタンが機能し、そこからJInternalFrameを開いています。 これは私のコードです。JFrameにJInternalFrameを追加する方法

public class MainWindow extends JFrame { 

private static final long serialVersionUID = 1L; 


//Constructor for the main window 
public MainWindow(){ 

    this.setLayout(new BorderLayout(5,5)); 

    //Loads the buttons for the top of the window. 
    TopButtons topPanel = new TopButtons(); 
    this.add(topPanel,BorderLayout.NORTH); 


}//End of the Constructor 




//Calls the constructor of the main class 
public static void main(String[] args) { 
    MainWindow window = new MainWindow(); 

    window.setSize(new Dimension(1000,1000)); 
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    window.setVisible(true); 
} 

}

これは、トップボタンクラスです。

public class TopButtons extends JPanel { 

private JPanel panelLeft; 
private JPanel panelRight; 


private static final long serialVersionUID = 1L; 

/** 
* Constructor creates the top panel. 
* @param frame 
*/ 
public TopButtons(){ 
    createTopPanel(); 

} 

//Creates the panel for the top of the window 
    private void createTopPanel() { 
     //Sets a grid layout for the top panel 
     setLayout(new GridLayout()); 

     //Sets and right panels to be put inside the classes panel. 
     panelLeft = new JPanel(); 
     panelRight = new JPanel(); 

     //Sets the layouts for the left and right panels. 
     panelLeft.setLayout(new FlowLayout(FlowLayout.LEFT,10,10)); 
     panelRight.setLayout(new FlowLayout(FlowLayout.RIGHT,10,10)); 

     //Adds buttons to the left panel 
     panelLeft.add(addButton("New Sale", "Icons/newSale.png")); 
     panelLeft.add(addButton("Orders", "Icons/orders.png")); 
     panelLeft.add(addButton("Products Search", "Icons/products.png")); 
     panelLeft.add(addButton("Edit Products", "Icons/editProducts.png")); 
     panelLeft.add(addButton("Customer Search", "Icons/customers.png")); 
     panelLeft.add(addButton("Stock Levels", "Icons/stock.png")); 

     //Adds buttons to the right panel 
     panelRight.add(addButton("Logout", "Icons/logout.png")); 

     //Adds the two panels to the grid layout 
     add(panelLeft); 
     add(panelRight); 

    }//End of top panel 


/*Creates a button and adds an image icon. 
* 
*/ 
private JButton addButton(String name, String imgPath){ 
    Image img = null; 
    Image icon = null;  
    JButton button = new JButton(); 

    button.setActionCommand(name); 
    button.setToolTipText(name);   

    //Gets the image from the image class. 
    try { 
     img = Images.getImage(imgPath); 
    } catch (NullPointerException e) { 
     e.printStackTrace(); 
    } catch (IOException e){ 
     e.printStackTrace(); 
    } 

    //Scales the image to the size that is best for appearance. 
    icon = img.getScaledInstance(40, 40, java.awt.Image.SCALE_SMOOTH) ; 

    //Adds icon to button and removes the buttons margins. 
    button.setIcon(new ImageIcon(icon)); 
    button.setMargin(new Insets(0,0,0,0)); 

    //Listener for the button. 
    button.addActionListener(new Listener()); 


    return button; 
} 


/** 
* Private listener class to handle events for the panels. 
* @author Carl 
* 
*/ 
private class Listener implements ActionListener{ 

    @Override 
    public void actionPerformed(ActionEvent e) { 

     //Checks the action command to see which button is pressed. 

     if(((JButton)e.getSource()).getActionCommand().equals("New Sale")){ 
      //TODO Program button. 
      System.out.println(((JButton) e.getSource()).getActionCommand()); 
     } 
     if(((JButton)e.getSource()).getActionCommand().equals("Customer Search")){ 
      //TODO Program button. 
      System.out.println(((JButton) e.getSource()).getActionCommand()); 
     } 
     if(((JButton)e.getSource()).getActionCommand().equals("Products Search")){ 
      //TODO Program button. 
      System.out.println(((JButton) e.getSource()).getActionCommand()); 
     } 
     if(((JButton)e.getSource()).getActionCommand().equals("Orders")){ 
      //TODO Program button. 
      System.out.println(((JButton) e.getSource()).getActionCommand()); 
     } 
     if(((JButton)e.getSource()).getActionCommand().equals("Stock Levels")){ 
      //TODO Program button. 
      System.out.println(((JButton) e.getSource()).getActionCommand()); 
     } 
     if(((JButton)e.getSource()).getActionCommand().equals("Logout")){ 
      //TODO Program button. 
      System.out.println(((JButton) e.getSource()).getActionCommand()); 
     } 
     if(((JButton)e.getSource()).getActionCommand().equals("Edit Products")){ 
      //TODO Program button. 
      System.out.println(((JButton) e.getSource()).getActionCommand()); 
     } 
    } 



}//End of private class. 

クラス

の} //終わりと子JInternalFrameのようにそれを追加する方法を知らないので、これは何のコードEditProductsではありません。

public class EditProducts extends JInternalFrame { 

/** 
* 
*/ 
private static final long serialVersionUID = 1L; 


public EditProducts(){ 

    this.add(new JLabel("hello")); 
} 

}

What I want the app to look like

+1

「JDesktopPane」が必要です(https://docs.oracle.com/javase/tutorial/uiswing/components/internalframe.htmlを参照)。 – Berger

答えて

1

こんにちはカールLeatherbarrow!

私は本当に何を意味するのか分かりませんが、ボタンをクリックしてJInternalFrameを作成する場合は、JDesktopPaneを作成する必要があります。次のように公式のJavaドキュメントで

が説明されている: 例コード:

...//In the constructor of InternalFrameDemo, a JFrame subclass: 
    desktop = new JDesktopPane(); 
    createFrame(); //Create first window 
    setContentPane(desktop); 
    ... 
    //Make dragging a little faster but perhaps uglier. 
    desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE); 
... 
protected void createFrame() { 
    MyInternalFrame frame = new MyInternalFrame(); 
    frame.setVisible(true); 
    desktop.add(frame); 
    try { 
     frame.setSelected(true); 
    } catch (java.beans.PropertyVetoException e) {} 
} 

...//In the constructor of MyInternalFrame, a JInternalFrame subclass: 
static int openFrameCount = 0; 
static final int xOffset = 30, yOffset = 30; 

public MyInternalFrame() { 
    super("Document #" + (++openFrameCount), 
      true, //resizable 
      true, //closable 
      true, //maximizable 
      true);//iconifiable 
    //...Create the GUI and put it in the window... 
    //...Then set the window size or call pack... 
    ... 
    //Set the window's location. 
    setLocation(xOffset*openFrameCount, yOffset*openFrameCount); 
} 

だからあなたのコードの中で、あなたがこのようにそれを実装することができます

private class Listener implements ActionListener{ 

    @Override 
    public void actionPerformed(ActionEvent e) { 

     //Checks the action command to see which button is pressed. 

     if(((JButton)e.getSource()).getActionCommand().equals("New Sale")){ 
      //TODO Program button. 
      System.out.println(((JButton) e.getSource()).getActionCommand()); 
     //Open the JInternalFrame 
     MyInternalFrame frame = new MyInternalFrame(); 
      frame.setVisible(true); 
      desktop.add(frame); 
      try { 
      frame.setSelected(true); 
      } catch (java.beans.PropertyVetoException e) {} 
     } 
     if(((JButton)e.getSource()).getActionCommand().equals("Customer Search")){ 
      //TODO Program button. 
      System.out.println(((JButton) e.getSource()).getActionCommand()); 
     } 
     if(((JButton)e.getSource()).getActionCommand().equals("Products Search")){ 
      //TODO Program button. 
      System.out.println(((JButton) e.getSource()).getActionCommand()); 
     } 
     if(((JButton)e.getSource()).getActionCommand().equals("Orders")){ 
      //TODO Program button. 
      System.out.println(((JButton) e.getSource()).getActionCommand()); 
     } 
     if(((JButton)e.getSource()).getActionCommand().equals("Stock Levels")){ 
      //TODO Program button. 
      System.out.println(((JButton) e.getSource()).getActionCommand()); 
     } 
     if(((JButton)e.getSource()).getActionCommand().equals("Logout")){ 
      //TODO Program button. 
      System.out.println(((JButton) e.getSource()).getActionCommand()); 
     } 
     if(((JButton)e.getSource()).getActionCommand().equals("Edit Products")){ 
      //TODO Program button. 
      System.out.println(((JButton) e.getSource()).getActionCommand()); 
     } 
    } 

about it you can read hereより。

関連する問題