java 在JTextArea中显示 滚动条
我的代码为什么不显示垂直滚动条是那里错了么如果有错求高手修改importjavax.swing.*;importjava.awt.*;importjava.awt.eve...
我的代码为什么不显示垂直滚动条 是那里错了么 如果有错 求高手修改
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Cai {
private JFrame _frame;
private JPanel _panelMain;
private JPanel _panelCai;
private JPanel _panelArea;
private JButton _bntCai;
private JTextArea _txtArea;
private JLabel _lbl;
private JTextField _txt;
private JScrollPane _jsp;
public Cai(){
_frame=new JFrame();
_panelMain=new JPanel(new BorderLayout());
_panelCai=new JPanel();
_lbl=new JLabel("请猜1-10之间的数字:");
_panelArea=new JPanel();
_txt=new JTextField(3);
_txtArea=new JTextArea();
_bntCai=new JButton("猜");
_jsp=new JScrollPane(_txtArea);
_panelArea.add(_txtArea);
_panelCai.add(_lbl);
_panelCai.add(_txt);
_panelCai.add(_bntCai);
_panelMain.add(_panelCai,BorderLayout.NORTH);
_panelMain.add(_txtArea,BorderLayout.CENTER);
//_panelMain.add(_jsp,BorderLayout.EAST);
_jsp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
_frame.setContentPane(_panelMain);
_frame.setBounds(300,300,300,300);
_frame.setVisible(true);
}
}
public static void main(String[] args) {
Cai a =new Cai();
} 展开
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Cai {
private JFrame _frame;
private JPanel _panelMain;
private JPanel _panelCai;
private JPanel _panelArea;
private JButton _bntCai;
private JTextArea _txtArea;
private JLabel _lbl;
private JTextField _txt;
private JScrollPane _jsp;
public Cai(){
_frame=new JFrame();
_panelMain=new JPanel(new BorderLayout());
_panelCai=new JPanel();
_lbl=new JLabel("请猜1-10之间的数字:");
_panelArea=new JPanel();
_txt=new JTextField(3);
_txtArea=new JTextArea();
_bntCai=new JButton("猜");
_jsp=new JScrollPane(_txtArea);
_panelArea.add(_txtArea);
_panelCai.add(_lbl);
_panelCai.add(_txt);
_panelCai.add(_bntCai);
_panelMain.add(_panelCai,BorderLayout.NORTH);
_panelMain.add(_txtArea,BorderLayout.CENTER);
//_panelMain.add(_jsp,BorderLayout.EAST);
_jsp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
_frame.setContentPane(_panelMain);
_frame.setBounds(300,300,300,300);
_frame.setVisible(true);
}
}
public static void main(String[] args) {
Cai a =new Cai();
} 展开
1个回答
展开全部
你的代码里面并没有把_jsp添加到_panelMain里面,所以没有显示。而且还有些其他的问题,看下面的代码:
import javax.swing.*;
import java.awt.*;
public class Cai {
private JFrame _frame;
private JPanel _panelMain;
private JPanel _panelCai;
private JPanel _panelArea;
private JButton _bntCai;
private JTextArea _txtArea;
private JLabel _lbl;
private JTextField _txt;
private JScrollPane _jsp;
public Cai() {
_frame = new JFrame();
_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//点击右上角的关闭按钮时退出程序运行
_panelMain = new JPanel(new BorderLayout());
_panelCai = new JPanel();
_lbl = new JLabel("请猜1-10之间的数字:");
_txt = new JTextField(3);
_bntCai = new JButton("猜");
_panelArea = new JPanel(new BorderLayout());
//设置BorderLayout,让_txtArea自动充满
_txtArea = new JTextArea();
_txtArea.setLineWrap(true);
//_txtArea自动换行
_jsp = new JScrollPane(_panelArea);
//_jsp 以_panelArea为Viewport
_jsp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
_panelArea.add(_txtArea);
_panelCai.add(_lbl);
_panelCai.add(_txt);
_panelCai.add(_bntCai);
_panelMain.add(_panelCai, BorderLayout.NORTH);
_panelMain.add(_jsp, BorderLayout.CENTER);
//添加_jsp在 BorderLayout.CENTER
_frame.setContentPane(_panelMain);
_frame.setBounds(300, 300, 300, 300);
_frame.setVisible(true);
}
public static void main(String[] args) {
Cai a = new Cai();
}
}
import javax.swing.*;
import java.awt.*;
public class Cai {
private JFrame _frame;
private JPanel _panelMain;
private JPanel _panelCai;
private JPanel _panelArea;
private JButton _bntCai;
private JTextArea _txtArea;
private JLabel _lbl;
private JTextField _txt;
private JScrollPane _jsp;
public Cai() {
_frame = new JFrame();
_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//点击右上角的关闭按钮时退出程序运行
_panelMain = new JPanel(new BorderLayout());
_panelCai = new JPanel();
_lbl = new JLabel("请猜1-10之间的数字:");
_txt = new JTextField(3);
_bntCai = new JButton("猜");
_panelArea = new JPanel(new BorderLayout());
//设置BorderLayout,让_txtArea自动充满
_txtArea = new JTextArea();
_txtArea.setLineWrap(true);
//_txtArea自动换行
_jsp = new JScrollPane(_panelArea);
//_jsp 以_panelArea为Viewport
_jsp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
_panelArea.add(_txtArea);
_panelCai.add(_lbl);
_panelCai.add(_txt);
_panelCai.add(_bntCai);
_panelMain.add(_panelCai, BorderLayout.NORTH);
_panelMain.add(_jsp, BorderLayout.CENTER);
//添加_jsp在 BorderLayout.CENTER
_frame.setContentPane(_panelMain);
_frame.setBounds(300, 300, 300, 300);
_frame.setVisible(true);
}
public static void main(String[] args) {
Cai a = new Cai();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询