2012-10-08 6 views
1

J'utilise JWS pour démarrer mon application. J'ai besoin de réinitialiser ma taille de JFrame et il ne devrait pas être redimensionné. J'ai essayé avec frame.setSize(new Dimension(500, 500)); et frame.setResizable(false); qui ne fonctionne pas. J'ai traversé le forum et essayé d'autres options disponibles. Mon code complet estcomment redimensionner la fenêtre de l'applet jws?

/** 
* 
*/ 
package com.bus.oprs; 

import java.util.Properties; 
import java.awt.Color; 
import java.awt.Cursor; 
import java.awt.Dimension; 
import java.awt.Font; 
import java.awt.GridLayout; 
import java.awt.Insets; 

import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 
import java.awt.image.BufferedImage; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
import javax.swing.JLabel; 
import javax.swing.SwingConstants; 
import javax.swing.UIManager; 
import javax.swing.border.EmptyBorder; 
import javax.swing.plaf.basic.BasicBorders; 
import javax.swing.plaf.basic.BasicBorders.ButtonBorder; 

import java.io.IOException; 
import java.io.InputStream; 
import java.net.URISyntaxException; 
import java.net.URL; 
import java.net.MalformedURLException; 

// classes of the web-start API, used in this example. 
import javax.imageio.ImageIO; 
import javax.jnlp.BasicService; 
import javax.jnlp.ServiceManager; 
import javax.jnlp.UnavailableServiceException; 

/** 
* @author sreekanth 
* @date Aug 29, 2012 11:33:16 AM 
* @version 1.0 
*/ 
public class ApsrtcOprsJNLP extends JFrame { 

    private static final long serialVersionUID = 8221867230431906706L; 
    private Color titleColor = new Color(241,248,255); 
    final private JPanel panel = new JPanel(); 
    final Properties properties = new Properties(); 
    /** 
    * @throws IOException, URISyntaxException 
    */ 
    public ApsrtcOprsJNLP() throws IOException, URISyntaxException { 
    super(); 
    launchApplication(); 
    } 


    private void loadProperties() { 
    try { 
     InputStream inputStreamUrl = this.getClass().getResourceAsStream("/AppResources.properties"); 
     properties.load(inputStreamUrl); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    } 

    private void setButtonProperties(JButton button){ 
    try { 
     Color buttonColor = new Color(102,153,255); 
     button.setBackground(buttonColor); 
     button.setForeground(Color.WHITE); 
     button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    } 

    private void launchApplication() throws IOException, URISyntaxException { 
    JLabel clickMessageLabel = null; 
    JLabel label = null; 
    Dimension dimension = null; 
    try { 
     final BasicService basicService = (BasicService) ServiceManager.lookup("javax.jnlp.BasicService"); 
     dimension = new Dimension(400, 300); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     this.loadProperties(); 
     setTitle(properties.getProperty("app.title")); 

     UIManager.put("InternalFrame.activeTitleBackground", new javax.swing.plaf.ColorUIResource(titleColor)); 
     JFrame.setDefaultLookAndFeelDecorated(true); 
     panel.setBackground(titleColor); 

     label = new JLabel(properties.getProperty("app.welcome.msg")); 
     Font newLabelFont = new Font(label.getFont().getName(), Font.BOLD, label.getFont().getSize()); 

     label.setHorizontalAlignment(SwingConstants.CENTER); 
     panel.add(label); 
     label.setFont(newLabelFont); 

     clickMessageLabel = new JLabel(properties.getProperty("app.click.link.txt")); 
     clickMessageLabel.setHorizontalAlignment(SwingConstants.CENTER); 
     panel.add(clickMessageLabel); 
     clickMessageLabel.setFont(newLabelFont); 

     String[] jButton = properties.getProperty("urls").split(","); 
     String[] jLabel = properties.getProperty("urlLabels").split(","); 

     for (int i = 0; i < jLabel.length; i++) { 
      JLabel labels = new JLabel(jLabel[i]); 
      panel.add(labels); 
      JButton button = new JButton(jButton[i]); 
      this.setButtonProperties(button); 
      button.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent ae) { 
      try { 
       URL url = new URL(ae.getActionCommand()); 
       basicService.showDocument(url); 
       setVisible(false); 
       dispose(); 
      } catch (MalformedURLException murle) { 
       JOptionPane.showMessageDialog(panel, 
        murle.getMessage()); 
      } 
      } 
     }); 
      panel.add(button); 
     } 

     panel.setBorder(new EmptyBorder(8, 8, 8, 8)); 
     setSize(dimension); 
     setResizable(false); 
     getContentPane().add(panel);   
     pack(); 

     setLocationRelativeTo(null); 
    } catch (UnavailableServiceException use) { 
     use.printStackTrace(); 
     System.exit(-1); 
    } finally { 
     label = null; 
     clickMessageLabel = null; dimension = null; 
    } 
    } 
    /** 
    * Construct the GUI and display it. 
    * 
    * @throws IOException 
    * @throws URISyntaxException 
    */ 
    public static void main(String[] args) throws IOException, URISyntaxException { 
    BufferedImage image = null; 
    ApsrtcOprsJNLP app = new ApsrtcOprsJNLP(); 
    try { 
     image = ImageIO.read(app.getClass().getResource("/logo.png")); 
     app.setPreferredSize(new Dimension(image.getWidth(null), image.getHeight(null))); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    app.setIconImage(image); 
    app.setVisible(true); 
    } 
} 

