刚学JAVA图形界面设计,想问下我这个程序怎么在下面文本框中添加滚动条
importjava.awt.FlowLayout;importjava.awt.event.ActionEvent;importjava.awt.event.Actio...
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
public class SwingTest extends JFrame {
private JButton b1 = new JButton("显示一把");
private JTextField txt = new JTextField(10);
private JTextArea area = new JTextArea(2, 20);
JScrollPane text2=new JScrollPane(area);
private JPanel jp = new JPanel();
class SwingTestListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(e.getSource()==b1){
area.append(txt.getText()+"\n");
txt.setText("");
}
}
}
public SwingTest(String frameName) {
super(frameName);
b1.addActionListener(new SwingTestListener());
txt.addActionListener(new SwingTestListener());
setLayout(new FlowLayout());
add(txt);
add(b1);
jp.setBorder(new TitledBorder("TextArea"));
jp.add(area);
jp.add(text2);
add(jp);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300 , 150);
setLocation(500, 500);
setVisible(true);
}
public static void main(String[] args) {
new SwingTest("SwingTest");
}
}
求大神帮忙添加下,可以的话,麻烦解释下,跪谢了~! 展开
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
public class SwingTest extends JFrame {
private JButton b1 = new JButton("显示一把");
private JTextField txt = new JTextField(10);
private JTextArea area = new JTextArea(2, 20);
JScrollPane text2=new JScrollPane(area);
private JPanel jp = new JPanel();
class SwingTestListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(e.getSource()==b1){
area.append(txt.getText()+"\n");
txt.setText("");
}
}
}
public SwingTest(String frameName) {
super(frameName);
b1.addActionListener(new SwingTestListener());
txt.addActionListener(new SwingTestListener());
setLayout(new FlowLayout());
add(txt);
add(b1);
jp.setBorder(new TitledBorder("TextArea"));
jp.add(area);
jp.add(text2);
add(jp);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300 , 150);
setLocation(500, 500);
setVisible(true);
}
public static void main(String[] args) {
new SwingTest("SwingTest");
}
}
求大神帮忙添加下,可以的话,麻烦解释下,跪谢了~! 展开
2个回答
展开全部
public SwingTest(String frameName) {
area.setLineWrap(true);//这句意思是自动换行
super(frameName);
b1.addActionListener(new SwingTestListener());
txt.addActionListener(new SwingTestListener());
setLayout(new FlowLayout());
add(txt);
add(b1);
jp.setBorder(new TitledBorder("TextArea"));
jp.add(text2);//这里删掉add area,因为你的文本区域已经加到滚动面板里了,不需要添加2次
add(jp);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300 , 150);
setLocation(500, 500);
setVisible(true);
}
area.setLineWrap(true);//这句意思是自动换行
super(frameName);
b1.addActionListener(new SwingTestListener());
txt.addActionListener(new SwingTestListener());
setLayout(new FlowLayout());
add(txt);
add(b1);
jp.setBorder(new TitledBorder("TextArea"));
jp.add(text2);//这里删掉add area,因为你的文本区域已经加到滚动面板里了,不需要添加2次
add(jp);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300 , 150);
setLocation(500, 500);
setVisible(true);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询