2014-01-13 209 views
0

每次運行這段代碼時,輸​​出都會出現在屏幕左上角的對話框中,並且標題不顯示。JDialog彈出窗口太小

有沒有什麼辦法可以改變這個,讓對話框出現在中間並且尺寸可以接受?

代碼:

public class Test { 

public static void main(String[] args) { 

    JFrame f = new JFrame(); 

    final JDialog dialog = new JDialog(f, "Hello world", true); 
    Timer timer = new Timer(10000, new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      dialog.setVisible(false); 
      dialog.dispose(); 
     } 
    }); 
    timer.setRepeats(false); 
    timer.start(); 

    dialog.setVisible(true); 
    System.out.println("Dialog closed"); 

} 
} 

JDialog only appears at the top right hand corner instead of center of screen

回答

3

絕對。默認情況下,JDialog(或JFrame)將以此類似形式出現。你需要設置界限:

dialog.setBounds(xPosition, yPosition, width, height); 

但是,如果你只是爲它設置一些幻數,這將不能很好地擴展到其他系統。取而代之的是,得到了屏幕尺寸,並掀起認爲:

//static and final because no reason not to be. Insert this at the class definition 
static final Dimension SCREEN_DIMENSION = Toolkit.getDefaultToolkit().getScreenSize(); 
... 
//I'd also make this static and final and insert them at the class definition 
int dialogWidth = SCREEN_DIMENSION.width/4; //example; a quarter of the screen size 
int dialogHeight = SCREEN_DIMENSION.height/4; //example 
... 
int dialogX = SCREEN_DIMENSION.width/2 - dialogWidth/2; //position right in the middle of the screen 
int dialogY = SCREEN_DIMESNION.height/2 - dialogHeight/2; 

dialog.setBounds(dialogX, dialogY, dialogWidth, dialogHeight); 

另外,如果你將組件添加到JDialog,然後調用

dialog.pack(); 

該對話框會立即將容納最小尺寸組件。如果您正在使用應該緊密包裝的組件,請使用此方法;那麼你不必費力地手工構建正確的寬度和高度。

0

添加此行

dialog.setBounds(0, 0, 1000, 500); 
2

「有什麼辦法,我可以改變這種做法,在出現的對話框在中間和可接受的大小?」

如果你只需要添加組件到它,包裝它,而相對於空位置設置它,它應該工作正常

優選將.pack(),而不是設置大小。要使包正常工作,您需要實際添加組件。 .pack()將完全按照其名稱的建議進行 - 按照所有添加組件的首選尺寸打包框架。

還與setLocationRelativeTo()您設置對話相對於一個組件。如果您使用null,它將始終位於屏幕上。但是,如果您將相對於其父項的位置設置爲該框架,則它將顯示在框架中央。

我絕對不知道你想實現與計時器什麼,所以我只是喜歡無評論

見例如

import java.awt.BorderLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JDialog; 
import javax.swing.JFrame; 
import javax.swing.Timer; 

public class Test { 

    public static void main(String[] args) { 

     JFrame f = new JFrame(); 
     final MyDialog dialog = new MyDialog(f, "Title", true); 
     Timer timer = new Timer(10000, new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       dialog.setVisible(false); 
       dialog.dispose(); 
      } 
     }); 
     timer.setRepeats(false); 
     timer.start(); 

     dialog.setVisible(true); 
     System.out.println("Dialog closed"); 

    } 

    private static class MyDialog extends JDialog { 

     public MyDialog(JFrame frame, String title, boolean modal) { 
      super(frame, title, modal); 

      setLayout(new BorderLayout()); 
      add(new JButton("NORTH"), BorderLayout.NORTH); 
      add(new JButton("SOUTH"), BorderLayout.SOUTH); 
      add(new JButton("EAST"), BorderLayout.EAST); 
      add(new JButton("WEST"), BorderLayout.WEST); 
      add(new JButton("CENTER"), BorderLayout.CENTER); 

      pack(); 
      setLocationRelativeTo(null); 

     } 
    } 
} 

作爲一個側面說明,你應該像這樣運行來自Event Dispatch Thread(EDT)的Swing應用程序

public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable(){ 
     public void run() { 
      // the code from your main method here 
     } 
    }); 
} 
+0

或['SwingUtilities.invokeLater'](http://stackoverflow.com/questions/8847083/swingutilities-invokelater-vs-eventqueue-invokelater)。 – Justin

1

添加幾行代碼timer.start();dialog.setVisible(true);語句之間 -

timer.start(); 

    dialog.setPreferredSize(new Dimension(50, 150));//set your desired size 
    dialog.pack(); 
    Toolkit toolkit = Toolkit.getDefaultToolkit(); 
    Dimension screenSize = toolkit.getScreenSize(); 
    int iWidth = (screenSize.width - dialog.getWidth())/2; 
    int iHeight = (screenSize.height - dialog.getHeight())/2; 
    dialog.setLocation(iWidth, iHeight); 

    dialog.setVisible(true); 

此代碼將對話框設置爲屏幕的中心。

+0

'ToolKit'對於確定屏幕的大小並不是很好,因爲它沒有考慮像任務欄這樣的東西。通常最好使用'setLocationRelativeTo(null)'來居中窗口......通常更好的方法是允許內容確定自己喜歡的大小,並使用'pack'而不是直接將其設置到對話框中。 – MadProgrammer