gpt4 book ai didi

java - 如何增加Tab中JTextField的宽度?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:22:13 24 4
gpt4 key购买 nike

Food Tab中,我想实现这个

enter image description here

但我只能得到这个

enter image description here

如何增加 Food Tab 中 JTextField 的宽度?下面是我的代码

 public class FoodOrdering {
static private JFrame frame;
static private JTextField textField;
static private GridBagConstraints gbc;
static private JLabel[] foodLabel;
static private JLabel[] labels;
static private JTextField[] qtyField;
static private JLabel[] foodImage;
static private File[] file;
private static final int ELEMENTS = 9;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FoodOrdering window = new FoodOrdering();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*
* @throws IOException
*/
public FoodOrdering() throws IOException {
initialize();
}
/**
* Initialize the contents of the frame.
*
* @throws IOException
*/
static void initialize() throws IOException {
frame = new JFrame();
frame.setBounds(100, 100, 700, 550);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame.setLocationRelativeTo(null);
JLabel lblFoodOrdered = new JLabel("Food Ordered");
lblFoodOrdered.setBounds(529, 11, 81, 14);
frame.getContentPane().add(lblFoodOrdered);
TextArea textArea = new TextArea();
textArea.setBounds(462, 31, 199, 275);
frame.getContentPane().add(textArea);
JLabel lblTotal = new JLabel("Total : ");
lblTotal.setBounds(519, 315, 46, 14);
frame.getContentPane().add(lblTotal);
textField = new JTextField();
textField.setBounds(575, 312, 86, 20);
frame.getContentPane().add(textField);
textField.setColumns(10);
JButton btnOrder = new JButton("Order");
btnOrder.setBounds(521, 352, 89, 23);
frame.getContentPane().add(btnOrder);
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
addIt(tabbedPane, "Foods");
addIt1(tabbedPane, "Drinks");
addIt1(tabbedPane, "Desserts");
tabbedPane.setBounds(23, 11, 400, 450);
frame.getContentPane().add(tabbedPane);
frame.setVisible(true);
}
static void addIt1(JTabbedPane tabbedPane, String text) {
JLabel label = new JLabel(text);
JButton button = new JButton(text);
JPanel panel = new JPanel();
panel.add(label);
panel.add(button);
tabbedPane.addTab(text, panel);
}
static void addIt(JTabbedPane tabbedPane, String text) throws IOException {
JPanel panel = new JPanel(new GridBagLayout());
gbc = new GridBagConstraints();
gbc.insets = new Insets(1, 1, 1, 1);
foodImage = new JLabel[ELEMENTS];
foodLabel = new JLabel[ELEMENTS];
labels = new JLabel[ELEMENTS];
qtyField = new JTextField[ELEMENTS];
file = new File[ELEMENTS];
try {
file[0] = new File("C:\\Users\\tony\\Desktop\\MedSalad.png");
file[1] = new File("C:\\Users\\tony\\Desktop\\JapanesePanNoodles.png");
file[2] = new File("C:\\Users\\tony\\Desktop\\Spaghetti.png");
file[3] = new File("C:\\Users\\tony\\Desktop\\PadThai.png");
file[4] = new File("C:\\Users\\tony\\Desktop\\RamenNoodles.png");
file[5] = new File("C:\\Users\\tony\\Desktop\\SpaghettiAndMeatBalls.png");
file[6] = new File("C:\\Users\\tony\\Desktop\\chickenRice.jpg");
file[7] = new File("C:\\Users\\tony\\Desktop\\thaiFood.jpeg");
file[8] = new File("C:\\Users\\tony\\Desktop\\vietnamFood.jpg");
foodLabel[0] = new JLabel("Salad");
foodLabel[1] = new JLabel("Japanese Noodles");
foodLabel[2] = new JLabel("Spaghetti");
foodLabel[3] = new JLabel("Spaghetti Meat Balls");
foodLabel[4] = new JLabel("Noodles");
foodLabel[5] = new JLabel("Kids Spaghetti");
foodLabel[6] = new JLabel("Chicken Rice");
foodLabel[7] = new JLabel("Thai Food");
foodLabel[8] = new JLabel("Vietnam Food");
} catch (Exception e) {
e.printStackTrace();
}
for (int i = 0; i < ELEMENTS; i++) {
Image image = ImageIO.read(file[i]);
Image imageScaled = image.getScaledInstance(80, 95, Image.SCALE_SMOOTH);
ImageIcon imageIcon = new ImageIcon(imageScaled);
qtyField[i] = new JTextField(3);
foodImage[i] = new JLabel(imageIcon);
}
gbc.gridx = 0;
for (int i = 0; i < ELEMENTS; i++) {
if (i % 3 == 0) {
gbc.gridy += 2;
gbc.gridx = 0;
}
panel.add(foodImage[i], gbc);
gbc.gridy++;
panel.add(foodLabel[i], gbc);
gbc.gridy--;
gbc.gridx++;
panel.add(qtyField[i], gbc);
gbc.gridx++;
tabbedPane.addTab(text, panel);
}
}
public void setVisible(boolean b) throws IOException {
}
}

最佳答案

由于 a very old "bug",JTextfield 对象非常窄在 GridBagLayout 中,这导致它忽略其内容的首选大小。

有几种可能的方法来解决这个错误:

  1. 按照链接中的说明创建一个名为 PreferredGridBagLayout 的类,并使用它代替 GridBagLayout
  2. 使用 qtyField[i].setMinimumSize(qtyField[i].getPreferredSize()) 设置每个 qtyField 实例的最小大小。
  3. 创建 JTextField 的子类,覆盖方法 getMinimumSize() 以返回与 getPreferredSize() 相同的值,或其他一些合理的最小值大小。

因为这个问题在使用 GridBagLayout 时很常见,从长远来看,解决方案 #1 是最简单的。

之后,您需要使 tabbedPane 对象更宽一些,或者切换到主面板中的布局管理器,自动确定选项卡式 Pane 的大小。

您的代码中有很多地方可以改进。在 Swing 中创建良好的布局并不容易,您需要做更多的工作才能创建漂亮的布局。但这将解决您折叠文本字段的问题。

关于java - 如何增加Tab中JTextField的宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45298098/

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