Redimensionarea ferestrei

Cunoscutul limbaj de programare Java a fost creat să întrunească câteva caracteristici de bază, printre care OOP, să fie independent de arhitectură (portabilitatea), să fie dinamic şi securizat. Aici vom vorbi despre Java SE, Java ME, respectiv Java EE. Dacă acesta este domeniul tău de interes, aceasta este secţiunea potrivită pentru tine.

Redimensionarea ferestrei

Postby valentin » 22 Jan 2011, 18:25

Am facut o aplicatie, daca o pot numi asa, care afiseaza o eticheta in functie de anumite evenimente. Problema mea este ca nu mi se redimensioneaza automat fereastra cand se adauga o eticheta noua. Unde este problema ?
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import java.util.*;
  5.  
  6. public class ValidateInputGUI extends JFrame
  7.                               implements ActionListener,KeyListener {
  8.    
  9.     private final static int COLUMNS = 32;
  10.     private JTextField  field = new JTextField(COLUMNS);
  11.     private JLabel newField;
  12.     private JPanel panel = new JPanel();
  13.     private JButton     button = new JButton("Press Ok");
  14.     private FlowLayout  layout = new FlowLayout();
  15.    
  16.     public ValidateInputGUI(String appName) {
  17.         super(appName);
  18.     }
  19.    
  20.     public void addComponentsToPane(final Container pane) {
  21.         panel.setLayout(layout);
  22.         layout.setAlignment(FlowLayout.LEFT);
  23.         panel.add(field);
  24.         panel.add(button);
  25.         button.addActionListener(this);
  26.         field.addKeyListener(this);
  27.         pane.add(panel);
  28.     }
  29.     public void actionPerformed(ActionEvent e) {
  30.         newField = new JLabel("ActionPerformed");
  31.         panel.add("NewField", newField);
  32.         panel.validate();
  33.     }
  34.     public void keyPressed(KeyEvent e) {
  35.         newField = new JLabel("KeyPressed");
  36.         panel.add("NewField", newField);
  37.         panel.validate();
  38.     }
  39.     public void keyTyped(KeyEvent e) {
  40.         newField = new JLabel("KeyTyped");
  41.         panel.add("NewField", newField);
  42.         panel.validate();
  43.     }
  44.     public void keyReleased(KeyEvent e) {
  45.         newField = new JLabel("KeyReleased");
  46.         panel.add("NewField", newField);
  47.         panel.validate();
  48.     }
  49.     private static void createAndShowGUI() {
  50.         ValidateInputGUI frame = new ValidateInputGUI("Valideaza input");
  51.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  52.         frame.addComponentsToPane(frame.getContentPane());
  53.         frame.pack();
  54.         frame.setVisible(true);
  55.     }
  56.     public static void main(String[] args) {
  57.         javax.swing.SwingUtilities.invokeLater(new Runnable() {
  58.             public void run() {
  59.                 createAndShowGUI();
  60.             }
  61.         });
  62.     }
  63. }
0,0p / 0 votes
User avatar
valentin
Bit
 
Joined: 22 Jan 2011
Status: 0

Re: Redimensionarea ferestrei

Postby smith » 22 Jan 2011, 20:23

Am să îți dau un răspuns dar poate nu va fi cel mai bun doarece și eu am început Java de puțin timp și deocamdată nu am ajuns să lucrez cu swing (decât ce am învățat acum de pe net :)) ).

Din câte am înțeles de pe internet, o fereastră este redimensionată când e apelată metoda pack(). Ea calculează dimensiunile obiectelor din fereastră și re-setează mărimea ferestrei. Astefel dacă ai pune this.pack() în fiecare metodă care e apelată în funcție de eventuri fereastra s-ar redimensiona.

Problema care apare în continuare este că fereastra se redimensionează doar pe orizontală. Cred că e din cauza layoutului pe care l-ai ales (FlowLayout). Dacă redimensionezi manual, labelurile se vor muta singure (din cauza comportamentului FlowLayout).
Nu sunt prea sigur de mine, așa că nu te baza prea tare pe ce spun.

Așteaptă să vadă Morpheus threadul. El îți va răspunde exact :P
0,0p / 0 votes
Ilea Cristian
User avatar
smith
Enum
 
Joined: 29 Dec 2009
Location: Cluj-Napoca
Status: 82

Re: Redimensionarea ferestrei

