To Nai and Eve
Back to review some basics about windows in java. Three types of sales are more used in Java Swing as far as respect.
The JFrames ordinary. What are those sales that are created in the taskbar if you use Windows.
JDialog windows that are usually modal, in which to respond before continuing with the other windows. Examples of dialogues, those windows where we are prompted to insert the location where you want to save a file.
JInternalFrame : This is a particular class of windows. Which are embedded in a parent window, which acts as a desktop. Currently most desktop applications, follow this format. These windows can be minimized and expanded within the parent window.
In this short tutorial we will see the JInternalFrame.
Creating a JInternalFrame
The parent window will be an ordinary frame.
FramePadre public class extends JFrame implements ActionListener {
JDesktopPane desktop = new Desktop ();// Working JDesktopPane
JMenuBar bar = new JMenuBar (); / /
menu bar JMenu menu = new JMenu ("File");
itemInterna = new JMenuItem JMenuItem ("Internal Frame ");
itemSalir = new JMenuItem JMenuItem (" Exit ");
public FramePadre () {
this.setDefaultCloseOperation (JFrame.DO_NOTHING_ON_CLOSE)
... ...}}
As shown extends JFrame and implements ActionListener interface to capture events from a menu bar to enter them. In this tutorial will not develop and create a menu bar, then my policy is to address each item separately, later I realize, but if you show you the code to accomplish it.
Furthermore we can see how we declare a variable rate JDesktopPane desktop, this will allow us to assign a work surface that delimits the internalFrames.
Within the constructor of the class can call a method that we created to manage the creation of the graphical user interface, or directly put the following code.
Toolkit.getDefaultToolkit Dimension size = (). GetScreenSize ();// this.getContentPane
display (). SetLayout (null);
this.setBounds (0.0, tamaño.width, tamaño.height -30);
this.setTitle ("Sample Desktop");
this.itemInterna.addActionListener (this) / / to capture the event in item
this.itemSalir.addActionListener (this) / / idem
previous menu . add (this.itemInterna) / / the menu item we add
menu.add (this.itemSalir) / / idem
this.barra.add previous (menu) / / to add the menu bar will
this.setJMenuBar (bar) / / the frame we add the bar.
escritorio.setBackground (new Color (251,255,235 ));// a p colorcito
this.setContentPane desktop (desktop) / / add the desktop to the frame
Once done with this, we only create the method remains capture that the user click on each item of the menu. Thus we declare the method actionPerformed, imposed by the ActionListener interface, and indicated that to make each user events.
public void actionPerformed (ActionEvent e) {
if (e.getSource () == this.itemSalir) {
this.salir ();}
if (e.getSource () == this.itemInterna) { FrameInterno
inner = new FrameInterno ("Internal Frame Example");
escritorio.add (internal) / / add the frame to the desktop try {
interno.setSelected (true) / / say we start Focused
} catch ( PropertyVetoException e) {}}
}
As we see in the event that the user clicks on the menu is instantiated itemInterna a internalFrame and then adds it to the desktop. Finally we say that is initialized focused and caught the exception.
Step 2 Create an Internal Frame
FrameInterno public class extends JInternalFrame implements ActionListener {
... / / Swing components , buttons, label etc. public
FrameInterno (String title) {
super (title,
true, / / \u200b\u200bresize
true, / / \u200b\u200bclose
true, / / \u200b\u200bmaximize
true) / / try {
minimize interfazGrafica () ;
this.setVisible (true);
} catch (Exception e) {}}
} As we can define the buttons that will hold the frame in the upper right corner. This we define passing as arguments to the super class Boolean values \u200b\u200bif we allow that. interfazGrafica Then in method (), write the entire interface could be:
private void
interfazGrafica () throws Exception {
this.getContentPane (). SetLayout (null);
this.setSize (new Dimension ( 311, 347));
btn_cerrar.setText ("Close");
btn_cerrar.setBounds (new Rectangle (180, 270, 110, 30));
jLabel1.setText ("This is an internal frame");
jLabel1 . setBounds (new Rectangle (30, 65, 255, 35));
jLabel1.setFont (new Font ("Dialog", 1, 20));
jLabel1.setForeground (Color.blue)
this.btn_cerrar.addActionListener (this);
this.getContentPane (). add (jLabel1, null);
this.getContentPane (). add (btn_cerrar, null);}
And finally we need to make visible the frame with setVisible
Finally we define the method which requires us
interface ActionListener public void actionPerformed (ActionEvent e) {
if (e.getSource () == this.btn_cerrar) {
this.setVisible (false);
}}
With this we have a basic frame from which we can begin to play and add components such as buttons and label or anything else that comes to mind.
not stop watching it is good that when minimizing a internalFrame! Greetings
0 comments:
Post a Comment