Home >Java >javaTutorial >Java value Swing series control one

Java value Swing series control one

怪我咯
怪我咯Original
2017-06-25 10:22:571644browse

Swing series of controls

JTextArea

JTextArea is a control that implements multi-line text

Constructor

  • JTextArea() Construct a new TextArea.

  • JTextArea(Document doc) Constructs a new JTextArea with the given document model, all other parameters default to (null, 0, 0) .

  • JTextArea(Document doc, String text, int rows, int columns) Constructs a new JTextArea with the specified number of rows and columns and the given model.

  • JTextArea(int rows, int columns) Constructs a new, empty TextArea with the specified number of rows and columns.

  • JTextArea(String text) Constructs a new TextArea that displays the specified text.

  • JTextArea(String text, int rows, int columns) Constructs a new TextArea with the specified text, number of rows, and number of columns.

Commonly used methods

  • ##append(String text) Insert text at the end

  • getColumns()

  • ##getLineWrap()

    Get the line wrapping strategy of the text area

  • setLineWrap(boolean wrap)

    Set the line wrapping strategy. If set to true, it will automatically wrap at the end of a line.

  • setWrapStyleWord(boolean wrap)

    Set line breaks and word breaks, that is, if you have not finished writing a word when it reaches the end, your word will be moved to the next line together, instead of just Move one word down so that the words are not separated

  • setFont(Font f)

    Set the font

  • insert(String str, int pos)

    Insert the specified text at the specified position.

  • replaceRange(String str, int start, int end)

    Replace the text from the indicated start position to the end position with the given new text.

  • setRows(int rows)

    Set the number of rows for this TextArea.

JScrollPane

Constructor

  • JScrollPane()

    Create a An empty (portless view) JScrollPane that displays both horizontal and vertical scroll bars when needed.

  • JScrollPane(Component view)

    Creates a JScrollPane that displays the content of the specified component. Horizontal and vertical scroll bars will be displayed as long as the content of the component exceeds the view size.

  • JScrollPane(Component view, int vsbPolicy, int hsbPolicy)

    Create a JScrollPane that displays the view component in a viewport, and the view position can use a pair of scrolls strip control. vsbPolicy and hsPolicy are used to control when the scroll bar appears. The values ​​are as follows:

    #HORIZONTAL_SCROLLBAR_ALAWAYS
  • : Always show horizontal scroll axis.

  • HORIZONTAL_SCROLLBAR_AS_NEEDED
  • : The horizontal scroll axis appears when the horizontal area of ​​the component content is larger than the display area.

  • HORIZONTAL_SCROLLBAR_NEVER
  • : The horizontal scroll axis is always not displayed.

  • VERTICAL_SCROLLBAR_ALWAYS
  • : Always show the vertical scroll axis.

  • VERTICAL_SCROLLBAR_AS_NEEDED
  • : The vertical scroll axis appears when the vertical area of ​​the component content is larger than the display area.

  • VERTICAL_SCROLLBAR_NEVER
  • : The vertical scroll axis is always not displayed.

##JScrollPane(int vsbPolicy, int hsbPolicy)
    Create an empty pane with the specified scroll bar policy (View without port) JScrollPane.
  • Commonly used methods

setHorizontalScrollBarPolicy(int policy)
    Determine when the horizontal scroll bar Displayed on the scroll pane.
  • setVerticalScrollBarPolicy(int policy)
  • Determines when a vertical scroll bar is displayed on the scroll pane.
  • setColumnHeaderView(Component view)
  • Create a column header viewport (if necessary), set its view and then add this column header viewport to the scroll pane . Set the horizontal header
  • setRowHeaderView(Component view)
  • Set the vertical header, which is to create a row title viewport
  • setViewportBorder(Border viewportBorder)
  • Adds a border around the viewport.
  • Example
package demo;import javax.swing.*;import javax.swing.border.Border;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class demo extends JFrame {private JTextField textField;public demo() {super();this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setBounds(100, 100, 400, 300);
        JPanel panel_North = new JPanel();this.getContentPane().add(panel_North, BorderLayout.NORTH);
        FlowLayout flowLayout = (FlowLayout) panel_North.getLayout();
        flowLayout.setAlignment(FlowLayout.LEFT);

        JLabel label = new JLabel("name:");
        label.setFont(new Font("微雅软黑", Font.BOLD + Font.ITALIC, 20));
        panel_North.add(label);

        textField = new JTextField(20);
        textField.setFont(new Font("微雅软黑", Font.BOLD + Font.ITALIC, 20));
        panel_North.add(textField);

        JTextArea textArea = new JTextArea(6, 30);
        textArea.setFont(new Font("微雅软黑", Font.BOLD + Font.ITALIC, 20));
        textArea.setWrapStyleWord(true);
        textArea.setLineWrap(true);

        JPanel panel_center = new JPanel();

        FlowLayout flowLayout_center = (FlowLayout) panel_center.getLayout();
        flowLayout_center.setAlignment(FlowLayout.LEFT);this.getContentPane().add(panel_center, BorderLayout.CENTER);  //设置中间的组件JScrollPane scrollPane = new JScrollPane(textArea);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);   //设置水平滚动条出现的时间scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);//设置垂直滚动条出现的时间panel_center.add(scrollPane);

        JPanel panel_header = new JPanel();
        JLabel label_header = new JLabel("水平表头");
        panel_header.add(label_header);
        scrollPane.setColumnHeaderView(panel_header);   //设置水平表头JPanel panel_v = new JPanel();
        JLabel label_v = new JLabel("垂直表头");
        panel_v.add(label_v);
        scrollPane.setRowHeaderView(panel_v);    //设置垂直表头}public static void main(String[] args) {
        demo f = new demo();
        f.setVisible(true);

    }

}

