2010-06-25 86 views
0

首先,我真的是Java Swing/AWT的新手,並且不會錯綜複雜地熟悉JFrame,JPanel & JButton的工作原理。我只想用一些文本和幾個JButton來顯示一個簡單的彈出式對話框,當兩個按鈕中的任何一個被點擊時它都應該退出。我有大部分在這個類邏輯的,但是我仍然在努力使對話框顯示:(簡單的JFrame對話框顯示JButton

任何想法我可能是做錯了:

public class UpgradePopupWindow extends JFrame implements ActionListener { 

static final long serialVersionUID = 0; 


final String upgrade = "Continue Upgrade"; 
final String restore = "Restore"; 

JPanel panels; 
JButton flashMe; 
JButton helpMe; 
JTextArea Message; 
JFrame newFrame; 
FlasherThread flash; 

    protected JTextArea addText(String text, boolean visible, int fontStyle) { 

    JTextArea textArea = new JTextArea(text); 

    textArea.setFont(new Font("SansSerif", fontStyle, 12)); //$NON-NLS-1$ 

    textArea.setLineWrap(true); 
    textArea.setWrapStyleWord(true); 
    textArea.setEditable(false); 
    textArea.setBackground(Color.DARK_GRAY); 
    textArea.setForeground(Color.WHITE); 
    textArea.setOpaque(false); 
    textArea.setVisible(visible); 
    textArea.setAlignmentX(Component.CENTER_ALIGNMENT); 

    add(textArea); 

    return textArea; 
} 

public UpgradePopupWindow(Object ft) { 
    super("PopUp Dialog"); 
    flash = (FlasherThread)ft; 
    String text = "An error occurred during the attempt to update your software. We recommend the following: (1) Restore your phone to its previous version, If you continue with the current update, only your previously backed-up data will be available."; 
    //addFiller(5); 
    JLabel label = addLabel(text, Font.PLAIN, 12); 
    //addText(text, true, Font.PLAIN); 
    //addFiller(20); 
    //newFrame = frame; 
    JPanel cards = new JPanel(); 
    cards.setLayout(null); 
    cards.setOpaque(false); 

    cards.setBounds(400, 200, 250, 150); 
    cards.add(label); 
    flashMe = new JButton(upgrade); 

    flashMe.setActionCommand("upgrade"); 
    flashMe.addActionListener(this); 
    flashMe.setEnabled(true); 
    cards.add(flashMe); 
    //add(flashMe); 


    helpMe = new JButton(restore); 
    helpMe.setActionCommand("restore"); 
    helpMe.addActionListener(this); 
    helpMe.setEnabled(true); 
    //add(helpMe); 
    cards.add(helpMe); 
    setContentPane(cards); 
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
    //setOpaque(true); 
    //newFrame.setContentPane(this); 
    pack(); 
    //setVisible(true); 
} 

    protected JLabel addLabel(String text, int fontStyle, int size) { 
    JLabel label = new JLabel(text); 
    label.setFont(new Font("SansSerif", fontStyle, size)); 
    label.setAlignmentX(Component.CENTER_ALIGNMENT); 
    label.setOpaque(false); 
    label.setVisible(true); 
    //label.setForeground(Color.BLUE); 

    //add(label); 
    return label; 
} 

    protected void addFiller(int size) { 
    /* 
    * create some space before the progress bar 
    */ 
    Dimension diminsion = new Dimension(size, size); 
    Filler filler = new Filler(diminsion, diminsion, diminsion); 
    filler.setAlignmentX(Component.CENTER_ALIGNMENT); 

    add(filler); 
} 

    public void actionPerformed(ActionEvent e) { 

    if("restore".equals(e.getActionCommand())) { 
     System.out.println("restore button selected"); 
     //flash.setUpgradeRestoreChoice("restore"); 
     //newFrame.dispose(); 
     dispose(); 
    } else if ("upgrade".equals(e.getActionCommand())) { 
     System.out.println("upgrade button selected"); 
     flash.setUpgradeRestoreChoice("upgrade"); 
     //newFrame.dispose(); 
     dispose(); 
    } 
} 

} 
+1

你爲什麼要註釋掉setVisible(true)?另外,你能顯示創建對話框的代碼嗎(你的主要方法也許?) – 2010-06-25 21:11:43

+0

可能的重複[爲什麼我的簡單JFrame顯示奇怪? ](http://stackoverflow.com/questions/3121504/why-is-my-simple-jframe-displaying-weird) – trashgod 2010-06-26 00:58:56

回答

2

您應該使用JOptionPane代替滾動你自己的。