2015-07-11 70 views
2

我已經創建了一個對話框,顯示消息,但該對話框的大小,每個我跑我下面的Java Swing應用程序的鏈接dailog的屏幕快照時間變化的方法:

Improrer Message Box Display
Proper Message Box Display
爲什麼的JDialog不displaing正確

下面是我爲的JDialog顯示創建的方法(SetMSGDialog)

public class DemoClass 
{ 
    private static JDialog MSGDialog; 
    private static JPanel MSGPanel; 
    private static JLabel MSGLabel; 
    private static JButton MSGButton; 

    DemoClass() 
    { 
     MSGDialog = new JDialog(MSGDialog,"Message",JDialog.ModalityType.APPLICATION_MODAL); 
     MSGPanel = new JPanel(null); 
     MSGLabel = new JLabel(); 
     MSGButton = new JButton("Close"); 
    } 

    public static void SetMSGDialog(String MSG) 
    { 
     MSGDialog.setModal(true); 
     MSGDialog.setSize(400,125); 
     MSGDialog.setResizable(false); 
     MSGDialog.setLocationRelativeTo(null); 
     MSGLabel.setText(MSG); 
     MSGButton.setText("Close"); 
     MSGLabel.setBounds(25, 25, 375, 30); 
     MSGButton.setBounds(160,70,80,30); 
     MSGPanel.add(MSGLabel); 
     MSGPanel.add(MSGButton); 
     MSGDialog.add(MSGPanel); 
     MSGDialog.setVisible(true); 
    } 

    public static void main(String[] args) 
    { 
     DemoClass MsgBox = new DemoClass(); 
     MsgBox.SetMSGDialog("Please Login First"); 
    } 
} 

請告訴我,我做錯了。爲了正確顯示Jdialog,我必須做些什麼。

+1

1)擺脫'的setSize(...)','的setBounds(...)'之類的。 2)使用佈局管理器來幫助確定組件的大小和位置。 3)在顯示對話框之前打包。 4)不要超過你的程序。 –

+0

BTW - Ubuntu,Eclipse,Linux?沒有必要告訴我們你的操作系統和IDE,除非代碼只在這些特定條件下失敗!相信我,這個代碼在任何系統上都會失敗,使用任何IDE。 –

回答

3

你問爲什麼你的JDialog顯示不正常,主要原因是你的代碼忽略了你的組件已經使用的佈局管理器。一個快速和可怕的解決方案是使用絕對或null佈局,但不推薦用於組件放置,因爲這使得非常不靈活的GUI,雖然它們可能在單個平臺和屏幕分辨率上看起來不錯,但它們在大多數其他平臺上看起來很糟糕或屏幕分辨率,並且很難更新和維護。

建議:

  • 擺脫的setSize(...)的,的setBounds(...)等。
  • 使用佈局管理器來幫助確定組件的大小和位置。
  • 顯示對話框之前打包。
  • 請勿在程序中過度使用靜態修飾符。
  • 你可以做一些類似下面的代碼,但已經顯示了這一點,一個JOptionPane是顯示這種類型的消息最簡單的方法。

import java.awt.BorderLayout; 
import java.awt.Component; 
import java.awt.Font; 
import java.awt.GridBagLayout; 
import java.awt.Window; 
import java.awt.event.ActionEvent; 
import java.awt.event.KeyEvent; 
import javax.swing.*; 

@SuppressWarnings("serial") 
public class DemoClass2 extends JPanel { 
    // constants 
    private static final String TITLE = "Message"; 
    private static final float LABEL_POINTS = 24f; 
    private static final int L_GAP = 15; 
    private static final int B_GAP = 25; 

    // static poor man's singlton instance 
    private static DemoClass2 demoClass2; 
    private JDialog dialog = new JDialog(); 
    private JLabel label = new JLabel("", SwingConstants.CENTER); 

    public DemoClass2() { 
     dialog.setModal(true); 
     dialog.setTitle(TITLE); 

     label.setFont(label.getFont().deriveFont(Font.BOLD, LABEL_POINTS)); 
     label.setBorder(BorderFactory.createEmptyBorder(L_GAP, L_GAP, L_GAP, L_GAP)); 

     JPanel btnPanel = new JPanel(new GridBagLayout()); 
     btnPanel.setBorder(BorderFactory.createEmptyBorder(B_GAP, B_GAP, B_GAP, B_GAP)); 
     btnPanel.add(new JButton(new DisposeAction("Close", KeyEvent.VK_C))); 

     setLayout(new BorderLayout()); 
     //setBorder(border); 
     add(label, BorderLayout.PAGE_START); 
     add(btnPanel, BorderLayout.CENTER); 

     dialog.getContentPane().add(this); 
    } 

    private void setMessage(String message) { 
     label.setText(message); 
    } 

    private void display() { 
     dialog.setResizable(false); 
     dialog.pack(); 
     dialog.setLocationRelativeTo(null); 
     dialog.setVisible(true); 
    } 

    public void setMessageAndDisplay(String message) { 
     setMessage(message); 
     display(); 
    } 

    public static void displayMessage(String message) { 
     if (demoClass2 == null) { 
     demoClass2 = new DemoClass2(); 
     } 
     demoClass2.setMessageAndDisplay(message); 
    } 

    private class DisposeAction extends AbstractAction { 
     public DisposeAction(String name, int mnemonic) { 
     super(name); 
     putValue(MNEMONIC_KEY, mnemonic); 
     } 

     @Override 
     public void actionPerformed(ActionEvent e) { 
     Component c = (Component) e.getSource(); 
     if (c == null) { 
      return; 
     } 
     Window win = SwingUtilities.getWindowAncestor(c); 
     if (win == null) { 
      return; 
     } 
     win.dispose();   
     } 
    } 

    private static void createAndShowGui() { 
     DemoClass2.displayMessage("Fubars Rule!!!"); 

     DemoClass2.displayMessage("This is a bit of a longer message. The dialog should get bigger."); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGui(); 
     } 
     }); 
    } 
} 
0

您是否嘗試刪除MSGDialog.setSize(400,125);並讓Swing決定其大小?

+0

是的,我已經試過,但然後對話框很小,很難看到 – Abhishek

+0

在調用'MSGDialog.setVisible(true)'之前嘗試調用'pack()'? –

+1

這裏的麻煩在於,OP已經通過不使用佈局「已經在自己的腳下開槍了」。當我們調用'pack()'時,通常它會將幀縮小到顯示組件所需的大小,但由於佈局爲'null',容器無法計算其首選或最小大小,並將返回0x0對彼此而言。當然,解決方案是**使用佈局**(和填充/邊框用於白色空間)來添加組件,然後*使用'pack()'和'setVisible(true)'.. –