2016-11-16 59 views
0

我想要做的是打开一个JInternalFrame(或将工作的东西)作为主JFrame的子元素。我在TopButtons类中有事件监听器,所有的按钮都可以工作,而我想要做的就是从那里打开一个JInternalFrame。 这是我的代码。如何将JInternalFrame添加到JFrame中

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. 

} //类的结束

这是EditProducts没有代码,不知道如何将其添加为一个孩子的JInternalFrame。

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