我可以设置 JOptionPane.showMessageDialog 的 window 的大小吗?

Can I set the size of the JOptionPane.showMessageDialog's window?

我正在开发一个将一些对象(人)存储在数组中的简单应用程序,我正在尝试使用 JOptionPane.showMessageDialog 在屏幕上列出它们 (obj.toString)。

问题是在大约六个对象之后,面板变得比屏幕高,并且没有侧边栏或通过拖动它来改变大小的方法。

有没有办法实现这些功能之一,或者可以将其设置为将对象显示为网格、列或任何其他可以修复它的东西?

我想人们要么忘记了,要么不知道,您可以将“活动组件”传递给 JOptionPane class 的 message 参数,这为您提供了无限可能,例如...

你可以...

使用 JTextArea 包裹在 JScrollPane 中,例如...

import java.awt.EventQueue;
import java.text.NumberFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.StringJoiner;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class Test {
    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                List<Person> people = new ArrayList<>(100);
                Random rnd = new Random();
                for (int index = 0; index < 100; index++ ) {
                    Person person = new Person(index, "Person " + (index + 1), "1234567890", LocalDate.now(), LocalDate.now(), LocalDate.MAX, rnd.nextDouble() * 10.0);
                    people.add(person);
                }

                StringJoiner joiner = new StringJoiner("\n----------------------------------------\n\n\n\n", "[\n", "\n]");
                for (Person person : people) {
                    joiner.add(person.toString());
                }

                JTextArea ta = new JTextArea(20, 30);
                ta.setText(joiner.toString());
                ta.setCaretPosition(0);
                ta.setEditable(false);
                JOptionPane.showMessageDialog(null, new JScrollPane(ta));
            }
        });
    }

    public class Person {
        private int id;
        private String name;
        private String phoneNumber;
        private LocalDate birthDate;
        private LocalDate registrationDate;
        private LocalDate lastUpdate;
        private double finalNote;

        public Person(int id, String name, String phoneNumber, LocalDate birthDate, LocalDate registrationDate, LocalDate lastUpdate, double finalNote) {
            this.id = id;
            this.name = name;
            this.phoneNumber = phoneNumber;
            this.birthDate = birthDate;
            this.registrationDate = registrationDate;
            this.lastUpdate = lastUpdate;
            this.finalNote = finalNote;
        }

        @Override
        public String toString() {
            return new StringJoiner("\n")
                    .add("Person ID: " + getId())
                    .add("Name: " + getName())
                    .add("Phone number: " + getPhoneNumber())
                    .add("Birth date: " + DateTimeFormatter.ISO_LOCAL_DATE.format(getBirthDate()))
                    .add("Registration date: " + DateTimeFormatter.ISO_LOCAL_DATE.format(getRegistrationDate()))
                    .add("Last date: " + DateTimeFormatter.ISO_LOCAL_DATE.format(getLastUpdate()))
                    .add("Final note: " + NumberFormat.getNumberInstance().format(getFinalNote()))
                    .toString();
        }

        public int getId() {
            return id;
        }

        public String getName() {
            return name;
        }

        public String getPhoneNumber() {
            return phoneNumber;
        }

        public LocalDate getBirthDate() {
            return birthDate;
        }

        public LocalDate getRegistrationDate() {
            return registrationDate;
        }

        public LocalDate getLastUpdate() {
            return lastUpdate;
        }

        public double getFinalNote() {
            return finalNote;
        }


    }
}

你可以...

使用 JList,例如...

import java.awt.Component;
import java.awt.EventQueue;
import java.text.NumberFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.StringJoiner;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                List<Person> people = new ArrayList<>(100);
                Random rnd = new Random();
                for (int index = 0; index < 100; index++) {
                    Person person = new Person(index, "Person " + (index + 1), "1234567890", LocalDate.now(), LocalDate.now(), LocalDate.MAX, rnd.nextDouble() * 10.0);
                    people.add(person);
                }

                DefaultListModel<Person> model = new DefaultListModel<>();
                model.addAll(people);
                JList<Person> list = new JList<>(model);
                list.setCellRenderer(new PersonListCellRenderer());
                list.setVisibleRowCount(5);

                JOptionPane.showMessageDialog(null, new JScrollPane(list));
            }
        });
    }

    public class PersonListCellRenderer extends DefaultListCellRenderer {

        @Override
        public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            if (value instanceof Person) {
                Person person = (Person) value;
                StringBuilder sb = new StringBuilder(32);
                sb.append("<html><body>")
                        .append("Person ID: ").append(person.getId()).append("<br>")
                        .append("Name: ").append(person.getName()).append("<br>")
                        .append("Phone number: ").append(person.getPhoneNumber()).append("<br>")
                        .append("Birth date: ").append(DateTimeFormatter.ISO_LOCAL_DATE.format(person.getBirthDate())).append("<br>")
                        .append("Registration date: ").append(DateTimeFormatter.ISO_LOCAL_DATE.format(person.getRegistrationDate())).append("<br>")
                        .append("Last update: ").append(DateTimeFormatter.ISO_LOCAL_DATE.format(person.getLastUpdate())).append("<br>")
                        .append("Final note: ").append(person.getId()).append("<br>")
                        .append("<hr>")
                        .append("</body></html>");

                value = sb.toString();
            }
            return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); //To change body of generated methods, choose Tools | Templates.
        }

    }

    public class Person {

        private int id;
        private String name;
        private String phoneNumber;
        private LocalDate birthDate;
        private LocalDate registrationDate;
        private LocalDate lastUpdate;
        private double finalNote;

        public Person(int id, String name, String phoneNumber, LocalDate birthDate, LocalDate registrationDate, LocalDate lastUpdate, double finalNote) {
            this.id = id;
            this.name = name;
            this.phoneNumber = phoneNumber;
            this.birthDate = birthDate;
            this.registrationDate = registrationDate;
            this.lastUpdate = lastUpdate;
            this.finalNote = finalNote;
        }

        @Override
        public String toString() {
            return new StringJoiner("\n")
                    .add("Person ID: " + getId())
                    .add("Name: " + getName())
                    .add("Phone number: " + getPhoneNumber())
                    .add("Birth date: " + DateTimeFormatter.ISO_LOCAL_DATE.format(getBirthDate()))
                    .add("Registration date: " + DateTimeFormatter.ISO_LOCAL_DATE.format(getRegistrationDate()))
                    .add("Last date: " + DateTimeFormatter.ISO_LOCAL_DATE.format(getLastUpdate()))
                    .add("Final note: " + NumberFormat.getNumberInstance().format(getFinalNote()))
                    .toString();
        }

        public int getId() {
            return id;
        }

        public String getName() {
            return name;
        }

        public String getPhoneNumber() {
            return phoneNumber;
        }

        public LocalDate getBirthDate() {
            return birthDate;
        }

        public LocalDate getRegistrationDate() {
            return registrationDate;
        }

        public LocalDate getLastUpdate() {
            return lastUpdate;
        }

        public double getFinalNote() {
            return finalNote;
        }

    }
}

