Java ScrollPane(内置TextArea)无法以编程方式滚动到顶部

时间:2015-11-23 01:05:24

标签: java swing jscrollpane jtextarea

为了解决这个问题,我放了2个按钮。一个叫做“Add& toTop”,另一个叫做“toTop”

一开始textArea中没有文字。

我为“Add& toTop”按钮添加了actionListener,如下所示:

btn1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

                textArea1.setText("jhggjhg\nhugffsrdtfg\ngfdrtdf\nhgftrsdf\nytfresrdcfvg\nuytyrdtesrdfgg\ntdrfygvhct\njh"
                        + "gfda\njftyuyiugcf\nhfuygihvftyughbuy\nhgyuftydfhgfyc\ndstryrfdts");
                //A long enough String
                JScrollBar jb = scrollPane1.getVerticalScrollBar();
                jb.setValue(jb.getMinimum());
                scrollPane1.repaint();
            }
        });

因此第一个按钮的功能是:“添加一些文本并滚动到顶部”

但它只会添加文字,但不会滚动到顶部

但是对于第二个按钮,我添加了一个这样的actionListener:

btn2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JScrollBar VerticalScrollBar =  scrollPane1.getVerticalScrollBar();
                VerticalScrollBar.setValue(VerticalScrollBar.getMinimum());

                scrollPane1.repaint();
            }
        });

因此,按下第一个按钮后按第二个按钮,第二个按钮将表现良好。 我真的感到困惑,为什么第一个按钮不会滚动到顶部:<

1 个答案:

答案 0 :(得分:4)

一个简单的解决方案可能是使用Value: ; ,例如

 public class AlarmClock extends Activity {
        private PendingIntent pendingIntent;
        private TimePicker alarmTimePicker;
        AlarmManager alarmManager;
        private EditText descriptionText;
        private static MainActivity inst;
        private CheckBox checkBox1,checkBox2,checkBox3,checkBox4,checkBox5,checkBox6,checkBox7;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_alarm_clock);
            alarmTimePicker = (TimePicker) findViewById(R.id.alarmTimePicker);
            alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
            checkBox1= (CheckBox) findViewById(R.id.sundayCheckBox);
            checkBox2= (CheckBox) findViewById(R.id.mondayCheckBox);
            checkBox3= (CheckBox) findViewById(R.id.tuesdayCheckBox);
            checkBox4= (CheckBox) findViewById(R.id.wednesdayCheckBox);
            checkBox5= (CheckBox) findViewById(R.id.thursdayCheckBox);
            checkBox6= (CheckBox) findViewById(R.id.fridayCheckBox);
            checkBox7= (CheckBox) findViewById(R.id.saturdayCheckBox);




        }
        public void save (View v){
            if (checkBox1.isChecked())
            {
                activityToPerform(1);
            }

            if (checkBox2.isChecked())
            {
                activityToPerform(2);
            }

            if (checkBox3.isChecked())
            {
                activityToPerform(3);
            }

            if (checkBox4.isChecked())
            {
                activityToPerform(4);
            }

            if (checkBox5.isChecked())
            {
                activityToPerform(5);
            }

            if (checkBox6.isChecked())
            {
                activityToPerform(6);
            }

            if (checkBox7.isChecked())
            {
                activityToPerform(7);
            }



        }
        private void activityToPerform(int week)
        {
            Log.d("MainActivity", "Alarm On");
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.DAY_OF_WEEK, week);
            calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getCurrentHour());
            calendar.set(Calendar.MINUTE, alarmTimePicker.getCurrentMinute());
            descriptionText= (EditText)findViewById(R.id.descriptionEditText);
            Intent myIntent = new Intent(this, AlarmBroadcast.class);
            pendingIntent = PendingIntent.getBroadcast(this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
        }

我玩JTextArea#setCaretPosition,但最终不得不使用import java.awt.BorderLayout; import java.awt.EventQueue; 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.UIManager; import javax.swing.UnsupportedLookAndFeelException; public class Test { public static void main(String[] args) { new Test(); } public Test() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { ex.printStackTrace(); } JFrame frame = new JFrame("Testing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new TestPane()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } public class TestPane extends JPanel { private JTextArea ta; public TestPane() { setLayout(new BorderLayout()); ta = new JTextArea(5, 20); add(new JScrollPane(ta)); JButton btn = new JButton("Add and to top"); btn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { ta.setText("jhggjhg\nhugffsrdtfg\ngfdrtdf\nhgftrsdf\nytfresrdcfvg\nuytyrdtesrdfgg\ntdrfygvhct\njh" + "gfda\njftyuyiugcf\nhfuygihvftyughbuy\nhgyuftydfhgfyc\ndstryrfdts"); ta.setCaretPosition(0); } }); add(btn, BorderLayout.SOUTH); } } } 让它发挥作用,例如......

scrollRectToVisible

所以我在这种情况下说SwingUtilites.invokeLater是更简单的解决方案