3

このすべての情報を 1 つのダイアログに表示するにはどうすればよいですか? ファイルを実行するたびに、さまざまなダイアログ ボックスが表示されるので、すべての情報を 1 つのダイアログ ボックスに表示する必要があります。

JOptionPane.showMessageDialog(null,"Your Name:"+a1,"Output",JOptionPane.INFORMATION_MESSAGE );
JOptionPane.showMessageDialog(null,"Your age:"+age,"Output",JOptionPane.INFORMATION_MESSAGE );
JOptionPane.showMessageDialog(null,"Your Birth year:"+a3,"Output",JOptionPane.INFORMATION_MESSAGE );
JOptionPane.showMessageDialog(null,"Your Height:"+H,"Output",JOptionPane.INFORMATION_MESSAGE );
JOptionPane.showMessageDialog(null,"Your Weight:"+W,"Output",JOptionPane.INFORMATION_MESSAGE );
JOptionPane.showMessageDialog(null,"Your BMI:"+BMI,"Output",JOptionPane.INFORMATION_MESSAGE );
4

4 に答える 4

7

タグを使用できHTMLます:

JOptionPane.showMessageDialog(null, "<html><br>First line.<br>Second line.</html>");

または、オブジェクトの配列を渡すこともできます:

オブジェクトの配列は、垂直スタックに配置された一連のメッセージ (オブジェクトごとに 1 つ) として解釈されます。

ドキュメントで述べたように。

于 2013-10-13T06:56:18.097 に答える
5

JPanel配置するコンポーネントを配置するため、適切なレイアウトで を作成し、これJPanelを に追加JOptionPaneしてメッセージを表示します。

ロジックを理解するのに役立つ小さな例:

import java.awt.*;
import javax.swing.*;

public class JOptionPaneExample {
    private String name;
    private int age;
    private int birthYear;

    public JOptionPaneExample() {
        name = "Myself";
        age = 19;
        birthYear = 1994;
    }

    private void displayGUI() {
        JOptionPane.showMessageDialog(
            null, getPanel(), "Output : ",
                JOptionPane.INFORMATION_MESSAGE);
    }

    private JPanel getPanel() {
        JPanel panel = new JPanel(new GridLayout(0, 1, 5, 5));
        JLabel nameLabel = getLabel("Your Name : " + name);
        JLabel ageLabel = getLabel("Your Age : " + age);
        JLabel yearLabel = getLabel("Your Birth Year : " + birthYear);
        panel.add(nameLabel);
        panel.add(ageLabel);
        panel.add(yearLabel);

        return panel;
    }

    private JLabel getLabel(String title) {
        return new JLabel(title);
    }

    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                new JOptionPaneExample().displayGUI();
            }
        };
        EventQueue.invokeLater(runnable);
    }
}

出力:

オプションペインの例

于 2013-10-13T08:14:02.760 に答える
4

JOptionPane の Javadoc、特に上部のメッセージ部分を見てください。Object の配列を渡すと、その要素がダイアログの垂直スタックに配置されます。

JOptionPane.showMessageDialog(null,
                              new Object[]{"line 1","line 2","line 3","line 4"},
                              JOptionPane.INFORMATION_MESSAGE);
于 2013-10-13T06:56:43.900 に答える
0

これらすべての行の代わりにこれを入力してください:

JOptionPane.showMessageDialog(null,"Your Name: " + a1 +"\n Your age: " + age +"\n Your Birth year: "+ a3 +
    "\n Your Height: " + H +"\n Your Weight: "+ W +"\n Your BMI: "+ BMI); 
于 2018-09-23T11:11:42.440 に答える