如何防止为 BoxLayout 扩大 JFormattedTextField?
How to prevent enlarging JFormattedTextField for BoxLayout?
我想在一列中包含 JFormattedTextField
个组件。我不明白为什么这段代码会导致放大到最大尺寸。
// Create the text fields and set them up.
priemerKolesa = new JFormattedTextField(numberFormat);
priemerOsi = new JFormattedTextField(numberFormat);
hrubkaPlasta = new JFormattedTextField(numberFormat);
hrubkaKolesa = new JFormattedTextField(numberFormat);
hrubkaNavinu = new JFormattedTextField(numberFormat);
hrubkaNavinu.setColumns(6);
// Layout the text fields in a panel.
JPanel fieldPane = new JPanel();
fieldPane.setLayout(new BoxLayout(fieldPane, BoxLayout.Y_AXIS));
fieldPane.add(priemerKolesa);
fieldPane.add(priemerOsi);
fieldPane.add(hrubkaPlasta);
fieldPane.add(hrubkaKolesa);
fieldPane.add(hrubkaNavinu);
我想把它放在一个漂亮的堆栈中,而不是像这样扩展。
似乎 BoxLayout
拉伸组件以适应可用的 space。一种解决方法是首先将每个组件放入 JPanel
和 FlowLayout
中(这将 不 拉伸其中的组件)然后将该面板添加到容器中使用BoxLayout
。例如。以上面提到的 MCVE 的形式:
import java.awt.*;
import java.text.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class BoxLayoutQuandary {
private JComponent ui = null;
BoxLayoutQuandary() {
initUI();
}
public void initUI() {
if (ui != null) {
return;
}
ui = new JPanel(new BorderLayout(4, 4));
ui.setBorder(new EmptyBorder(4, 4, 4, 4));
NumberFormat numberFormat = new DecimalFormat("##.#");
// Create the text fields and set them up.
JFormattedTextField priemerKolesa = new JFormattedTextField(numberFormat);
priemerKolesa.setColumns(8);
JFormattedTextField hrubkaNavinu = new JFormattedTextField(numberFormat);
hrubkaNavinu.setColumns(6);
// Layout the text fields in a panel.
JPanel fieldPane = new JPanel();
fieldPane.setLayout(new BoxLayout(fieldPane, BoxLayout.Y_AXIS));
ui.add(fieldPane);
JPanel priemerKolesaPanel = new JPanel();
priemerKolesaPanel.add(priemerKolesa);
fieldPane.add(priemerKolesaPanel);
JPanel hrubkaNavinuPanel = new JPanel();
hrubkaNavinuPanel.add(hrubkaNavinu);
fieldPane.add(hrubkaNavinuPanel);
}
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) {
}
BoxLayoutQuandary o = new BoxLayoutQuandary();
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);
}
}
我想在一列中包含 JFormattedTextField
个组件。我不明白为什么这段代码会导致放大到最大尺寸。
// Create the text fields and set them up.
priemerKolesa = new JFormattedTextField(numberFormat);
priemerOsi = new JFormattedTextField(numberFormat);
hrubkaPlasta = new JFormattedTextField(numberFormat);
hrubkaKolesa = new JFormattedTextField(numberFormat);
hrubkaNavinu = new JFormattedTextField(numberFormat);
hrubkaNavinu.setColumns(6);
// Layout the text fields in a panel.
JPanel fieldPane = new JPanel();
fieldPane.setLayout(new BoxLayout(fieldPane, BoxLayout.Y_AXIS));
fieldPane.add(priemerKolesa);
fieldPane.add(priemerOsi);
fieldPane.add(hrubkaPlasta);
fieldPane.add(hrubkaKolesa);
fieldPane.add(hrubkaNavinu);
我想把它放在一个漂亮的堆栈中,而不是像这样扩展。
似乎 BoxLayout
拉伸组件以适应可用的 space。一种解决方法是首先将每个组件放入 JPanel
和 FlowLayout
中(这将 不 拉伸其中的组件)然后将该面板添加到容器中使用BoxLayout
。例如。以上面提到的 MCVE 的形式:
import java.awt.*;
import java.text.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class BoxLayoutQuandary {
private JComponent ui = null;
BoxLayoutQuandary() {
initUI();
}
public void initUI() {
if (ui != null) {
return;
}
ui = new JPanel(new BorderLayout(4, 4));
ui.setBorder(new EmptyBorder(4, 4, 4, 4));
NumberFormat numberFormat = new DecimalFormat("##.#");
// Create the text fields and set them up.
JFormattedTextField priemerKolesa = new JFormattedTextField(numberFormat);
priemerKolesa.setColumns(8);
JFormattedTextField hrubkaNavinu = new JFormattedTextField(numberFormat);
hrubkaNavinu.setColumns(6);
// Layout the text fields in a panel.
JPanel fieldPane = new JPanel();
fieldPane.setLayout(new BoxLayout(fieldPane, BoxLayout.Y_AXIS));
ui.add(fieldPane);
JPanel priemerKolesaPanel = new JPanel();
priemerKolesaPanel.add(priemerKolesa);
fieldPane.add(priemerKolesaPanel);
JPanel hrubkaNavinuPanel = new JPanel();
hrubkaNavinuPanel.add(hrubkaNavinu);
fieldPane.add(hrubkaNavinuPanel);
}
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) {
}
BoxLayoutQuandary o = new BoxLayoutQuandary();
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);
}
}