2013年10月31日星期四

Java multi -line text can not display scroll bars ? JTextArea and JScrollPane problem

package myproject;
import java.awt. *;
import java.awt.event. *;

import javax.swing. *;


public class test {

static final int WIDTH = 400;
static final int HEIGHT = 500;

public static void main (String args []) {
JFrame frame = new JFrame ("TCMESFD");
frame.setSize (WIDTH, HEIGHT);
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

/ / menu bar
JMenuBar menubar1 = new JMenuBar () ;/ / add menu bar assembly
frame.setJMenuBar (menubar1) ;/ / will be added to the top-level menu bar container
JMenu menu1 = new JMenu (" fault tree " ) ; / / set menu component
JMenu menu2 = new JMenu (" knowledge base" ) ;
JMenu menu3 = new JMenu (" diagnosis " ) ;
JMenu menu4 = new JMenu (" return to the main interface " ) ;
JMenu menu5 = new JMenu (" Help " ) ;
menubar1.add (menu1) ;/ / add components to the menu in the menu bar assembly
menubar1.add (menu2);
menubar1.add (menu3);
menubar1.add (menu4);
menubar1.add (menu5);
/ / title and buttons
JLabel title = new JLabel ("QWE");
JButton StartD = new JButton (" Start Diagnostics" ) ;
JButton StopD = new JButton (" Stop Diagnostics" ) ;
frame.add (title);
frame.add (StartD);
frame.add (StopD);
/ / failure cause and effect relationships
Container cont = frame.getContentPane ();
JRadioButton jrb1 = new JRadioButton (" one to one " ) ;
JRadioButton jrb2 = new JRadioButton (" a result of multi- fruit" ) ;
JRadioButton jrb3 = new JRadioButton (" a fruit and more due to" ) ;
JPanel pan = new JPanel ();
pan.setBorder (BorderFactory.createTitledBorder (" Cause and effect relationships " ) ) ; / / Set the display of a border strip
pan.setLayout (new GridLayout (1,3));
pan.add (jrb1);
pan.add (jrb2);
pan.add (jrb3);
cont.add (pan);
frame.addWindowListener (new WindowAdapter () {
public void windowClosing (WindowEvent obj) {
System.exit (1);
}
});
/ / task decomposition and distribution of tasks


JTextArea jta1 = new JTextArea ("12345678790");
JScrollPane scr1 = new JScrollPane (jta1, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
/ / JScrollPane scr1 = new JScrollPane (jta1);
frame.add (jta1);
jta1.setLineWrap (true);


/ / frame.setContentPane (scr1);
/ / frame.add (jta1);











frame.setLayout (null);
title.setBounds (90,5,250,20);
StartD.setBounds (70,30,100,20);
StopD.setBounds (220,30,100,20);
pan.setBounds (5,210,375,50);
jta1.setBounds (15,80,150,105);
/ / jta2.setBounds (200,80,170,87);


frame.setVisible (true);


}
};
------ Solution ---------------------------------- ----------
position it under their own adjustments .




import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class Test4 {

static final int WIDTH = 400;
static final int HEIGHT = 500;

public static void main(String args[]) {
JFrame frame = new JFrame("TCMESFD");
frame.setSize(WIDTH, HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// 菜单栏
JMenuBar menubar1 = new JMenuBar();// 添加菜单条组件
frame.setJMenuBar(menubar1);// 将菜单条添加到顶层容器中
JMenu menu1 = new JMenu("故障树"); // 设置菜单组件
JMenu menu2 = new JMenu("知识库");
JMenu menu3 = new JMenu("诊断结果");
JMenu menu4 = new JMenu("返回主界面");
JMenu menu5 = new JMenu("帮助");
menubar1.add(menu1);// 将菜单组件添加到菜单条组件中
menubar1.add(menu2);
menubar1.add(menu3);
menubar1.add(menu4);
menubar1.add(menu5);
// 标题和按钮
JLabel title = new JLabel("QWE");
JButton StartD = new JButton("开始诊断");
JButton StopD = new JButton("停止诊断");
frame.add(title);
frame.add(StartD);
frame.add(StopD);
JTextArea jta1 = new JTextArea(
"1234567asdgasdfasdgasdfaskjdglkajsdlfkjaslkdgjalksdjflkasjdgljasldfkjalskdgjlkasdjglkajsdlgkjasldkgjalskdfjlkasjdflkasjdflkasjdglkajsdlfkjasdklgjasldjgaklsdgjlkajsdglkasjdglkjasldg8790");
JScrollPane scr1 = new JScrollPane(jta1);
scr1.setBounds(60, 60, 100, 100);
frame.add(scr1);
// 故障原因与结果的关系
Container cont = frame.getContentPane();
JRadioButton jrb1 = new JRadioButton("一一对应");
JRadioButton jrb2 = new JRadioButton("一因多果");
JRadioButton jrb3 = new JRadioButton("一果多因");
JPanel pan = new JPanel();
pan.setBorder(BorderFactory.createTitledBorder("故障原因与结果的关系")); // 设置一个边框的显示条
pan.setLayout(new GridLayout(1, 3));
pan.add(jrb1);
pan.add(jrb2);
pan.add(jrb3);
cont.add(pan);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent obj) {
System.exit(1);
}
});
// 任务分解和任务分配

frame.setLayout(null);
title.setBounds(90, 5, 250, 20);
StartD.setBounds(70, 30, 100, 20);
StopD.setBounds(220, 30, 100, 20);
pan.setBounds(5, 210, 375, 50);
jta1.setBounds(15, 80, 150, 105);
// jta2.setBounds(200,80,170,87) ;

frame.setVisible(true);

}
};

------ For reference only ----------------------------------- ----

------ For reference only ---------------------------------------
as shown, more lines of text at a specific location , but also bring the vertical scroll bar. Request help
------ For reference only ------------------------------------ ---
very grateful.

没有评论:

发表评论