java-swing 给文本区jtextarea怎么加个滚动条?
//packagecn.edu.jnu.www;importjavax.swing.*;importjavax.swing.event.*;importjava.awt....
//package cn.edu.jnu.www;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
public class first extends JFrame{
private JLabel jLabel;
private JTextField jTextField;
private JButton jButton,jButton1,jButton2;
private JTextArea jtextarea;
private JScrollPane sp;
public first()
{
super();
this.setSize(700, 500);
this.getContentPane().setLayout(null);
this.add(getJLabel(), null);
this.add(getJTextField(), null);
this.add(getJButtonok(), null);
this.add(getJButtoncancel(),null);
this.add(getJButtonzero(),null);
this.setBackground(Color.red);
this.add(getJTextArea(), null);
this.setTitle("calculator");
}
private JTextArea getJTextArea(){
if(jtextarea==null){
jtextarea=new JTextArea();
jtextarea.setBounds(5, 45, 650, 400);
}
jtextarea.setLineWrap(true);
//sp=new JScrollPane(jtextarea);
//sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
return jtextarea;
}
private JLabel getJLabel() {
if(jLabel == null) {
jLabel = new javax.swing.JLabel();
jLabel.setBounds(10, 10, 150, 30);
jLabel.setText("请输入需要输出的位数:");
}
return jLabel;
}
private JTextField getJTextField() {
if(jTextField == null) {
jTextField = new javax.swing.JTextField();
jTextField.setBounds(150, 10, 160, 30);
}
return jTextField;
}
private JButton getJButtonok() {
if(jButton == null) {
jButton = new javax.swing.JButton();
jButton.setBounds(400, 10, 100,30);
jButton.setText("begin");
}
return jButton;
}
private JButton getJButtoncancel() {
if(jButton1 == null) {
jButton1 = new JButton();
jButton1.setBounds(530, 10, 100, 30);
jButton1.setText("stop");
}
return jButton1;
}
private JButton getJButtonzero() {
if(jButton2 == null) {
jButton2 = new JButton();
jButton2.setBounds(310, 10, 80, 30);
jButton2.setText("清零");
}
return jButton2;
}
public static void main(String[] args)
{
first w = new first();
w.setVisible(true);
}
} 展开
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
public class first extends JFrame{
private JLabel jLabel;
private JTextField jTextField;
private JButton jButton,jButton1,jButton2;
private JTextArea jtextarea;
private JScrollPane sp;
public first()
{
super();
this.setSize(700, 500);
this.getContentPane().setLayout(null);
this.add(getJLabel(), null);
this.add(getJTextField(), null);
this.add(getJButtonok(), null);
this.add(getJButtoncancel(),null);
this.add(getJButtonzero(),null);
this.setBackground(Color.red);
this.add(getJTextArea(), null);
this.setTitle("calculator");
}
private JTextArea getJTextArea(){
if(jtextarea==null){
jtextarea=new JTextArea();
jtextarea.setBounds(5, 45, 650, 400);
}
jtextarea.setLineWrap(true);
//sp=new JScrollPane(jtextarea);
//sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
return jtextarea;
}
private JLabel getJLabel() {
if(jLabel == null) {
jLabel = new javax.swing.JLabel();
jLabel.setBounds(10, 10, 150, 30);
jLabel.setText("请输入需要输出的位数:");
}
return jLabel;
}
private JTextField getJTextField() {
if(jTextField == null) {
jTextField = new javax.swing.JTextField();
jTextField.setBounds(150, 10, 160, 30);
}
return jTextField;
}
private JButton getJButtonok() {
if(jButton == null) {
jButton = new javax.swing.JButton();
jButton.setBounds(400, 10, 100,30);
jButton.setText("begin");
}
return jButton;
}
private JButton getJButtoncancel() {
if(jButton1 == null) {
jButton1 = new JButton();
jButton1.setBounds(530, 10, 100, 30);
jButton1.setText("stop");
}
return jButton1;
}
private JButton getJButtonzero() {
if(jButton2 == null) {
jButton2 = new JButton();
jButton2.setBounds(310, 10, 80, 30);
jButton2.setText("清零");
}
return jButton2;
}
public static void main(String[] args)
{
first w = new first();
w.setVisible(true);
}
} 展开
1个回答
展开全部
把你的getJTextArea()函数改一下就行了。
完整的程序如下:(注意getJTextArea()函数)
//package cn.edu.jnu.www;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
public class first extends JFrame{
private JLabel jLabel;
private JTextField jTextField;
private JButton jButton,jButton1,jButton2;
private JTextArea jtextarea;
private JScrollPane sp;
public first()
{
super();
this.setSize(700, 500);
this.getContentPane().setLayout(null);
this.add(getJLabel(), null);
this.add(getJTextField(), null);
this.add(getJButtonok(), null);
this.add(getJButtoncancel(),null);
this.add(getJButtonzero(),null);
this.setBackground(Color.red);
this.add(getJTextArea(), null);
this.setTitle("calculator");
}
private JScrollPane getJTextArea(){
if(jtextarea==null){
jtextarea=new JTextArea();
//jtextarea.setBounds(5, 45, 650, 400);
}
//jtextarea.setLineWrap(true);
sp=new JScrollPane(jtextarea);
sp.setBounds(5, 45, 650, 400);
//sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
return sp;
}
private JLabel getJLabel() {
if(jLabel == null) {
jLabel = new javax.swing.JLabel();
jLabel.setBounds(10, 10, 150, 30);
jLabel.setText("请输入需要输出的位数:");
}
return jLabel;
}
private JTextField getJTextField() {
if(jTextField == null) {
jTextField = new javax.swing.JTextField();
jTextField.setBounds(150, 10, 160, 30);
}
return jTextField;
}
private JButton getJButtonok() {
if(jButton == null) {
jButton = new javax.swing.JButton();
jButton.setBounds(400, 10, 100,30);
jButton.setText("begin");
}
return jButton;
}
private JButton getJButtoncancel() {
if(jButton1 == null) {
jButton1 = new JButton();
jButton1.setBounds(530, 10, 100, 30);
jButton1.setText("stop");
}
return jButton1;
}
private JButton getJButtonzero() {
if(jButton2 == null) {
jButton2 = new JButton();
jButton2.setBounds(310, 10, 80, 30);
jButton2.setText("清零");
}
return jButton2;
}
public static void main(String[] args)
{
first w = new first();
w.setVisible(true);
}
}
更多追问追答
追问
谢谢,滚动条有了,不过为什么要这样啊?怎么不是返回文本区呢?还有怎么用sp去设定文本区的位置和长宽?
追答
因为JTextArea放入到了JScrollPane中,而你设定了空布局,所以要对sp设定位置和长宽。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询