我想做的是打开一个JInternalFrame(或将起作用的东西)作为主JFrame的子级。我在TopButtons类中具有事件侦听器,所有按钮均可工作,我想从那里打开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.
} //课程结束
这是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
最佳答案
你好卡尔·莱瑟鲍罗!
我真的不知道您的意思是什么,但是如果您想通过单击按钮创建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。
关于java - 如何将JInternalFrame添加到JFrame,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40637490/