显示JButton的简单JFrame对话框

时间:2010-06-25 20:44:06

标签: swing awt jframe jpanel jbutton

首先,我是Java Swing / AWT的新手,并且不熟悉JFrame,JPanel& amp;的工作原理。 JButton的。我想只显示一个带有一些文本和几个JButton的简单PopUp对话框,当点击两个按钮中的任何一个时它应该退出。我在这堂课中有大部分的逻辑,但是我仍然在努力使对话显示:(

任何想法我可能做错了什么:

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 个答案:

答案 0 :(得分:2)

您应该使用JOptionPane而不是自己动手。