亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

向文本區域添加滾動條

向文本區域添加滾動條

一只萌萌小番薯 2023-02-23 16:29:38
我使用 Eclipse Window Builder。當我點擊按鈕時,屏幕上會寫一些東西。但是由于我的打印件很長,所以我想使用滾動窗格。public class uyg2 extends JFrame {private JPanel contentPane;/** * Launch the application. */public static void main(String[] args) {    EventQueue.invokeLater(new Runnable() {        public void run() {            try {                uyg2 frame = new uyg2();                frame.setVisible(true);            } catch (Exception e) {                e.printStackTrace();            }        }    });}/** * Create the frame. */public uyg2() {    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    setBounds(100, 100, 450, 300);    contentPane = new JPanel();    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));    setContentPane(contentPane);    contentPane.setLayout(null);    JButton btnNewButton = new JButton("New button");    btnNewButton.setBounds(32, 29, 89, 23);    contentPane.add(btnNewButton);    JTextArea textArea = new JTextArea();    textArea.setBounds(10, 63, 233, 173);    contentPane.add(textArea);    ScrollPane scrollPane = new ScrollPane();    scrollPane.setBounds(249, 10, 173, 118);    contentPane.add(scrollPane);}
查看完整描述

2 回答

?
ABOUTYOU

TA貢獻1812條經驗 獲得超5個贊

所以,基于...


public class uyg1 extends JFrame {


    private JPanel contentPane;


    /**

     * Launch the application.

     */

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {

            public void run() {

                try {

                    uyg1 frame = new uyg1();

                    frame.setVisible(true);

                } catch (Exception e) {

                    e.printStackTrace();

                }


            }

        });

    }


    /**

     * Create the frame.

     */

    public uyg1() {

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setBounds(100, 100, 450, 300);

        contentPane = new JPanel();

        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

        contentPane.setLayout(new BorderLayout(0, 0));

        setContentPane(contentPane);

        JTextArea textArea = new JTextArea("Test");

        textArea.setSize(400, 400);

        JScrollPane scroll = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

        frame.getContentPane().add(scroll);

        frame.setVisible(true);

    }

}

textArea.setSize(400, 400);無關緊要,因為布局管理器將處理它。您可以通過構造函數提供大小調整提示JTextArea(String, int, int),但請記住,這是寬度/高度中的字符數,而不是像素數。


以下是給你的問題......


frame.getContentPane().add(scroll);

frame.setVisible(true);

因為frame未定義。由于該類是從 擴展的JFrame,因此它們毫無意義,應該只是


getContentPane().add(scroll);

setVisible(true);

但是,我要補充...


pack();

setLocationRelativeTo(null);

在它之前,因為它會給你一個總體上更好的體驗


查看完整回答
反對 回復 2023-02-23
?
達令說

TA貢獻1821條經驗 獲得超6個贊

您需要將 TextArea 添加到 ScrollPane。不要在內容窗格中添加文本區域。



查看完整回答
反對 回復 2023-02-23
  • 2 回答
  • 0 關注
  • 160 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號