第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

如何制作用戶可調(diào)整大小的 JTextArea?

如何制作用戶可調(diào)整大小的 JTextArea?

拉丁的傳說 2022-07-20 19:31:59
似乎唯一的選擇是設(shè)置行數(shù),但我需要我的文本區(qū)域可以為用戶調(diào)整大小。JScrollPane 有幫助,但是當(dāng)有很多文本時,我想讓用戶調(diào)整區(qū)域本身的大小。我該怎么做呢?如果我可以為此目的使用另一個類,我會完全同意的。簡化的代碼是import javax.swing.*;import java.awt.*;public class Problematic {    public static void main(String[] args) {        JFrame f = new JFrame("frame");        f.setLayout(new BorderLayout());        JPanel p1 = new JPanel();        JPanel p = new JPanel();        JButton button = new JButton("Whatever here");        JTextArea t2 = new JTextArea(5, 30);        JScrollPane scrollPane = new JScrollPane(t2);        scrollPane.setSize(600, 400);        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);        t2.setText("this is some random text\nthat may go for many rows\nso it may look messy");        p1.add(button);        p.add(scrollPane);        f.add(p, BorderLayout.NORTH);        f.add(p1, BorderLayout.CENTER);        f.setSize(600, 500);        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);        f.setVisible(true);    }}
查看完整描述

3 回答

?
慕的地6264312

TA貢獻(xiàn)1817條經(jīng)驗 獲得超6個贊

您可以使用JSplitPane使窗口的各個區(qū)域可調(diào)整大小。試試下面的例子。請參閱我在代碼中的評論。


import javax.swing.*;

import java.awt.*;


public class Problematic {


  public static void main(String[] args) {

    JFrame f = new JFrame("frame");

    f.setLayout(new BorderLayout());


    JPanel p1 = new JPanel();

    JPanel p = new JPanel();

    // Set BorderLayout so that scroll pane fills the panel

    p.setLayout(new BorderLayout());


    JButton button = new JButton("Whatever here");

    JTextArea t2 = new JTextArea(5, 30);


    JScrollPane scrollPane = new JScrollPane(t2);


    // Setting scroll pane size is not necessary

    //scrollPane.setSize(600, 400);

    scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);


    t2.setText("this is some random text\nthat may go for many rows\nso it may look messy");


    p1.add(button);

    p.add(scrollPane);


    // Use JSplitPane to make the panels resizable

    f.add(new JSplitPane(JSplitPane.VERTICAL_SPLIT, p, p1), BorderLayout.CENTER);


    f.setSize(600, 500);

    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    f.setVisible(true);

  }

}


查看完整回答
反對 回復(fù) 2022-07-20
?
慕婉清6462132

TA貢獻(xiàn)1804條經(jīng)驗 獲得超2個贊

作為第一個改進(jìn),您可以進(jìn)行一些小的布局更改,這將JTextArea占用整個空間并隨著框架調(diào)整大?。?/p>


import java.awt.BorderLayout;

import java.awt.GridLayout;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

import javax.swing.WindowConstants;


public class Problematic {


    public static void main(String[] args) {

        JFrame f = new JFrame("frame");

        f.setLayout(new BorderLayout()); 


        JPanel p = new JPanel(new GridLayout(1, 1)); //assign gridlayout so text area fills panel 

        JTextArea t2 = new JTextArea(5, 30);

        t2.setText("this is some random text\nthat may go for many rows\nso it may look messy");


        JScrollPane scrollPane = new JScrollPane(t2);

        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

        p.add(scrollPane);

        f.add(p, BorderLayout.CENTER); //place text area panel in center position 


        JPanel p1 = new JPanel();

        JButton button = new JButton("Whatever here");

        p1.add(button);

        f.add(p1, BorderLayout.PAGE_END);


        f.setSize(600, 500);

        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        f.setVisible(true);

    }

}

為了獲得更大的靈活性,您可以添加JSplitPane:


public class Problematic {


    public static void main(String[] args) {

        JFrame f = new JFrame("frame");

        f.getContentPane().setLayout(new BorderLayout());


        JPanel p1 = new JPanel();

        JButton button = new JButton("Whatever here");

        p1.add(button);


        JPanel p = new JPanel(new GridLayout(1, 1)); //assign gridlayout so text area fills panel

        JTextArea t2 = new JTextArea(5, 30);

        t2.setText("this is some random text\nthat may go for many rows\nso it may look messy");


        JScrollPane scrollPane = new JScrollPane(t2);       scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

        p.add(scrollPane);


        JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, p,p1);

        f.getContentPane().add(splitPane, BorderLayout.CENTER);


        f.setSize(600, 500);

        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        f.setVisible(true);

    }

}


查看完整回答
反對 回復(fù) 2022-07-20
?
慕的地10843

TA貢獻(xiàn)1785條經(jīng)驗 獲得超8個贊

這是 c0der 的實現(xiàn),但設(shè)置了更簡化的生命周期。每一行都解釋了正在發(fā)生的事情和時間。


import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.text.JTextComponent;


public class TextApp implements Runnable {

    private static final String APP_NAME = "Text App";


    private JFrame frame;

    private JTextArea txtAra;

    private JButton button;


    // This is a generic action that handles clearing the text of a JTextComponent

    // It can also be a stand-alone class

    private static class ClearAction <T extends JTextComponent> implements ActionListener {

        private T txtAra;


        public ClearAction(T txtAra) {

            this.txtAra = txtAra;

        }


        @Override

        public void actionPerformed(ActionEvent e) {

            this.txtAra.setText("");

        }

    }


    public TextApp() {

        // Initialize instance fields

        frame = new JFrame(APP_NAME);

        txtAra = new JTextArea(5, 30);

        button = new JButton("Clear Text");


        // Internal panels used for layout

        JPanel mainPanel = new JPanel(new GridLayout(1, 1));

        JScrollPane scrollPane = new JScrollPane(txtAra);

        JPanel buttonPanel = new JPanel();


        // Add components to containers

        frame.setLayout(new BorderLayout());

        frame.add(mainPanel, BorderLayout.CENTER);

        frame.add(buttonPanel, BorderLayout.PAGE_END);

        mainPanel.add(scrollPane);

        buttonPanel.add(button);


        // Additional setup

        scrollPane.setSize(600, 400);

        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);


        // Add listeners

        button.addActionListener(new ClearAction(txtAra));

    }


    @Override

    public void run() {

        // Set text

        txtAra.setText("this is some random text\nthat may go for many rows\nso it may look messy");


        // Prepare frame

        frame.pack();

        frame.setLocationRelativeTo(null);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setVisible(true);

    }


    public static void main(String[] args) {

        SwingUtilities.invokeLater(new TextApp());

    }

}


查看完整回答
反對 回復(fù) 2022-07-20
  • 3 回答
  • 0 關(guān)注
  • 146 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號