2017-04-09 72 views
-1

我有一個JButton的JDialog(截圖中的右邊框)。當我單擊JButton時,我想更改另一個JFrame中的JPanel大小(截圖中的左邊框)。通過JButton更改JPanel大小

我的問題是,目前我的應用程序不會將JPanel的大小更改爲新大小(20,20),當我單擊按鈕「單擊我」時。當我點擊按鈕時,我的測試文本「Hello world」出現在控制檯中。

實施例的屏幕截圖:如果沒有顯示截圖

備選鏈路:http://www.dachaufsetzer.net/files/images/other/java-question-1.jpg

這是我的代碼:

Dialog.java(所述的JDialog)

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.*; 

public class Dialog extends JDialog implements ActionListener{ 

    public Dialog(){ 
     JDialog d = new JDialog(); 

     d.setSize(300, 300); 

     JButton b = new JButton("click me"); 
     b.addActionListener(new Generator()); 
     d.add(b); 


     d.setVisible(true); 


    } 

} 

Gener ator.java(在Frame.java使用的JPanel)

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class Generator extends JPanel implements ActionListener{ 

    int width; 
    int height; 

    public Generator(){ 

    } 

    public Generator(int w, int h){ 

     this.width = w; 
     this.height = h; 

     super.setBackground(Color.red); 
     super.setPreferredSize(new Dimension(this.width, this.height)); 

    } 

    @Override 
    public void paintComponent(Graphics g){ 
     super.paintComponent(g); 
     g.setColor(Color.BLUE); 
     g.fillOval(0, 0, 50, 50); 

    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     super.setPreferredSize(new Dimension(20, 20)); 
     System.out.println("Hello world"); 
    } 
} 

Frame.java(JFrame中其使用Generator.java)

import java.awt.*; 
import java.awt.event.ActionEvent; 
import javax.swing.*; 

public class Frame extends JFrame { 

    public Frame(){ 

     JFrame f = new JFrame(); 
     f.setSize(400, 400); 

     Generator g = new Generator(2000, 2000); 

     JScrollPane scrollPane = new JScrollPane(g); 
     scrollPane.setPreferredSize(new Dimension(300,300)); 

     f.add(scrollPane); 

     f.setVisible(true); 
    } 

}

主.java(主類)

public class Main { 

    public static void main(String[] args) { 

     Frame window = new Frame(); 
     Dialog d = new Dialog(); 

    }  
} 

感謝您的支持。

回答

0

你正在做的事情沒有意義。

關於Frame類:

在你Frame類,你在構造函數中創建另一個JFrame的對象使得Frame類無用。您還需要保存添加到Frame ScrollPane的Generator面板的引用,以便從Dialog更改其大小。這是Frame類的變化:

import java.awt.*; 
import java.awt.event.ActionEvent; 
import javax.swing.*; 

public class Frame extends JFrame { 
    public Generator g; 

    public Frame(){ 
     setSize(400, 400); 
     g = new Generator(2000, 2000); 
     JScrollPane scrollPane = new JScrollPane(g); 
     scrollPane.setPreferredSize(new Dimension(300,300)); 
     add(scrollPane); 
     setVisible(true); 
    } 
} 

關於Dialog類:

你也做了同樣的錯誤。您正在創建Dialog類構造函數中的另一個JDialog對象。另一個巨大的錯誤是,您正在將一個new Generator()作爲動作偵聽器添加到按鈕中。要更改添加到FrameGenerator面板的大小,您需要獲取該面板的引用並將其添加爲ActionListener。這是Dialog類的變化:

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.*; 

public class Dialog extends JDialog implements ActionListener{ 

    public Dialog(Generator g){ 
     setSize(300, 300); 
     JButton b = new JButton("click me"); 
     b.addActionListener(g); 
     add(b); 
     setVisible(true); 
    } 
} 

正如你可以看到,現在Dialog構造函數有Generator作爲參數。該參數是您想要更改的面板。

然後在主類:

​​

當您使用new創建一個類的實例,這個實例都有它的屬性是從同一類型的其他實例不同。在你的代碼中,你創建了多個相同類型的實例,認爲改變其中一個會改變另一個(我認爲你實際上並不瞭解它們是兩個單獨的東西)。

+0

非常感謝您爲Flood2d提供的巨大幫助。現在很清楚! – spiceflo

1

ActionListener代碼中,您還需要在重置首選大小後調用revalidate()。這將調用佈局管理器。您可能還需要在revalidate()之後調用repaint()

當然這隻會改變面板的大小。對話框大小將保持不變。如果您希望調整大小,您可能還需要在對話框中調用pack()。您可以通過在ActionListener中使用SwingUtilities.windowForComponent(...)來獲得對話框的引用。

+0

感謝camickr對你的支持,與你的反饋和Flood2d的反饋,它的工作原理 – spiceflo