JScrollBar

滚动条的实现。用户在滚动条内移动滑块可确定显示区域中的内容。该程序通常将显示调整为滚动条的结束代表可显示内容的结束,或内容的 100%。滚动条的开始为可显示内容的开始,或 0%。然后滑块在其边界中的位置转换为可显示内容对应的百分比。

构造函数

  • JScrollBar() 创建具有下列初始值的垂直滚动条

  • JScrollBar(int orientation) 创建具有指定方向和下列初始值的滚动条,其中的值为JScrollBar.VERTICAL(垂直滚动条),JScrollBar.HORIZONTAL(水平滚动条)

  • JScrollBar(int orientation, int value, int extent, int min, int max) 创建具有指定方向、值、跨度、最小值和最大值的一个滚动条。value为当前滚动条位于的位置,min和max共同组成了滚动条的范围,extent表示滚动条的跨越的宽度

常用方法

*addAdjustmentListener(AdjustmentListener l) 添加一个滚动条调整的事件侦听器,例子如下:

scrollBar_1.addAdjustmentListener(new AdjustmentListener() {    //添加一个事件侦听器@Overridepublic void adjustmentValueChanged(AdjustmentEvent e) {
                scrollBar_1=(JScrollBar)e.getSource();    //获得监听的控件,这里要强制转换成滚动条类型的textField_North.setText(""+scrollBar_1.getValue());    //r如果滚动条移动了,那么就将刻度显示在文本域中}
        });
  • setModel(BoundedRangeModel newModel) 设置处理滚动条的以下四个基本属性的模型:minimum、maximum、value 和 extent。通过这个函数可以将滚动条与其他的控件绑定在一起,下面将会演示一下JTextField绑定滚动条

//获取文本字段的可见性,如果文本字段大小大于分配给他的大小那么可以对他进行调整BoundedRangeModel boundedRangeModel = textField.getHorizontalVisibility();//设置处理滚动条的以下四个基本属性的模型:minimum、maximum、value 和 extent。scrollBar.setModel(boundedRangeModel);   //通过这个的绑定,就将滚动条和文本域绑定在一起了
  • setOrientation(int orientation) 将滚动条的方向设置为 VERTICAL 或 HORIZONTAL。

  • setMaximum(int maximum) 设置模型的 maximum 属性。

  • setMinimum(int minimum) 设置模型的 minimum 属性。

  • setValue(int value) 设置当前的值

  • setUnitIncrement(int unitIncrement) 设置 unitIncrement 属性,设置拖曳滚动轴时,滚动轴刻度一次的变化量。

  • setBlockIncrement(int blockIncrement) 设置 blockIncrement 属性, 设置当鼠标在滚动轴列上按一下是,滚动轴一次所跳的区块大小

  • setVisibleAmount(int extent); 设置extent值

JTextPanel

可以用以图形方式表示的属性来标记的文本组件,相当于处理文本的工具word文档一样,可以在其中为文本设置不同的属性

构造函数

  • JTextPane()创建一个新的 JTextPane。

  • JTextPane(StyledDocument doc) 创建具有指定文档模型的新 JTextPane。

常用方法

  • setCharacterAttributes(AttributeSet attr, boolean replace) 将给定属性应用于字符内容,这个是将样式应用到文本的函数

  • getStyledDocument() 获取与编辑器关联的模型。

实例

package demo;import javax.swing.*;import javax.swing.text.*;import java.awt.*;/** * Created by chenjiabing on 17-5-22. */public class TextPane extends JFrame {public TextPane() {super();this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setBounds(100, 100, 400, 500);

        JTextPane textPane = new JTextPane();//        textPane.setBackground(Color.black);//      textPane.setFont(new Font("微软雅黑",Font.ITALIC,20));JScrollPane scrollPane = new JScrollPane(textPane);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

        SimpleAttributeSet simpleAttributeSet = new SimpleAttributeSet();  //创建一个属性StyleConstants.setForeground(simpleAttributeSet, Color.GREEN);    //设置前景色StyleConstants.setBold(simpleAttributeSet, true);    //设置加粗StyleConstants.setUnderline(simpleAttributeSet, true);    //设置下划线StyleConstants.setFontFamily(simpleAttributeSet, "微软雅黑");   //设置字体StyleConstants.setFontSize(simpleAttributeSet, 20);   //设置字体大小StyleConstants.setItalic(simpleAttributeSet, true);   //设置倾斜StyleConstants.setStrikeThrough(simpleAttributeSet, true);   //设置删除线StyleConstants.setFirstLineIndent(simpleAttributeSet, 2.5f);    //设置首行缩进StyleConstants.setLineSpacing(simpleAttributeSet, 20);
        StyleConstants.setRightIndent(simpleAttributeSet, 2.4f);


        textPane.setCharacterAttributes(simpleAttributeSet, true);//        Document doc=textPane.getStyledDocument();   //获得关联的文本//        try {//            doc.insertString(doc.getLength(), "陈加兵", simpleAttributeSet);   //向文本中插入字符串//        }catch (BadLocationException e)//        {//            e.printStackTrace();//        }this.getContentPane().add(scrollPane, BorderLayout.CENTER);
    }public static void main(String[] args) {
        TextPane text = new TextPane();
        text.setVisible(true);


    }


}

The above is the detailed content of Java value Swing series control one. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn