簡體   English   中英

Java GUI JScrollBar如何設置長度?

[英]Java GUI JScrollBar How to set the length?

我編寫了以下GUI,但我想增加右側滾動條的長度。

任何想法如何做到這一點?

// test class that implements the gui stuff.
public class testing
{

    //variables
    private JFrame f = new JFrame("GUI TEST");

    private JPanel p = new JPanel();
    private JPanel p2 = new JPanel();
    private JPanel p3 = new JPanel();
    private JPanel p4 = new JPanel();
    private JPanel p5 = new JPanel();
    private JPanel p6 = new JPanel();
    private JPanel p7 = new JPanel();
    private JPanel p8 = new JPanel();
    private JPanel p9 = new JPanel();
    private JPanel p10 = new JPanel();
    private JPanel p11 = new JPanel();

    private JButton b1 = new JButton("Button");

    private JTextField tf1 = new JTextField("                                           ");
    private JTextField tf2 = new JTextField("                                           ");
    private JTextField tf3 = new JTextField("                                           ");

    private JTextArea ta1 = new JTextArea(10,45);

    private JLabel label1 = new JLabel("Label 1");
    private JLabel label2 = new JLabel("Label 2");
    private JLabel label3 = new JLabel("Label 3");
    private JLabel label4 = new JLabel("Label 4");

    private JScrollBar sb1 = new JScrollBar();

    //class constructor
    public testing()
    {
        gui();
    }

    public void gui()
    {
        //change length of scroll bar
        f.setVisible(true);
        f.setSize(600,300);
        p.add(label1);
        p.add(tf1);
        p2.add(label2);
        p2.add(tf2);
        p3.add(label3);
        p3.add(tf3);
        p4.add(sb1);
        p4.add(label4);
        p5.add(ta1);
        p6.add(b1);
        p4.setBackground(Color.GRAY);
        p9.setBackground(Color.GRAY);
        p10.setBackground(Color.GRAY);
        p11.setBackground(Color.GRAY);
        p9.add(p);
        p9.add(p2);
        p9.add(p3);
        p10.add(p5);
        p11.add(p6);
        //adds panels to frames
        f.add(p4, BorderLayout.EAST);
        f.add(p9, BorderLayout.NORTH);
        f.add(p10, BorderLayout.CENTER);
        f.add(p11, BorderLayout.PAGE_END);
    }

    public static void main(String[] args)
    {
        new testing();
    }

Ordinarilly,您只需將JTextArea添加到JScrollPane ,它可以為您處理調整大小的行為。

f.add(new JScrollPane(ta1), BorderLayout.CENTER);

出於演示目的,您可以重寫JScrollBargetPreferredSize()方法以查看效果。

private JScrollBar sb1 = new JScrollBar(){

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(
            super.getPreferredSize().width, ta1.getPreferredSize().height);
    }
};

此外,

  • 只能事件分發線程上構造和操作Swing GUI對象。

  • 使用適當的構造函數來建立所需的文本組件初始大小。

  • 使用適當的布局以獲得所需的調整大小行為。

圖片

經測試:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Testing {
    //variables
    private JFrame f = new JFrame("GUI TEST");

    private JPanel p = new JPanel();
    private JPanel p2 = new JPanel();
    private JPanel p3 = new JPanel();
    private JPanel p4 = new JPanel();
    private JPanel p5 = new JPanel();
    private JPanel p6 = new JPanel();
    private JPanel p9 = new JPanel();
    private JPanel p10 = new JPanel();
    private JPanel p11 = new JPanel();

    private JButton b1 = new JButton("Button");

    private JTextField tf1 = new JTextField(12);
    private JTextField tf2 = new JTextField(12);
    private JTextField tf3 = new JTextField(12);

    private JTextArea ta1 = new JTextArea(10, 45);

    private JLabel label1 = new JLabel("Label 1");
    private JLabel label2 = new JLabel("Label 2");
    private JLabel label3 = new JLabel("Label 3");
    private JLabel label4 = new JLabel("Label 4");

    private JScrollBar sb1 = new JScrollBar(){

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(super.getPreferredSize().width, ta1.getPreferredSize().height);
        }
    };

    //class constructor
    public Testing() {
        gui();
    }

    public void gui() {
        p.add(label1);
        p.add(tf1);
        p2.add(label2);
        p2.add(tf2);
        p3.add(label3);
        p3.add(tf3);
        p4.add(sb1);
        p4.add(label4);
        p5.add(ta1);
        p6.add(b1);
        p4.setBackground(Color.GRAY);
        p9.setBackground(Color.GRAY);
        p10.setBackground(Color.GRAY);
        p11.setBackground(Color.GRAY);
        p9.add(p);
        p9.add(p2);
        p9.add(p3);
        p10.add(p5);
        p11.add(p6);
        //adds panels to frames
        f.add(p4, BorderLayout.EAST);
        f.add(p9, BorderLayout.NORTH);
        f.add(p10, BorderLayout.CENTER);
        f.add(p11, BorderLayout.PAGE_END);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.pack();
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            new Testing();
        });
    }
}

暫無
暫無

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

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