gpt4 book ai didi

java - JTextField 滚动

转载 作者:行者123 更新时间:2023-12-02 12:36:05 31 4
gpt4 key购买 nike

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

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

最佳答案

调整所示方法here使用 GridLayout 只需进行最少的更改。另请考虑 BoxLayout,如图 here ,或 GroupLayout,如图 herehere .

image

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

关于java - JTextField 滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15076014/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com