Java中JTextarea 与滚动条的问题 高手解答
importjava.io.*;importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;publiccla...
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Chat extends JFrame{
JTextArea area1,area2;
JLabel label;
JButton button1,button2;
Container container;
public Chat(){
super("");
setGUIComponent();
setSize(400,300);
setVisible(true);
}
public void setGUIComponent(){
container=getContentPane();
Font font=new Font("Serif",Font.BOLD+Font.ITALIC,12);
container.setLayout(null);
button1=new JButton("取消");
button1.setFont(font);
button1.setBackground(Color.yellow);
button1.setBounds(60,180,70,20);
button2=new JButton("发送");
button2.setFont(font);
button2.setBackground(Color.yellow);
button2.setBounds(140,180,70,20);
area1=new JTextArea(200,100);
area1.setEditable(false);
area2=new JTextArea(200,40);
area2.setEditable(true);
area1.setBounds(10,10,210,110);
area2.setBounds(10,130,210,40);
container.add(button1);//添加组件
container.add(button2);
JScrollPane A=new JScrollPane(area1);
//A.isWheelScrollingEnabled();
container.add(A);
container.add(area2);
button1.addActionListener(new ActionListener(){//为关闭按钮注册事件监听器
public void actionPerformed(ActionEvent e){
if(e.getSource()==button1)
area2.setText("");
}
});
button2.addActionListener(new ActionListener(){//为发送按钮注册事件监听器
public void actionPerformed(ActionEvent e){
if(e.getSource()==button2)
area1.append(area2.getText()+"\n");
area2.setText("");
}
});
area2.addKeyListener(new KeyListener(){//为输入文本区注册事件监听器
public void keyPressed(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){
if(e.getKeyChar()==KeyEvent.VK_ENTER)
{
area1.append(area2.getText()+"\n");
area2.setText("");
}
}
});
}
public static void main(String args[])
{
Chat c=new Chat();
c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
} 展开
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Chat extends JFrame{
JTextArea area1,area2;
JLabel label;
JButton button1,button2;
Container container;
public Chat(){
super("");
setGUIComponent();
setSize(400,300);
setVisible(true);
}
public void setGUIComponent(){
container=getContentPane();
Font font=new Font("Serif",Font.BOLD+Font.ITALIC,12);
container.setLayout(null);
button1=new JButton("取消");
button1.setFont(font);
button1.setBackground(Color.yellow);
button1.setBounds(60,180,70,20);
button2=new JButton("发送");
button2.setFont(font);
button2.setBackground(Color.yellow);
button2.setBounds(140,180,70,20);
area1=new JTextArea(200,100);
area1.setEditable(false);
area2=new JTextArea(200,40);
area2.setEditable(true);
area1.setBounds(10,10,210,110);
area2.setBounds(10,130,210,40);
container.add(button1);//添加组件
container.add(button2);
JScrollPane A=new JScrollPane(area1);
//A.isWheelScrollingEnabled();
container.add(A);
container.add(area2);
button1.addActionListener(new ActionListener(){//为关闭按钮注册事件监听器
public void actionPerformed(ActionEvent e){
if(e.getSource()==button1)
area2.setText("");
}
});
button2.addActionListener(new ActionListener(){//为发送按钮注册事件监听器
public void actionPerformed(ActionEvent e){
if(e.getSource()==button2)
area1.append(area2.getText()+"\n");
area2.setText("");
}
});
area2.addKeyListener(new KeyListener(){//为输入文本区注册事件监听器
public void keyPressed(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){
if(e.getKeyChar()==KeyEvent.VK_ENTER)
{
area1.append(area2.getText()+"\n");
area2.setText("");
}
}
});
}
public static void main(String args[])
{
Chat c=new Chat();
c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
} 展开
3个回答
展开全部
A.setBounds(10,10,210,110);
JScrollPane的大小和内部组件的大小是两回事儿,OK?
JScrollPane的大小和内部组件的大小是两回事儿,OK?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
多翻翻课本了,很基础的
追问
唉 我知道基础啊 你运行了没有 把area1加入滚动条后 area1就不显示了 为什么啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询