- java version "1.6.0_24"
- Java(TM) SE Runtime Environment (build 1.6.0_24-b07)
- Java HotSpot(TM) Server VM (build 19.1-b02, mixed mode)
- Linux ADRIAN-LAPTOP 2.6.32-5-686 #1 SMP Tue Mar 8 21:36:00 UTC 2011 i686 GNU/Linux
- Debian 6
Am atașat la post un fișier .jar care exemplifică problema.
Pentru layout am folosit un GridLayout cu 3 linii și 1 coloana. Pe linii am în următoarea ordine: un obiect de tip canvas și două butoane. Când se redimensionează fereastra, dreptunghiul din obiectul de tip canvas ar trebui să se redeseneze în funcție de dimensiunile ferestrei. Problema este că butoanele se redimensionează în timp REAL, iar dreptunghiul nu (decât după ce butonul mouse-ului este lăsat liber).
Cum aș putea să fac să se redimensioneze și deptunghiul în timp real?
--LATER EDIT--
Am să postez totuși codul, poate nu am fost destul de clar în explicații (și apoi, ca bump):
- import java.awt.*;
- import java.awt.event.*;
- class WindowSizeListener extends ComponentAdapter {
- Window win;
- public WindowSizeListener(Window w){
- this.win = w;
- }
- public void componentResized(ComponentEvent e) {
- Dimension winDimension = win.getSize();
- win.setGraphSize( winDimension.width -20 , (winDimension.height /3)-20 );
- win.repaintGraph();
- }
- }
- @SuppressWarnings("serial")
- class Board extends Canvas {
- Dimension canvasSize = new Dimension(100, 100);
- public void paint(Graphics g) {
- g.setColor(Color.RED);
- g.drawRect(0, 0, canvasSize.width-2, canvasSize.height-3);
- }
- public Dimension getMinimumSize() {
- return canvasSize;
- }
- public Dimension getPreferredSize() {
- return canvasSize;
- }
- public void setSize(int width, int height){
- canvasSize = new Dimension(width, height);
- }
- }
- @SuppressWarnings("serial")
- class Window extends Frame {
- private Button closeButton = new Button("Close");
- private Button aboutButton = new Button("About");
- private Board graph = new Board();
- public Window(String titlu) {
- super(titlu);
- }
- public void initialize() {
- this.setSize(300, 300);
- this.setLocationToCenter();
- this.initializeComponents();
- //window size listener
- this.addComponentListener( new WindowSizeListener(this));
- //listener for close X button
- this.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- System.exit(0);
- }
- });
- }
- //sets the window in the center of the screen
- private void setLocationToCenter(){
- Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
- Dimension windowSize = this.getSize();
- int y = (screenSize.height - windowSize.height) / 2;
- int x = (screenSize.width - windowSize.width ) / 2;
- this.setLocation(x, y);
- }
- //initialize components
- private void initializeComponents(){
- this.setLayout(new GridLayout(3,1));
- this.add(graph);
- this.add(aboutButton);
- this.add(closeButton);
- }
- public void setGraphSize(int width, int height){
- graph.setSize(width, height);
- }
- public void repaintGraph(){
- graph.repaint();
- }
- public Dimension getGraphDimension(){
- return graph.canvasSize;
- }
- }
- public class HelloWorld {
- public static void main(String args[]) {
- Window f = new Window("Test");
- f.initialize();
- f.setVisible(true);
- }
- }
Welcome to BitCell. Click here to register !