JTextField scroll

2019-07-10 02:18发布

问题:

Im making this program for fun and i got stuck because the program was running off the screen. how do i implement a scroll bar without having to change my code completely.

public static void main(String args[]) throws IOException 
    {

          String ai,ia,ny;
          JTextField field1 = new JTextField();
          JTextField field2 = new JTextField();
          JTextField field3 = new JTextField();
          JTextField field4 = new JTextField();
          JTextField field5 = new JTextField();
          JTextField field6 = new JTextField();
          JTextField field7 = new JTextField();
          JTextField field8 = new JTextField();
          JTextField field9 = new JTextField();
          JTextField field10 = new JTextField();
          JTextField field11 = new JTextField();
          JTextField field12 = new JTextField();
          JTextField field13 = new JTextField();
          JTextField field14 = new JTextField();
          JTextField field15 = new JTextField();
          JTextField field16 = new JTextField();
          JTextField field17 = new JTextField();
          JTextField field18 = new JTextField();
          JTextField field19 = new JTextField();
          JTextField field20 = new JTextField();
          JTextField field21 = new JTextField();
          JTextField field22 = new JTextField();
          JTextField field23 = new JTextField();

      Object[] message = {
          "Rent:", field1,
          "Water:", field2,
          "Light:", field3,    
          "DTV:", field4, 
          "Sprint:", field5, 
          "stf:", field6, 
          "Tithe:", field7, 
          "sff:", field8, 
          "trr:", field9, 
          "Citi:", field10, 
          "Chase:", field11, 
          "Walmart:", field12, 
          "Miss:", field13, 
          "Charter",field14,
          "Insurance:",field15,
          "Contribution", field16,
          "Lowes", field17,
          "Stream", field18,
          //"Water", field19,
          //"", field,

      };

        //user input
      //final ImageIcon icon = new ImageIcon("icon.jpg");
      int option = JOptionPane.showConfirmDialog(field1, message, "Enter all your info", JOptionPane.OK_CANCEL_OPTION);

    if (option == JOptionPane.OK_OPTION)
      {
          String Rent = field1.getText();
          String Water = field2.getText();
          String Light = field3.getText();
          String Dtv = field4.getText();
          String Sprint = field5.getText();
          String Par = field6.getText();
          String Tit = field7.getText();
          String sd = field8.getText();
          String sdd = field9.getText();
          String Citi = field10.getText();
          String Chase = field11.getText();
          String Wal = field12.getText();
          String sd = field13.getText();
          String Chasrt = field14.getText();
          String ds = field15.getText();
          String sdd = field16.getText();
          String Lowes = field17.getText();
          String Stream = field18.getText();

回答1:

Adapting the approach shown here to use GridLayout required minimal changes. Also consider BoxLayout, shown here, or GroupLayout, shown here and here.

import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

/**
 * @see https://stackoverflow.com/a/15078211/230513
 */
public class Test {

    JTextField field1 = new JTextField();
    JTextField field2 = new JTextField();
    JTextField field3 = new JTextField();
    JTextField field4 = new JTextField();
    JTextField field5 = new JTextField();
    JTextField field6 = new JTextField();
    JTextField field7 = new JTextField();
    JTextField field8 = new JTextField();
    JTextField field9 = new JTextField();
    JTextField field10 = new JTextField();
    JTextField field11 = new JTextField();
    JTextField field12 = new JTextField();
    JTextField field13 = new JTextField();
    JTextField field14 = new JTextField();
    JTextField field15 = new JTextField();
    JTextField field16 = new JTextField();
    JTextField field17 = new JTextField();
    JTextField field18 = new JTextField();
    Object[] message = {
        "Rent:", field1,
        "Water:", field2,
        "Light:", field3,
        "DTV:", field4,
        "Sprint:", field5,
        "stf:", field6,
        "Tithe:", field7,
        "sff:", field8,
        "trr:", field9,
        "Citi:", field10,
        "Chase:", field11,
        "Walmart:", field12,
        "Miss:", field13,
        "Charter", field14,
        "Insurance:", field15,
        "Contribution", field16,
        "Lowes", field17,
        "Stream", field18,};

    private void display() {
        JPanel panel = new JPanel(new GridLayout(0, 2));
        int i = 0;
        while (i < message.length) {
            panel.add(new JLabel((String) message[i++], JLabel.RIGHT));
            panel.add((Component) message[i++]);
        }
        JScrollPane jsp = new JScrollPane(panel) {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(320, 240);
            }
        };
        JOptionPane.showMessageDialog(
            null, jsp, "Enter data", JOptionPane.PLAIN_MESSAGE);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Test().display();
            }
        });
    }
}


回答2:

You can use a JScrollPane.

All you need to do is instance the JScrollPane class and as the first parameter give it the content pane that is housing all of your text fields.

JScrollPane scrollPane = new JScrollPane(Your main content pane here, ScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

After that just add your scrollPane to the main content pane.