簡體   English   中英

在JDialog上使用滾動條擺動TextArea

[英]Swing TextArea with Scrollbar on JDialog

我有一種情況,當用戶單擊按鈕時,將打開一個帶有文本區域的彈出窗口。 需要時,文本區域將包含一些帶有滾動條的內容。 為此,我使用了JDialog並在JDialog中添加了文本區域。 就我而言,我能夠在單擊按鈕時顯示對話框,並在對話框中顯示包含內容的文本區域。 但是我無法獲得文本區域的滾動條。 我也將JScrollPane用於文本區域。

public class DialogPanel {

    public void createDialog() {
        final JFrame mainFrame = new JFrame();
        mainFrame.setVisible(true);
        mainFrame.setSize(500, 600);
        mainFrame.setLayout(new BorderLayout());
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JButton btn = new JButton("Open Dialog");
        mainFrame.add(btn, BorderLayout.SOUTH);
        JTextField txtField = new JTextField();
        mainFrame.add(txtField, BorderLayout.NORTH);
        btn.setPreferredSize(new Dimension(100, 100));
        btn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                JDialog dialog = new JDialog(mainFrame);
                dialog.setLocationByPlatform(true);
                JTextArea txtArea = new JTextArea();
                txtArea.setAutoscrolls(true);
                txtArea.setPreferredSize(new Dimension(900, 500));
                txtArea.setBorder(BorderFactory.createLineBorder(Color.BLACK));
                txtArea.setFont(new Font("courier new", Font.PLAIN, 12));
                txtArea.setLineWrap(true);
                JScrollPane txtAreaScroll = new JScrollPane();
                txtAreaScroll.setViewportView(txtArea);
                txtAreaScroll.setAutoscrolls(true);

                File file;
                String line = null;
                StringBuilder fileContents = new StringBuilder();
                try {
                    file = new File(
                            "D:\\Softwares\\Apache\\apache-tomcat-7.0.47\\RUNNING.txt");
                    BufferedReader reader = new BufferedReader(new FileReader(
                            file));
                    while ((line = reader.readLine()) != null) {
                        fileContents.append(line + "\n");
                    }
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

                txtArea.setText(fileContents.toString());

                dialog.add(txtAreaScroll);
                dialog.pack();
                dialog.setVisible(true);
            }
        });
    }

    public static void main(String[] args) {
        DialogPanel dialogPanel = new DialogPanel();
        dialogPanel.createDialog();
    }
}

在此處輸入圖片說明

本質上, txtArea.setPreferredSize(new Dimension(900, 500)); 正在刪除JTextArea用來確定顯示所有文本所需的空間量的自動計算。 您實際上是在說,將僅需要500個像素的高度。

您可以“設置”滾動窗格的首選大小,但實際上不建議這樣做。 相反,您想要更改JTextAreagetPreferredScrollableViewportSize返回的值。

這告訴滾動窗格將可視區域擴大到多大...如果可以的話...

JTextArea txtArea = new JTextArea() {

    @Override
    public Dimension getPreferredScrollableViewportSize() {
        return new Dimension(900, 500);
    }

};

查看Scrollable了解更多詳細信息

更新

正如AndrewThompson所指出的那樣,一種更好的(也是首選的方式)將是簡單地為JTextArea指定行和列,並讓其根據平台渲染功能找出含義。

JTextArea txtArea = new JTextArea(40, 100);

是的,為了簡單...

您正在使用dialog.pack()請參見此處並為對話框定義自己的大小

這樣,您可以將文字區域與scroll一起使用:

                JTextArea txtArea = new JTextArea(40,100);
                txtArea.setAutoscrolls(true);
                txtArea.setBorder(BorderFactory.createLineBorder(Color.BLACK));
                txtArea.setFont(new Font("courier new", Font.PLAIN, 12));
                txtArea.setLineWrap(true);
                txtArea.setText(j);
                JScrollPane txtAreaScroll = new JScrollPane (txtArea, 
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
                txtAreaScroll.setViewportView(txtArea);
                txtAreaScroll.setAutoscrolls(true);
                // now add scroll pane in frame

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM