Để tạo ứng dụng MDI trong Java, thực hiện các bước sau:
- Tạo JFrame cha, đặt JDesktopPane vào JFrame cha
- Các Frame con là các lớp kế thừa JInternalFrame, hiển thị Frame con trong Frame cha như sau:
GDBook f = new GDBook();
package desktoppane;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JDesktopPane;
public class DesktopPane extends JDesktopPane
{
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Image img = Toolkit.getDefaultToolkit().getImage(
"background_image/Presentation1.jpg");
if(img != null)
g.drawImage(img, 0,0,this.getWidth(),this.getHeight(),
this);
//else g.drawString("Image not found", 50,50);
}
}
|
|
Lớp Frame cha như sau:
package librarymanagement;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.KeyStroke;
public class BookMenu extends javax.swing.JFrame {
public BookMenu() {
super("Library Management");
initComponents();
//Thiết lập kích thước cửa sổ toàn màn hình
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setSize(screenSize);
this.mexit.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_E, ActionEvent.ALT_MASK));
this.mupdatebook.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_B, ActionEvent.ALT_MASK));
this.jPanel1.add(this.jToolBar1, BorderLayout.NORTH);
this.jPanel1.setSize(this.getWidth(),30);
}
private void initComponents() {
jPopupMenu1 = new javax.swing.JPopupMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jMenuItem2 = new javax.swing.JMenuItem();
desktopPane1 = new desktoppane.DesktopPane();
jPanel1 = new javax.swing.JPanel();
jToolBar1 = new javax.swing.JToolBar();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jMenuBar1 = new javax.swing.JMenuBar();
msystem = new javax.swing.JMenu();
mexit = new javax.swing.JMenuItem();
mupdate = new javax.swing.JMenu();
mupdatebook = new javax.swing.JMenuItem();
jMenuItem1.setText("Update Book");
jMenuItem1.setToolTipText("");
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem1ActionPerformed(evt);
}
});
jPopupMenu1.add(jMenuItem1);
jMenuItem2.setText("Exit");
jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem2ActionPerformed(evt);
}
});
jPopupMenu1.add(jMenuItem2);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseReleased(java.awt.event.MouseEvent evt) {
formMouseReleased(evt);
}
});
jToolBar1.setRollover(true);
jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/edit.gif")));
jButton1.setToolTipText("Update Book");
jButton1.setAutoscrolls(true);
jButton1.setFocusable(false);
jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jToolBar1.add(jButton1);
jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/Exit.png")));
jButton2.setToolTipText("Exit");
jButton2.setFocusable(false);
jButton2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
jButton2.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jToolBar1.add(jButton2);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 444, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE)
);
desktopPane1.add(jPanel1);
jPanel1.setBounds(0, 0, 400, 30);
msystem.setMnemonic('S');
msystem.setText("System");
mexit.setMnemonic('E');
mexit.setText("Exit");
mexit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mexitActionPerformed(evt);
}
});
msystem.add(mexit);
jMenuBar1.add(msystem);
mupdate.setMnemonic('U');
mupdate.setText("Update");
mupdatebook.setMnemonic('B');
mupdatebook.setText("Update Book");
mupdatebook.setToolTipText("");
mupdatebook.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mupdatebookActionPerformed(evt);
}
});
mupdate.add(mupdatebook);
jMenuBar1.add(mupdate);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(desktopPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 399, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(desktopPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 373, Short.MAX_VALUE)
);
pack();
}
private void mexitActionPerformed(java.awt.event.ActionEvent evt) {
this.dispose();
//System.exit(0);
}
private void mupdatebookActionPerformed(java.awt.event.ActionEvent evt) {
GDBook f = new GDBook();
f.setVisible(true);
this.desktopPane1.add(f);
}
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
GDBook f = new GDBook();
f.setVisible(true);
this.desktopPane1.add(f);
}
private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {
this.dispose();
}
private void formMouseReleased(java.awt.event.MouseEvent evt) {
if (evt.getButton()==3)
this.jPopupMenu1.show(this, evt.getX(), evt.getY());
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
this.dispose();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
GDBook f = new GDBook();
f.setVisible(true);
this.desktopPane1.add(f);
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(BookMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(BookMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(BookMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(BookMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new BookMenu().setVisible(true);
}
});
}
private desktoppane.DesktopPane desktopPane1;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JPanel jPanel1;
private javax.swing.JPopupMenu jPopupMenu1;
private javax.swing.JToolBar jToolBar1;
private javax.swing.JMenuItem mexit;
private javax.swing.JMenu msystem;
private javax.swing.JMenu mupdate;
private javax.swing.JMenuItem mupdatebook;
}
|
» Tin mới nhất:
» Các tin khác: