Han*_*hah 3 java swing joptionpane
如何在一个对话框中显示所有这些信息?每次运行文件时都会出现不同的对话框,我真的需要它们出现在一个包含所有信息的对话框中.
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 );
Run Code Online (Sandbox Code Playgroud)
你可以使用HTML标签:
JOptionPane.showMessageDialog(null, "<html><br>First line.<br>Second line.</html>");
Run Code Online (Sandbox Code Playgroud)
或者您可以传递一组对象:
对象数组被解释为以垂直堆栈排列的一系列消息(每个对象一个)
如文档中所述.
只需创建一个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);
}
}
Run Code Online (Sandbox Code Playgroud)
输出:

| 归档时间: |
|
| 查看次数: |
15755 次 |
| 最近记录: |