首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JScrollBar不工作

JScrollBar不工作
EN

Stack Overflow用户
提问于 2016-05-12 02:43:55
回答 3查看 49关注 0票数 0

我正在尝试向JTextArea添加一个JScrollPane,但是当我添加它时,JPane不再显示我的textarea。如果没有JScrollPane,它会显示它,但是我不能显示从文件中检索到的所有信息。

这是我想用JScrollPane包装的一个textArea的代码。

代码语言:javascript
复制
   public GUI_CWK()
{
    //frame details
    setSize(450,400);

    setLayout(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setTitle("H.o. | Location Database");
    setExtendedState(JFrame.MAXIMIZED_BOTH); 
    getContentPane().setBackground(Color.YELLOW);
    // Useing Lable to set a title for multiple buttons
    lbl2 = new JLabel();
    lbl2.setSize(420,85);
    lbl2.setLocation(15,0);
    lbl2.setEnabled(false);
    lbl2.setFont(new Font ("arial",5,17));
    lbl2.setText("Options to Manipulate with Data");
    add(lbl2);

    // Adding buttons to JFrame
    btn1 = new JButton("Add new entry");
    btn1.setSize(120,20);
    btn1.setLocation(7,80);
    btn1.addActionListener(this);
    add(btn1);

    btn2 = new JButton("Search");
    btn2.setSize(120,20);
    btn2.setLocation(7,110);
    btn2.addActionListener(this);
    add(btn2);

    btn3 = new JButton("Update Entry");
    btn3.setSize(120,20);
    btn3.setLocation(150,110);
    btn3.addActionListener(this);
    add(btn3);

    btn4 = new JButton("Print All");
    btn4.setSize(120,20);
    btn4.setLocation(150,80);
    btn4.addActionListener(this);
    add(btn4);

    btn5 = new JButton("Print Arrays");
    btn5.setSize(120,20);
    btn5.setLocation(7,140);
    btn5.addActionListener(this);
    add(btn5);

    btn6 = new JButton("Delete Entry");
    btn6.setSize(120,20);
    btn6.setLocation(300,80);
    btn6.addActionListener(this);
    add(btn6);

    btn7 = new JButton("Delete ALL");
    btn7.setSize(120,20);
    btn7.setLocation(300,110);
    btn7.addActionListener(this);
    add(btn7);

    lbl1 = new JLabel();
    lbl1.setSize(420,85);
    lbl1.setLocation(15,190);
    lbl1.setEnabled(false);
    lbl1.setFont(new Font ("arial",5,17));
    lbl1.setText("Extra Options");
    add(lbl1);

    btn8 = new JButton("Sort Data");
    btn8.setSize(160,20);
    btn8.setLocation(7,265);
    btn8.addActionListener(this);
    add(btn8);

    btn9 = new JButton("Open Text file");
    btn9.setSize(160,20);
    btn9.setLocation(7,300);
    btn9.addActionListener(this);
    add(btn9);

    btn11 = new JButton("Retrieve from text file");
    btn11.setSize(160,20);
    btn11.setLocation(240,265);
    btn11.addActionListener(this);
    add(btn11);

    btn12 = new JButton("Exit Program");
    btn12.setSize(160,20);
    btn12.setLocation(240,300);
    btn12.addActionListener(this);
    add(btn12);

    lbl3 = new JLabel();
    lbl3.setSize(420,85);
    lbl3.setLocation(15,340);
    lbl3.setEnabled(false);
    lbl3.setFont(new Font ("arial",5,17));
    lbl3.setText("Terminal Window");
    add(lbl3);
    // Adding textArea to display each user entered value
    textArea2 = new JTextArea(5, 20);
    textArea2.setEditable(false);
    textArea2.setSize(440,300);
    textArea2.setLocation(7,400);
    textArea2.setDisabledTextColor(Color.black);
    textArea2.setFont(new Font("arial",5,14));
    sp2 = new JScrollPane(textArea2);
    sp2.setVerticalScrollBarPolicy ( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS );
    add(sp2);

    lbl4 = new JLabel();
    lbl4.setSize(420,40);
    lbl4.setLocation(570,10);
    lbl4.setEnabled(false);
    lbl4.setFont(new Font ("arial",5,17));
    lbl4.setText("Data Display");
    add(lbl4);

    textArea = new JTextArea(55, 50);
    textArea.setEditable(false);
    textArea.setSize(800,640);
    textArea.setLocation(560,60);
    textArea.setFont(new Font("arial",5,19));
    textArea.setDisabledTextColor(Color.black);
    sp = new JScrollPane(textArea);
    sp.setVerticalScrollBarPolicy ( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS );
    add(sp);

    setVisible(true);
}
EN

回答 3

Stack Overflow用户

发布于 2016-05-12 03:47:58

我认为这应该可以解决你的问题。

您会注意到,我删除了addActionListener()方法之间的this,因为您必须指定希望ActionListener做什么。

StartingPoint (main)类:

代码语言:javascript
复制
public class StartingPoint{
public static void main(String[] args){

    GUI_CWK gui_cwk = new GUI_CWK(); //Creates a new instance of "GUI_CWK".
    gui_cwk.setVisible(true); //You set the gui_cwk to visible.

}
}

GUI_CWK类:

代码语言:javascript
复制
import java.awt.Color;
import java.awt.Font;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;

public class GUI_CWK extends JFrame{ 
private static final long serialVersionUID = 1L;

public GUI_CWK() {

    // frame details
    this.setSize(450, 400);

    JPanel panel = new JPanel();

    this.setLayout(null); /* Each time that you want to refer to the classyou are in, you say "this" */
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setTitle("H.o. | Location Database");
    this.setExtendedState(JFrame.MAXIMIZED_BOTH);
    this.getContentPane().setBackground(Color.YELLOW);

    // Using labels to set a title for multiple buttons
    JLabel lbl2 = new JLabel(); /*Remember that you have to create the instances of an object by naming the class before the name of the instance */
    lbl2.setSize(420, 85);
    lbl2.setLocation(15, 0);
    lbl2.setEnabled(false);
    lbl2.setFont(new Font("arial", 5, 17));
    lbl2.setText("Options to Manipulate with Data");
    panel.add(lbl2);

    // Adding buttons to JFrame
    JButton btn1 = new JButton("Add new entry");
    btn1.setSize(120, 20);
    btn1.setLocation(7, 80);
    btn1.addActionListener(/* Insert ActionListener */);
    panel.add(btn1); //The button is added to the panel, so then the last one can be added to the JFrame at the end.

    JButton btn2 = new JButton("Search");
    btn2.setSize(120, 20);
    btn2.setLocation(7, 110);
    btn2.addActionListener(/* Insert ActionListener */);
    panel.add(btn2);

    JButton btn3 = new JButton("Update Entry");
    btn3.setSize(120, 20);
    btn3.setLocation(150, 110);
    btn3.addActionListener(/* Insert ActionListener */);
    panel.add(btn3);

    JButton btn4 = new JButton("Print All");
    btn4.setSize(120, 20);
    btn4.setLocation(150, 80);
    btn4.addActionListener(/* Insert ActionListener */);
    panel.add(btn4);

    JButton btn5 = new JButton("Print Arrays");
    btn5.setSize(120, 20);
    btn5.setLocation(7, 140);
    btn5.addActionListener(/* Insert ActionListener */);
    panel.add(btn5);

    JButton btn6 = new JButton("Delete Entry");
    btn6.setSize(120, 20);
    btn6.setLocation(300, 80);
    btn6.addActionListener(/* Insert ActionListener */);
    panel.add(btn6);

    JButton btn7 = new JButton("Delete ALL");
    btn7.setSize(120, 20);
    btn7.setLocation(300, 110);
    btn7.addActionListener(/* Insert ActionListener */);
    panel.add(btn7);

    JLabel lbl1 = new JLabel();
    lbl1.setSize(420, 85);
    lbl1.setLocation(15, 190);
    lbl1.setEnabled(false);
    lbl1.setFont(new Font("arial", 5, 17));
    lbl1.setText("Extra Options");
    panel.add(lbl1);

    JButton btn8 = new JButton("Sort Data");
    btn8.setSize(160, 20);
    btn8.setLocation(7, 265);
    btn8.addActionListener(/* Insert ActionListener */);
    panel.add(btn8);

    JButton btn9 = new JButton("Open Text file");
    btn9.setSize(160, 20);
    btn9.setLocation(7, 300);
    btn9.addActionListener(/* Insert ActionListener */);
    panel.add(btn9);

    JButton btn11 = new JButton("Retrieve from text file");
    btn11.setSize(160, 20);
    btn11.setLocation(240, 265);
    btn11.addActionListener(/* Insert ActionListener */);
    panel.add(btn11);

    JButton btn12 = new JButton("Exit Program");
    btn12.setSize(160, 20);
    btn12.setLocation(240, 300);
    btn12.addActionListener(/* Insert ActionListener */);
    panel.add(btn12);

    JLabel lbl3 = new JLabel();
    lbl3.setSize(420, 85);
    lbl3.setLocation(15, 340);
    lbl3.setEnabled(false);
    lbl3.setFont(new Font("arial", 5, 17));
    lbl3.setText("Terminal Window");
    panel.add(lbl3);

    // Adding textArea to display each user entered value
    JTextArea textArea2 = new JTextArea(5, 20);
    textArea2.setEditable(false);
    textArea2.setSize(440, 300);
    textArea2.setLocation(7, 400);
    textArea2.setDisabledTextColor(Color.black);
    textArea2.setFont(new Font("arial", 5, 14));

    JScrollPane sp2 = new JScrollPane(textArea2);
    sp2.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    textArea2.add(sp2); //Here you add the scrollPanel to the textArea,
                        //in the original code you were adding it to the frame
    panel.add(textArea2);

    JLabel lbl4 = new JLabel();
    lbl4.setSize(420, 40);
    lbl4.setLocation(570, 10);
    lbl4.setEnabled(false);
    lbl4.setFont(new Font("arial", 5, 17));
    lbl4.setText("Data Display");
    panel.add(lbl4);

    JTextArea textArea = new JTextArea(55, 50);
    textArea.setEditable(false);
    textArea.setSize(800, 640);
    textArea.setLocation(560, 60);
    textArea.setFont(new Font("arial", 5, 19));
    textArea.setDisabledTextColor(Color.black);
    JScrollPane sp1 = new JScrollPane(textArea);
    sp1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    textArea.setVisible(true);
    textArea.add(sp1);
    panel.add(textArea);

    this.add(panel); //Everything which was added to the panel now is added to the frame.
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
票数 0
EN

Stack Overflow用户

发布于 2016-05-12 04:29:52

下面是一个使用滚动窗格的示例。

代码语言:javascript
复制
import javax.swing.*;
import java.awt.*;
public class ScrollPaneExample{
    public void buildGui(){
        JFrame frame = new JFrame("button and scrollpane");

        JButton button = new JButton("example");
        JTextArea area = new JTextArea(20, 20);
        //this is where we will place our components.
        //It has a flow layout by default.
        JPanel content = new JPanel(); 

        JScrollPane sp = new JScrollPane(area);
        content.add(button);
        content.add(sp);

        //this line limits the size of the scroll pane.
        sp.setPreferredSize(new Dimension(400, 60));

        //The frame will use our panel instead of the default one.
        frame.setContentPane(content);

        //sizes the frame to the content area.
        frame.pack();        

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
    public static void main(String[] args){
        EventQueue.invokeLater(()->{new ScrollPaneExample().buildGui();});
    }

}

主要区别在于我设置了我的内容窗格,以便我知道它的布局管理器。你似乎手动设置了所有的尺寸,我很惊讶它还能工作。你应该选择一个好的布局管理器。pick one你想做什么就做什么。

还要注意的是,这是一个可以编译的最小示例。

票数 0
EN

Stack Overflow用户

发布于 2016-05-12 05:38:38

谢谢你们的帮助,我找到了答案。正如@matt告诉我的那样,我应该为JScrollPane而不是textArea添加大小和位置。所以我就这么做了,现在它对我来说很有效。

代码如下。

代码语言:javascript
复制
 textArea2 = new JTextArea(5,20);
    textArea2.setEditable(false);
    textArea2.setDisabledTextColor(Color.black);
    textArea2.setFont(new Font("arial",5,14));
    sp2 = new JScrollPane(textArea2);
    sp2.setSize(440,300);
    sp2.setLocation(7,400);
    sp2.setVerticalScrollBarPolicy ( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS );
    add(sp2);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37170934

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档