如何在Java中为组布局设置Scrollable TextArea?

时间:2012-11-12 13:08:27

标签: java swing jframe jtextarea keylistener

我有问题。每次,当我向TextArea添加更多符号(数字)时,它都不会使其可滚动。编辑:现在它按我的意愿工作。我只需要改变2个单词。感谢。

class NumOnly extends KeyAdapter {  

    private String Atlauts = "[^0-9]";  //Allowed Buttons.
    public void keyReleased(KeyEvent e) {   //Key event. What happens when the button is pressed
        String curText = ((JTextComponent) e.getSource()).getText();  //Current text
        curText = curText.replaceAll(Atlauts, ""); 

        ((JTextComponent) e.getSource()).setText(curText);  
    }  
}  

public class kursadarbs{

    public static void main(String[] args) {

        JFrame frame= new JFrame();
        JPanel panel= new JPanel();

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //components
        JLabel label1= new JLabel("Insert first number: ");

        final JTextField textbox1= new JTextField(10);
        textbox1.addKeyListener(new NumOnly());

        JLabel label2= new JLabel("Insert second number: ");

        final JTextField textbox2= new JTextField(10);
        textbox2.addKeyListener(new NumOnly());

        JButton button= new JButton("Calculate");

        final JTextArea textarea= new JTextArea(20,20); //Result is stored in there
        textarea.setEditable(false);
        textarea.setLineWrap(true);
        JScrollPane scroll= new JScrollPane(textarea);
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        textarea.setWrapStyleWord(true);
        textarea.setBorder(new TitledBorder(new EtchedBorder(), "Result"));




        GroupLayout groupLayout = new GroupLayout(panel);
        panel.setLayout(groupLayout);  
        groupLayout.setAutoCreateGaps(true);      
        groupLayout.setAutoCreateContainerGaps(true); 

        GroupLayout.SequentialGroup HorSGroup= groupLayout.createSequentialGroup(); 
        GroupLayout.SequentialGroup VerSGroup= groupLayout.createSequentialGroup(); 

        GroupLayout.ParallelGroup HParallelGroup1= groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING);
        GroupLayout.ParallelGroup HParallelGroup2= groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING);
        GroupLayout.ParallelGroup HParallelGroup3= groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING);

        HParallelGroup1.addComponent(label1); //adding components to the group
        HParallelGroup1.addComponent(label2);
        HParallelGroup2.addComponent(textbox1);
        HParallelGroup2.addComponent(textbox2);
        HParallelGroup2.addComponent(scroll);
        HParallelGroup3.addComponent(button);

        HorSGroup.addGroup(HParallelGroup1);
        HorSGroup.addGroup(HParallelGroup2);
        HorSGroup.addGroup(HParallelGroup3);

        GroupLayout.ParallelGroup VerPGroup1= groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE); //Vertical group
        GroupLayout.ParallelGroup VerPGroup2= groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE);
        GroupLayout.ParallelGroup VerPGroup3= groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE);

        VerPGroup1.addComponent(label1); //adding components to groups
        VerPGroup1.addComponent(textbox1);
        VerPGroup1.addComponent(button);
        VerPGroup2.addComponent(label2);
        VerPGroup2.addComponent(textbox2);
        VerPGroup3.addComponent(scroll);

        VerSGroup.addGroup(VerPGroup1);
        VerSGroup.addGroup(VerPGroup2);
        VerSGroup.addGroup(VerPGroup3);

        groupLayout.setHorizontalGroup(HorSGroup);
        groupLayout.setVerticalGroup(VerSGroup);




        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (int) (dimension.getWidth()/4);
        int y = (int) (dimension.getHeight()/4);
        frame.setLocation(x, y); //Places the program almost in the middle

        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
               //what happens when "calculate" is pressed.
                String first = null, second = null; 
                int first1=0, second1=0;

               first= textbox1.getText();  //getting textbox1 value.
               second= textbox2.getText();

               if(!first.isEmpty() && !second.isEmpty()) //If fields are not empty..
               {
                   first1= Integer.parseInt(first);  //string to integer.
                   second1= Integer.parseInt(second);

                   if(first1<second1){ //Check, if the first number is bigger than second.
                   System.out.println(first1);                 
                   textarea.append(first+"\n");
                   }// Ja ir pareizi 
                   else 
                   {
                       JOptionPane.showMessageDialog(null,"Incorrect data. " );
                   }
               } else 
               {
                   JOptionPane.showMessageDialog(null,"Incorrect data." );
               }
            }
        });


        frame.add(panel); //add the panel
        frame.setSize(500, 500); //program size in pix
        frame.setResizable(false); //putting that the frame can't change size
        frame.setTitle("Kursa darbs");
        frame.setVisible(true);


    }

}

嗯,主要问题是Textarea。如您所见,英语不是我的母语,我是JFrame的初学者。 我已经尝试了一切......请帮助。

谢谢你。

2 个答案:

答案 0 :(得分:3)

答案 1 :(得分:2)

不是将JTextArea添加到群组,而是添加包含它的JScrollPane

    //HParallelGroup2.addComponent(textarea);
    HParallelGroup2.addComponent(scroll);
    HParallelGroup3.addComponent(button);

    HorSGroup.addGroup(HParallelGroup1); //Horizontālās grupas tiek pievienotas sakārtotajai grupai
    HorSGroup.addGroup(HParallelGroup2);
    HorSGroup.addGroup(HParallelGroup3);

    GroupLayout.ParallelGroup VerPGroup1= groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE); //Izveido Vertikālās grupas.
    GroupLayout.ParallelGroup VerPGroup2= groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE);
    GroupLayout.ParallelGroup VerPGroup3= groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE);

    VerPGroup1.addComponent(label1); //Pievieno objektus
    VerPGroup1.addComponent(textbox1);
    VerPGroup1.addComponent(button);
    VerPGroup2.addComponent(label2);
    VerPGroup2.addComponent(textbox2);
    //VerPGroup3.addComponent(textarea);
    VerPGroup3.addComponent(scroll);