你可以...

使用 JTable,例如...

import java.awt.EventQueue;
import java.text.NumberFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.StringJoiner;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                List<Person> people = new ArrayList<>(100);
                Random rnd = new Random();
                for (int index = 0; index < 100; index++) {
                    Person person = new Person(index, "Person " + (index + 1), "1234567890", LocalDate.now(), LocalDate.now(), LocalDate.MAX, rnd.nextDouble() * 10.0);
                    people.add(person);
                }

                TableModel model = new PersonTableModel(people);
                JTable table = new JTable(model);
                table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

                JOptionPane.showMessageDialog(null, new JScrollPane(table));
            }
        });
    }

    public class PersonTableModel extends AbstractTableModel {

        private List<Person> people;
        private String[] columnNames = new String[]{"ID", "Name", "Phone number", "Date of birth", "Date of registration", "Last updated", "Final note"};

        public PersonTableModel(List<Person> people) {
            this.people = people;
        }

        @Override
        public int getRowCount() {
            return people.size();
        }

        @Override
        public int getColumnCount() {
            return columnNames.length;
        }

        @Override
        public String getColumnName(int column) {
            return columnNames[column];
        }

        @Override
        public Class<?> getColumnClass(int columnIndex) {
            switch (columnIndex) {
                case 0:
                    return Integer.class;
                case 1:
                case 2:
                    return String.class;
                case 3:
                case 4:
                case 5:
                    return LocalDate.class;
                case 6:
                    return Double.class;
            }
            return String.class;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            Person person = people.get(rowIndex);
            switch (columnIndex) {
                case 0:
                    return person.getId();
                case 1:
                    return person.getName();
                case 2:
                    return person.getPhoneNumber();
                case 3:
                    return person.getBirthDate();
                case 4:
                    return person.getRegistrationDate();
                case 5:
                    return person.getLastUpdate();
                case 6:
                    return person.getFinalNote();
            }
            return null;
        }

    }

    public class Person {

        private int id;
        private String name;
        private String phoneNumber;
        private LocalDate birthDate;
        private LocalDate registrationDate;
        private LocalDate lastUpdate;
        private double finalNote;

        public Person(int id, String name, String phoneNumber, LocalDate birthDate, LocalDate registrationDate, LocalDate lastUpdate, double finalNote) {
            this.id = id;
            this.name = name;
            this.phoneNumber = phoneNumber;
            this.birthDate = birthDate;
            this.registrationDate = registrationDate;
            this.lastUpdate = lastUpdate;
            this.finalNote = finalNote;
        }

        @Override
        public String toString() {
            return new StringJoiner("\n")
                    .add("Person ID: " + getId())
                    .add("Name: " + getName())
                    .add("Phone number: " + getPhoneNumber())
                    .add("Birth date: " + DateTimeFormatter.ISO_LOCAL_DATE.format(getBirthDate()))
                    .add("Registration date: " + DateTimeFormatter.ISO_LOCAL_DATE.format(getRegistrationDate()))
                    .add("Last date: " + DateTimeFormatter.ISO_LOCAL_DATE.format(getLastUpdate()))
                    .add("Final note: " + NumberFormat.getNumberInstance().format(getFinalNote()))
                    .toString();
        }

        public int getId() {
            return id;
        }

        public String getName() {
            return name;
        }

        public String getPhoneNumber() {
            return phoneNumber;
        }

        public LocalDate getBirthDate() {
            return birthDate;
        }

        public LocalDate getRegistrationDate() {
            return registrationDate;
        }

        public LocalDate getLastUpdate() {
            return lastUpdate;
        }

        public double getFinalNote() {
            return finalNote;
        }

    }
}