2

我制作这个程序是为了好玩,但我被卡住了,因为程序在屏幕外运行。我如何实现滚动条而不必完全更改我的代码。

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();
4

2 回答 2

6

调整此处显示的方法以使用GridLayout所需的最小更改。还要考虑BoxLayout,显示在这里,或者GroupLayout,显示在这里这里

图片

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();
            }
        });
    }
}
于 2013-02-25T23:02:52.837 回答
4

您可以使用 JScrollPane。

您需要做的就是实例化 JScrollPane 类,并作为第一个参数为其提供包含所有文本字段的内容窗格。

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

之后,只需将您的 scrollPane 添加到主内容窗格。

于 2013-02-25T20:44:44.763 回答