写了一个满足你的图片的要求,你看看,有什么问题百度hi我:
公司主营业务:成都做网站、成都网站建设、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。成都创新互联公司是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。成都创新互联公司推出沭阳免费做网站回馈大家。
import java.awt.BorderLayout;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.TitledBorder;
public class TestDemo extends JFrame{
private static final long serialVersionUID = -5355432125621015300L;
private Option[] options = new Option[4];
public TestDemo () {
init();
}
private void init(){
setTitle("我的文本域");
setSize(300,200);
setContentPane(createContentPane());
}
private JPanel createContentPane(){
JPanel pane = new JPanel(new BorderLayout());
pane.add(BorderLayout.NORTH,createQuestionPane());
pane.add(BorderLayout.SOUTH,createOptionsPane());
return pane;
}
private JPanel createOptionsPane(){
JPanel pane = new JPanel();
Option a = new Option(0,"A");
Option b = new Option(1,"B");
Option c = new Option(2,"C");
Option d = new Option(3,"D");
options[0]=a;
options[1]=b;
options[2]=c;
options[3]=d;
pane.add(a);
pane.add(b);
pane.add(c);
pane.add(d);
return pane;
}
private JScrollPane createQuestionPane() {
JScrollPane pane = new JScrollPane();
pane.setBorder(new TitledBorder("题目"));
JTextArea questionArea = new JTextArea();
questionArea.setRows(6);
questionArea.setText("问题\nA.\nB.\nC.\nD.");
questionArea.setLineWrap(true);
questionArea.setEditable(false);
pane.getViewport().add(questionArea);
return pane;
}
class Option extends JCheckBox{
private static final long serialVersionUID = 5569918347028393191L;
int val;
public Option(int val, String txt) {
super(txt);
this.val = val;
}
}
public static void main(String[]args){
TestDemo td=new TestDemo();
td.setVisible(true);
}
}
你加入滚动条对象这个操作应该在容器加入文本框之前,代码给你修改如下:
import javax.swing.*;
public class wenbenyu extends JFrame
{JPanel rongqi=new JPanel();
JTextArea wby=new JTextArea("文本域",3,3);
JLabel biaoqian=new JLabel("标签");
public wenbenyu(){
//int rows=wby.getRows();
//wby.insert("向文本域输入信息:"+rows);
JScrollPane gundong=new JScrollPane(wby,
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
rongqi.add(wby);
rongqi.add(biaoqian);
rongqi.add(biaoqian);
this.add(rongqi);
this.setSize(300,300);
this.show();
}
public static void main(String[] args)
{
wenbenyu aaaa=new wenbenyu();
}
}
我给你写了一个简单的,实现了说的功能,并且还多加了一个按钮的清空事件,直接建一个类,把下面代码复制进去就行了,注意类名一致。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class WindowTest extends JFrame implements ActionListener, KeyListener
{
private static final long serialVersionUID = 1L;
/**
* 主方法
*/
public static void main(String[] args)
{
WindowTest win = new WindowTest();
}
/**
* 下面是具体实现
*/
JTextField text;
JButton button;
JEditorPane textArea;
public WindowTest()
{
super("测试窗体");
text = new JTextField(15);
text.addKeyListener(this);
JPanel p1 = new JPanel();
p1.add(new JLabel("输入字符:"));
p1.add(text);
button = new JButton("清除");
button.addActionListener(this);
p1.add(button);
p1.setBounds(5, 5, 220, 100);
textArea = new JEditorPane();
textArea.setBounds(1, 1, 216, 200);
JPanel p2 = new JPanel();
p2.add(new JLabel("显示字符:"));
p2.add(textArea);
p2.setBounds(5, 115, 340, 220);
JPanel p3 = new JPanel();
p3.add(p1);
p3.add(p2);
add(p3);
setBounds(160, 60, 400, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void keyPressed(KeyEvent e)
{
}
@Override
public void keyReleased(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_ENTER)
{
textArea.setText("");
}
else
{
String str = text.getText();
textArea.setText(str);
}
}
@Override
public void keyTyped(KeyEvent e)
{
}
@Override
public void actionPerformed(ActionEvent e)
{
text.setText("");
textArea.setText("");
}
}