0

私はJavaSwing/ AWT et alにかなり慣れていないので、質問があります。いくつかのテキストといくつかの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 を使用することもできます。

この種のダイアログを最初から作成する必要はありません。多くのオプションがあります。それらの 1 つは、私の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 に答える