JTextField 数组。面板对齐
Array of JTextField. Alignment in panel
您好!我正在尝试像图片中那样定位一个 JTextField 数组。我选择制作一个数组,因为我想拥有尽可能多的字段。
现在,我遇到的问题是,当我将 JTextFields 放置在面板中时,它会将它们垂直放置。
是否也可以在可滚动面板中插入文本字段?
private int nrFields=4;
private JTextField[] fields = new JTextField[nrFields];
private JPanel panelNew = new JPanel();
private void create() {
for (JTextField auxField: fields) {
panelNew.add(auxField=new JTextField(30));
}
JFrame frame = new JFrame("Frame");
frame.getContentPane().add(this.panelNew);
frame.setSize(800, 800);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
编辑:
我设法通过将文本字段放在一个面板中来解决问题,我在面板上将布局设置为 boxlayout。接下来我将这个面板放在一个可滚动的窗格中,这个窗格被放置在一个新的面板中。
private int nrFields=10;
private JTextField[] fields = new JTextField[nrFields];
private JPanel panelNew = new JPanel();
private JScrollPane newScroll = new JScrollPane();
private JPanel panelMain = new JPanel();
private void create() {
newScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
panelMain.setLayout(new BoxLayout(panelMain,BoxLayout.Y_AXIS));
for (int i=0;i<nrFields;i++) {
panelMain.add(fields[i]=new JTextField(30));
}
newScroll.setViewportView(panelMain);
panelNew.add(newScroll);
JFrame frame = new JFrame("Frame");
frame.getContentPane().add(this.panelNew);
frame.setSize(800, 800);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
您必须使用 JScrollPane。然后调用 setViewportview(Component)。然后只需将 JTextField 放入其中。如果您可以 post 一些代码,也许我可以提供更多帮助。
希望对你有帮助。
这是(基本上)我会如何去做。
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.util.Random;
public class VerticalTextFields {
private JComponent ui = null;
VerticalTextFields() {
initUI();
}
public void initUI() {
if (ui!=null) return;
ui = new JPanel(new BorderLayout(4,4));
ui.setBorder(new EmptyBorder(4,20,4,20));
Random r = new Random();
int n = 10 + r.nextInt(10);
JPanel textFieldContainer = new JPanel(new GridLayout(0, 1, 5, 5));
/*
While I do not see the point of this array, you may have
some purpose for it that is not immediately obvious, so..
*/
JTextField[] textFieldArray = new JTextField[n];
for (int ii=0; ii<n; ii++) {
textFieldArray[ii] = new JTextField("Text field " + (ii+1), 40);
textFieldContainer.add(textFieldArray[ii]);
}
JScrollPane textFieldScrollPane = new JScrollPane(textFieldContainer);
Dimension d = textFieldContainer.getPreferredSize();
textFieldScrollPane.getViewport().setPreferredSize(
new Dimension(d.width, 200));
ui.add(textFieldScrollPane, BorderLayout.CENTER);
JPanel textAreaContainer = new JPanel(new FlowLayout());
textAreaContainer.setBorder(new EmptyBorder(10, 50, 10, 50));
textAreaContainer.add(new JScrollPane(new JTextArea("Text area", 3, 20)));
ui.add(textAreaContainer, BorderLayout.PAGE_START);
JPanel buttonContainer = new JPanel(new BorderLayout());
buttonContainer.setBorder(new EmptyBorder(10, 10, 10, 10));
ui.add(buttonContainer, BorderLayout.PAGE_END);
buttonContainer.add(new JButton("Line Start"), BorderLayout.LINE_START);
buttonContainer.add(new JButton("Line End"), BorderLayout.LINE_END);
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
VerticalTextFields o = new VerticalTextFields();
JFrame f = new JFrame(o.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}
您好!我正在尝试像图片中那样定位一个 JTextField 数组。我选择制作一个数组,因为我想拥有尽可能多的字段。
现在,我遇到的问题是,当我将 JTextFields 放置在面板中时,它会将它们垂直放置。
是否也可以在可滚动面板中插入文本字段?
private int nrFields=4;
private JTextField[] fields = new JTextField[nrFields];
private JPanel panelNew = new JPanel();
private void create() {
for (JTextField auxField: fields) {
panelNew.add(auxField=new JTextField(30));
}
JFrame frame = new JFrame("Frame");
frame.getContentPane().add(this.panelNew);
frame.setSize(800, 800);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
编辑:
我设法通过将文本字段放在一个面板中来解决问题,我在面板上将布局设置为 boxlayout。接下来我将这个面板放在一个可滚动的窗格中,这个窗格被放置在一个新的面板中。
private int nrFields=10;
private JTextField[] fields = new JTextField[nrFields];
private JPanel panelNew = new JPanel();
private JScrollPane newScroll = new JScrollPane();
private JPanel panelMain = new JPanel();
private void create() {
newScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
panelMain.setLayout(new BoxLayout(panelMain,BoxLayout.Y_AXIS));
for (int i=0;i<nrFields;i++) {
panelMain.add(fields[i]=new JTextField(30));
}
newScroll.setViewportView(panelMain);
panelNew.add(newScroll);
JFrame frame = new JFrame("Frame");
frame.getContentPane().add(this.panelNew);
frame.setSize(800, 800);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
您必须使用 JScrollPane。然后调用 setViewportview(Component)。然后只需将 JTextField 放入其中。如果您可以 post 一些代码,也许我可以提供更多帮助。 希望对你有帮助。
这是(基本上)我会如何去做。
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.util.Random;
public class VerticalTextFields {
private JComponent ui = null;
VerticalTextFields() {
initUI();
}
public void initUI() {
if (ui!=null) return;
ui = new JPanel(new BorderLayout(4,4));
ui.setBorder(new EmptyBorder(4,20,4,20));
Random r = new Random();
int n = 10 + r.nextInt(10);
JPanel textFieldContainer = new JPanel(new GridLayout(0, 1, 5, 5));
/*
While I do not see the point of this array, you may have
some purpose for it that is not immediately obvious, so..
*/
JTextField[] textFieldArray = new JTextField[n];
for (int ii=0; ii<n; ii++) {
textFieldArray[ii] = new JTextField("Text field " + (ii+1), 40);
textFieldContainer.add(textFieldArray[ii]);
}
JScrollPane textFieldScrollPane = new JScrollPane(textFieldContainer);
Dimension d = textFieldContainer.getPreferredSize();
textFieldScrollPane.getViewport().setPreferredSize(
new Dimension(d.width, 200));
ui.add(textFieldScrollPane, BorderLayout.CENTER);
JPanel textAreaContainer = new JPanel(new FlowLayout());
textAreaContainer.setBorder(new EmptyBorder(10, 50, 10, 50));
textAreaContainer.add(new JScrollPane(new JTextArea("Text area", 3, 20)));
ui.add(textAreaContainer, BorderLayout.PAGE_START);
JPanel buttonContainer = new JPanel(new BorderLayout());
buttonContainer.setBorder(new EmptyBorder(10, 10, 10, 10));
ui.add(buttonContainer, BorderLayout.PAGE_END);
buttonContainer.add(new JButton("Line Start"), BorderLayout.LINE_START);
buttonContainer.add(new JButton("Line End"), BorderLayout.LINE_END);
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
VerticalTextFields o = new VerticalTextFields();
JFrame f = new JFrame(o.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}