Java JOptionPane和滚动功能

Java JOptionPane和滚动功能,java,swing,jscrollpane,jlist,joptionpane,Java,Swing,Jscrollpane,Jlist,Joptionpane,我想在JOptionPane中列出很多结果,但是,如果结果太多,我不确定如何添加滚动功能。如何将滚动条添加到JOptionPane?任何帮助都会很好 谢谢。将对象放入JList或其他类似组件中,将其放入JScrollPane中,并将JScrollPane放入JOptionPane中,您可以添加任何JComponent,include contains下面是一个示例,使用嵌入在JScrollPane中的JTextArea: JTextArea textArea = new JTextArea("I

我想在JOptionPane中列出很多结果,但是,如果结果太多,我不确定如何添加滚动功能。如何将滚动条添加到JOptionPane?任何帮助都会很好


谢谢。

将对象放入
JList
或其他类似组件中,将其放入
JScrollPane
中,并将
JScrollPane
放入
JOptionPane

中,您可以添加任何
JComponent
,include contains

下面是一个示例,使用嵌入在
JScrollPane
中的
JTextArea

JTextArea textArea = new JTextArea("Insert your Text here");
JScrollPane scrollPane = new JScrollPane(textArea);  
textArea.setLineWrap(true);  
textArea.setWrapStyleWord(true); 
scrollPane.setPreferredSize( new Dimension( 500, 500 ) );
JOptionPane.showMessageDialog(null, scrollPane, "dialog test with textarea",  
                                       JOptionPane.YES_NO_OPTION);

我也有类似的需求,一个带有滚动文本区的JOptionPane,我的任何类都可以在应用程序中写入它。这是为了向用户提供状态和进度信息。 我的方法是使它成为一个静态类,我实例化了一次,任何类都可以调用它的update方法来写入它。下面是代码和一个小驱动程序,希望它能节省一些时间。这可以不是静态的,只是适合我的需要

package com.acme.view;

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import com.acme.controller.MyController;
import com.acme.utils.NonModalMessage;

public class MyView {

    private JFrame frame;
    private int dialogNum = 0;
    private MyController myCntrlr;
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        NonModalMessage.createMesgDialog();
        NonModalMessage.updateMessage("Acme Anvil Targeting Progress");

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MyView window = new MyView();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public MyView() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 250, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        myCntrlr = new MyController();

        JButton btnMybutton = new JButton("myButton");

        btnMybutton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                NonModalMessage.setMessageVisible();
                if(dialogNum > 0 && dialogNum % 10 == 0){
                    NonModalMessage.clearMessage();
                    NonModalMessage.updateMessage("Acme Anvil Targeting Progress");
                    myCntrlr.someMethod("Controller reports Roadrunner sighted. Message Number: ", dialogNum);
                }
                NonModalMessage.getMessage();

                NonModalMessage.updateMessage("Message number: " + Integer.toString(dialogNum));
                System.out.println("dialogNum: " + dialogNum);
                dialogNum++;
            }
        });

        frame.getContentPane().add(btnMybutton, BorderLayout.NORTH);
    }


}

package com.acme.controller;
import com.acme.utils.NonModalMessage;

public class MyController {

    public MyController(){

    }

    public void someMethod(String mystring, int myInt){
        NonModalMessage.updateMessage(mystring + " "+ myInt);
    }

}

package com.acme.utils;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;

public class NonModalMessage {
    private static JTextArea textArea = null;
    private static JOptionPane oPane   = null;
    private static JDialog dialog     = null;
    private static JScrollPane myjsPane = null;
    public NonModalMessage(){}

    public static void createMesgDialog(){

        textArea = new JTextArea(); 
        textArea.setLineWrap(true);  
        textArea.setWrapStyleWord(true); 
        myjsPane = new JScrollPane(textArea);
        myjsPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        oPane = new JOptionPane();
        oPane.add(myjsPane);    

        //final JDialog dialog = pane.createDialog(myPane, "Progress Dialog");
        dialog = oPane.createDialog(null, "");
        dialog.setTitle("Progress Messages");
        dialog.setModal(false); 
        dialog.setSize(400, 250);
        dialog.setResizable(true);
        dialog.setAlwaysOnTop(true);
    }

    public static void setMessageVisible(){
        dialog.setVisible(true);
    }

    public static void updateMessage(String newMessage){
        String mytext = textArea.getText();
        if(mytext.isEmpty()){
            textArea.setText(newMessage);
        }
        else{
            textArea.setText(mytext + "\n" + newMessage);
        }

        oPane.setMessage( myjsPane );
    }
    public static String getMessage(){
        return textArea.getText();
    }

    public static void clearMessage(){
        textArea.setText("");
        oPane.setMessage( myjsPane );
    }

}

请将其更改为JList:-)@andrethompson同意showMessageDialog更适合这种情况。@mKorbel是的,JList也应该这样做。我在这里提供的示例仅用于指导。不是你的投票人,+1作为备选方案,也许你发布了这个问题的答案,
@Andrew Thompson
hmm也许你是对的,我们在这里错过了一些重要的东西,连接…..来自OP的pip-pip-pip断开了:-),然后我把我的评论留给你的话……你编辑了代码!:-)见鬼,如果我还没这么做的话,我会投票的