Postby Adrian » 22 Jan 2011, 20:23

Apeleaza pack() de fiecare data cand adaugi o componenta noua unui panel.
1p / 1 votes
User avatar
Adrian
Byte
 
Joined: 04 May 2010
Status: 13.5

Re: Redimensionarea ferestrei

Postby morpheus » 22 Jan 2011, 20:39

Presupun ca scopul programului este de a testa evenimente de tipul keyPressed, actionPerformed, etc.
Redimensionarea ferestrei in timp ce tastezi ar fi o greseala de design al interfetei grafice (uzabilitatea ar lasa enorm de dorit).
Mai bine ai afisa mesajele intr-o componenta JTextArea pusa intr-un JScrollPane.

  1.  
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5.  
  6. public class ValidateInputGUI extends JFrame
  7.                               implements ActionListener,KeyListener {
  8.    
  9.     private final static int COLUMNS = 32;
  10.     private JTextField  field = new JTextField(COLUMNS);
  11.     private JButton button = new JButton("Press Ok");
  12.     private JTextArea textArea = new JTextArea();
  13.    
  14.     public ValidateInputGUI(String appName) {
  15.         super(appName);
  16.         this.setPreferredSize(new Dimension(500, 300));
  17.     }
  18.    
  19.     public void addComponentsToPane(final Container pane) {
  20.         button.addActionListener(this);
  21.         field.addKeyListener(this);
  22.          
  23.         // add the text field and button to a panel having
  24.         // flow layout
  25.         JPanel flowPanel = new JPanel();
  26.         FlowLayout flowLayout = new FlowLayout();
  27.         flowLayout.setAlignment(FlowLayout.LEFT);
  28.         flowPanel.setLayout(flowLayout);
  29.         flowPanel.add(field);
  30.         flowPanel.add(button);
  31.         flowPanel.setPreferredSize(new Dimension(500, 60));
  32.        
  33.         // add the text area to a scroll pane
  34.         textArea.setLineWrap(true);
  35.         textArea.setWrapStyleWord(true);
  36.         JScrollPane scrollPanel = new JScrollPane();
  37.         scrollPanel.getViewport().add(textArea);
  38.        
  39.         // the top panel have a border layout
  40.         JPanel topPanel = new JPanel();
  41.         topPanel.setLayout(new BorderLayout());
  42.        
  43.         // add the panel containing the button and field
  44.         // to the page start (north part) of the top layout
  45.         topPanel.add(flowPanel, BorderLayout.PAGE_START);
  46.         // add the scroll panel to the center of the top layout
  47.         topPanel.add(scrollPanel, BorderLayout.CENTER);
  48.         pane.add(topPanel);
  49.     }
  50.     public void actionPerformed(ActionEvent e) {
  51.         textArea.append("ActionPerformed ");
  52.     }
  53.     public void keyPressed(KeyEvent e) {
  54.         textArea.append("KeyPressed ");
  55.     }
  56.     public void keyTyped(KeyEvent e) {
  57.         textArea.append("KeyTyped ");
  58.     }
  59.     public void keyReleased(KeyEvent e) {
  60.         textArea.append("KeyReleased ");
  61.     }
  62.     private static void createAndShowGUI() {
  63.         ValidateInputGUI frame = new ValidateInputGUI("Valideaza input");
  64.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  65.         frame.addComponentsToPane(frame.getContentPane());
  66.         frame.pack();
  67.         frame.setVisible(true);
  68.     }
  69.     public static void main(String[] args) {
  70.         javax.swing.SwingUtilities.invokeLater(new Runnable() {
  71.             public void run() {
  72.                 createAndShowGUI();
  73.             }
  74.         });
  75.     }
  76. }
  77.  
1p / 2 votes
User avatar
morpheus
Word
 
Joined: 30 Dec 2009
Location: Bucharest, Romania
Status: 54.84

Re: Redimensionarea ferestrei

Postby valentin » 23 Jan 2011, 01:13

@smith: ma bucur sa aud ca am un coleg de "suferinta" :) . sper sa tinem legatura, poate chiar sa colaboram :D
@Adrian && @morpheus: Multumesc pentru sfaturi ! It works !
0,0p / 0 votes
User avatar
valentin
Bit
 
Joined: 22 Jan 2011
Status: 0


Return to Java

Who is online

Users browsing this forum: No registered users and 0 guests