JNLP:

<?xml version="1.0" encoding="UTF-8" ?> 
<jnlp spec="1.0+" 
     href="http://localhost:9999/apsrtc-oprs-jnlp/launch-app.jnlp"> 
    <information> 
    <title>APSRTC-OPRS</title> 
    <vendor>APSRTC</vendor> 
    <description kind="one-line"> 
     APSRTC-OPRS WEB START 
    </description> 
    <icon kind="shortcut" href="logo.png" width="128" height="128"/> 
    <shortcut online="false"> 
     <desktop/> 
    </shortcut> 
    </information> 
    <resources> 
    <j2se version="1.6+" href="http://java.sun.com/products/autodl/j2se" /> 
    <jar href="http://localhost:9999/apsrtc-oprs-jnlp/oprs-jnlp.jar" main="true" /> 
    <applet-desc 
     name='apsrtc-oprs-jnlp' 
     main-class='com.abhibus.oprs.ApsrtcOprsJNLP' 
     width="800" 
     height="800"> 
    </applet-desc> 
    </resources> 
<application-desc main-class="com.abhibus.oprs.ApsrtcOprsJNLP"/> 
</jnlp> 

Merci

+1

1) Pour une meilleure aide plus tôt, un poster [SSCCE] (http://sscce.org/) sans tous les cochonneries supplémentaire d'utiliser l'API JNLP et la mise en icônes, etc. 2) Ce fichier JNLP est complètement invalide. Assurez-vous de vérifier (et corriger) en utilisant [JaNeLA] (http://pscode.org/janela/). 3) Il est généralement préférable de définir la taille du contenu ou du volet de contenu et d'appeler 'pack()' sur le cadre, ce qui permet au contenu d'avoir exactement la taille requise tout en tenant compte des décorations de cadre. –

Répondre

1

Appel pack() va changer la taille au minimum requis pour adapter le contenu de l'interface utilisateur placé dans cette fenêtre. Si nous supprimons cette ligne, les dimensions doivent rester là où vous les avez définies.

private void launchApplication() { 
    /* other code omitted */ 
    panel.setBorder(new EmptyBorder(8, 8, 8, 8)); 
    setSize(dimension); 
    setResizable(false); 
    getContentPane().add(panel);   
    //pack(); 
} 
+1

J'ai essayé mais la fenêtre est toujours redimensionnable et les dimensions ne sont pas réglées. Je pense que c'est quelque chose en rapport avec jws mais je ne sais pas comment? Merci pour la réponse –

+0

Si vous exécutez votre programme à partir de votre IDE ou en double-cliquant sur le fichier jar, se comporte-t-il comme avec jws? – Thorn

Questions connexes