0

我对 Java Swing/AWT 等人相当陌生,因此是这个问题。我有一个带有一些文本和几个 JButton 的简单对话框。我正在为这个对话框使用 GridbagLayout。

但是当我查看对话框时,JButton 的形状有点奇怪,我似乎也无法正确证明文本。

这是我正在使用的布局的限制吗?对于按钮或文本布局的问题,是否有更简单和优雅的解决方案?

下面附上了对话框的屏幕截图。

我的对话框类的代码是:

 public class UpgradePopupWindow extends JPanel implements ActionListener {
static final long serialVersionUID = 0;


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

JPanel panels;
JButton upgradeButton;
JButton restoreButton;
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;
}

protected JTextArea addMultiLineLabel(String text, int fontStyle, int fontSize, Object constraints) {

    JTextArea textArea = new JTextArea(text);

    textArea.setFont(new Font("SansSerif", fontStyle, fontSize));

    textArea.setLineWrap(true);
    textArea.setWrapStyleWord(true);
    textArea.setEditable(false);
    textArea.setBackground(new Color(0, 0, 0, 0)); // Zero alpha = transparent background
    textArea.setOpaque(false);
    textArea.setBorder(new TitledBorder(""));
    textArea.setAlignmentX(Component.CENTER_ALIGNMENT);

    add(textArea, constraints);

    return textArea;
}


private UpgradePopupWindow(JFrame frame, Object ft) {
    super(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    flash = (FlasherThread)ft;
    String text = "An error occurred during the attempt to update your device's software. We recommend the following: (1) Restore your device to its previous version, (2) back up important data, and then (3) try updating your device again. If you continue with the current update, only your previously backed-up data will be available.";
    //addFiller(5);
    //addLabel(text, Font.PLAIN, 12);
    gbc.gridy = 0;
    gbc.gridx = 0;
    gbc.gridwidth = 2;
    gbc.weightx = 1.0;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    addMultiLineLabel(text, Font.PLAIN, 12, gbc);

    //addText(text, true, Font.PLAIN);
    addFiller(20);
    newFrame = frame;
    gbc.gridy = 1;
    gbc.gridwidth = 1;
    gbc.fill = GridBagConstraints.NONE;

    upgradeButton = new JButton(upgrade);

    upgradeButton.setActionCommand("upgrade");
    upgradeButton.addActionListener(this);
    upgradeButton.setEnabled(true);
    upgradeButton.setBackground(Color.WHITE);
    upgradeButton.setForeground(Color.GRAY);
    add(upgradeButton,gbc);

    ++ gbc.gridx;
    restoreButton = new JButton(restore);
    restoreButton.setActionCommand("restore");
    restoreButton.addActionListener(this);
    restoreButton.setEnabled(true);
    //restoreButton.setForeground(Color.DARK_GRAY);
    restoreButton.setBackground(Color.DARK_GRAY);
    add(restoreButton,gbc);
    setOpaque(true);
    newFrame.setContentPane(this);
    //newFrame.getContentPane().setBackground(Color.LIGHT_GRAY);
}

protected void addFiller(int size) {
    Dimension diminsion = new Dimension(size, size);
    Filler filler = new Filler(diminsion, diminsion, diminsion);
    filler.setAlignmentX(Component.CENTER_ALIGNMENT);
    add(filler);
}

public static void createGUI(Object obj) {
    //Create and set up the frame.
    JFrame frame = new JFrame("PopUp Dialog");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setPreferredSize(new Dimension(440, 180));
    //create and setup the content pane
    UpgradePopupWindow popUpContentPane = new UpgradePopupWindow(frame, obj);
    popUpContentPane.setOpaque(true);
    frame.setContentPane(popUpContentPane);
    frame.pack();
    frame.setVisible(true);

}


public void actionPerformed(ActionEvent e) {

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

}

 ![alt text][1]

替代文字

4

2 回答 2

1

正如@Javaguru 所说 - 使用 MigLayout。对于文本,不要使用 JTextArea。该组件主要用于文本编辑。JLabel 足以满足您的目的 - 您甚至可以在其中使用 HTML。

您不必从头开始创建这种对话框。有很多选择。其中之一是我的Swing TaskDialog 框架;)

但是,如果您仍然坚持自己在这里实现它,请使用 MigLayout 进行简单的实现(即使使用 MigLayout 也可以使用几种不同的方式完成):

    import java.awt.Color;
    import java.awt.Dimension;

    import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingConstants;

    import net.miginfocom.swing.MigLayout;

public class TestDialog extends JDialog {

    private static final long serialVersionUID = 1L;

    private final JLabel label = new JLabel("Your text here");
    private final JButton restoreButton = new JButton("Restore Previous Version");
    private final JButton ugradeButton = new JButton("Continue Upgrade");



    public TestDialog( String title ) {
        super( (JDialog)null, title );
        setContentPane(createContent());
    }

    private JPanel createContent() {
        JPanel content = new JPanel();
        content.setPreferredSize( new Dimension(400, 100));
        content.setBackground(Color.WHITE);
        content.setLayout(new MigLayout("", "[400px,grow][pref!]", "[grow][pref!]"));
        this.label.setVerticalAlignment(SwingConstants.TOP);
        content.add(this.label, "cell 0 0 2 1,grow");
        content.add(this.restoreButton, "cell 1 1,alignx left,aligny top");
        content.add(this.ugradeButton, "cell 0 1,alignx right,aligny top");
        return content;
    }


    // very simplified test
    public static void main(String[] args) {

        TestDialog dlg =  new TestDialog("Popup Dialog");
        dlg.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dlg.pack();
        dlg.setLocationRelativeTo(null);
        dlg.setVisible(true);

    }

}
于 2010-07-13T23:22:27.187 回答
0

那里有很多更优雅的解决方案.. ;) 如果不是绝对需要(或仅使用 gui 编辑器),您真的不应该使用 GridbagLayout!简直是蛋疼!相反,我建议使用以下 LayoutManager 之一:

MigLayout: http: //www.miglayout.com/

表格布局:https ://tablelayout.dev.java.net/

与使用 GridbagLayout 及其配置开销相比,创建令人愉悦的布局要简单和直观得多。

于 2010-07-13T22:47:59.653 回答