Простой диалог JFrame, отображающий JButton - PullRequest
0 голосов
/ 26 июня 2010

Во-первых, я настоящий новичок в Java Swing / AWT и не очень хорошо знаком с работой JFrame, JPanel & JButton. Я хочу просто отобразить простое всплывающее диалоговое окно с текстом и парой 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 Ответ

2 голосов
/ 26 июня 2010

Вы должны использовать JOptionPane вместо того, чтобы бросать свои собственные.

...