2011-12-28 51 views
2

如何調整JWindow的高度從JButton行動(我試過setSize和重繪,但它不起作用)?如何在Java中使用JButton動作調整JWindow的寬度和高度?

public class Gui extends JWindow implements KeyListener 
{ 
    public static Gui mystatic; 
    private static JPanel panelBgImg; 
    private final Dimension screen; 
    public static int w = 0; 
    public static int h = 0; 

    public void paintComponent(Graphics g) 
    { 
    } 

    public Dimension getPreferredSize() 
    {  
     return new Dimension(100, h); 
    } 

    public Gui() 
    { 
    .... 

    /* Button 1 */ 
    JButton jb2 = new JButton("") 
    { 
     public Dimension getPreferredSize() 
     { 
      return new Dimension(50,50); 
     }    
    }; 
    jb2.setOpaque(false); 
    jb2.setContentAreaFilled(false); 
    jb2.setBorderPainted(false); 

    jb2.addActionListener(new ActionListener() 
    {     
     public void actionPerformed(ActionEvent e) 
     { 
     h = 100;   
     /* HERE i am trying to resize the JWindow itself but not working 
       return new Dimension(100, h); */ 
     mystatic.setSize(screen.width, 100); 
     mystatic.repaint(); 
     } 
    }); 

    ... 

} 

回答

4

例如

請不要使用Thread.sleep(int) durring EDT,在此代碼示例演示,還有用於關閉當前JVM實例,否則這種情況不會從PC的RAM不見了

import java.util.logging.*; 
import javax.swing.*; 
import java.awt.BorderLayout; 
import java.awt.event.*; 

public class SSCCE { 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       createAndShowGUI(); 
      } 
     }); 
    } 

    private static void createAndShowGUI() { 
     final JWindow window = new JWindow(); 
     final JPanel windowContents = new JPanel(); 
     JLabel label = new JLabel("A window that is pushed into view.........."); 
     windowContents.add(label); 
     window.add(windowContents); 
     window.pack(); 
     window.setLocationRelativeTo(null); 
     final int desiredWidth = window.getWidth(); 
     window.getContentPane().setLayout(null); 
     window.setSize(0, window.getHeight()); 
     window.setVisible(true); 
     Timer timer = new Timer(15, new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       int newWidth = Math.min(window.getWidth() + 1, desiredWidth); 
       window.setSize(newWidth, window.getHeight()); 
       windowContents.setLocation(newWidth - desiredWidth, 0); 
       if (newWidth >= desiredWidth) { 
        ((Timer) e.getSource()).stop(); 
        //restore original layout 
        window.getContentPane().setLayout(new BorderLayout()); 
        window.validate(); 
        try { 
         Thread.sleep(5000); 
        } catch (InterruptedException ex) { 
         Logger.getLogger(SSCCE.class.getName()).log(Level.SEVERE, null, ex); 
        } 
        System.exit(0); 
       } 
      } 
     }); 
     timer.start(); 
    } 

    private SSCCE() { 
    } 
} 
+0

請告訴我SSCCE的完整形式,大多數人都使用這個縮寫。我將被迫。問候 – 2011-12-28 09:21:34

+0

http://sscce.org/是虛擬的rulles( - :由他的陛下設計:-) @Andrew Thompson, – mKorbel 2011-12-28 09:31:36

+0

Thankyou的信息,只是在你的代碼中捕捉到了這個縮寫。好像對我真的有幫助。 Registers – 2011-12-28 09:38:10

3

這是一個小程序,其中setSize()方法正在使用Event Dispatcher Thread。

import java.awt.event.*; 

import javax.swing.*; 

public class WindowTest extends JWindow implements ActionListener 
{ 
    private JPanel panel; 
    private JButton sizeButton; 
    private int height = 220; 
    private int width = 220; 

    public WindowTest() 
    {  
    panel = new JPanel(); 
    sizeButton = new JButton("Set Size"); 
    sizeButton.addActionListener(this); 

    panel.add(sizeButton); 

    add(panel); 
    setSize(width, height); 
    setLocationRelativeTo(null); 
} 

public void actionPerformed(ActionEvent ae) 
{ 
    height += 10; 
    width += 10; 

    this.setSize(width, height); 
} 

public static void main(String... args) 
{ 
    SwingUtilities.invokeLater(new Runnable() 
    { 
     public void run() 
     { 
     new WindowTest().setVisible(true); 
     } 
    }); 
} 
} 

希望這會有所幫助,你只需要安排的事件調度線程進行作業,爲的GUI顯示進行的更改不hickups,作爲主要方法來完成。

Regards

+0

發佈SSCCE +1 – mKorbel 2011-12-28 09:32:04

相關問題