2017-07-25 16 views
9

食品标签,我想要实现这个如何在Tab中增加JTextField的宽度?

enter image description here

但我只能够得到这个

enter image description here

我怎样才能提高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 { 
     } 
    } 
+4

我要做的第一件事就是摆脱'frame.getContentPane()。setLayout(null);'并开始使用合适的布局管理器,它可以更好地处理JTabbedPane的尺寸要求。在我看来,带有'GridBagLayout'的面板完全没有空间,并且正在使用组件的最小尺寸而不是 – MadProgrammer

+0

创建'JTextField',而不是直接将它添加到数组中,因此您可以执行一些操作它。那么你可以调用'textField.setPreferredSize(new Dimension(50,(int)textPane.getPreferredSize()。getHeight()));' – XtremeBaumer

+3

@XtremeBaumer'new JTextField(3);'将文本字段的preferredSize设置为基于当前字体度量的三列。这个问题可能来自于使用'null'布局管理器,它已经引起了问题,并且手动更改字段的首选大小既不是一个好主意,而且可能会导致一系列额外的问题 - 直接的解决方案是找到更好的框架的布局管理器 – MadProgrammer

回答

4

JTextField的对象是因为在GridBagLayout从而导致它忽略其内容物的优选尺寸a very old "bug"如此窄。

有几种可能的工作,解决此错误的方法:

  1. 创建一个名为PreferredGridBagLayout类中的链接和使用而不是GridBagLayout作为解释。
  2. 使用qtyField[i].setMinimumSize(qtyField[i].getPreferredSize())设置每个qtyField实例的最小大小。
  3. 创建JTextField的子类,它们覆盖方法getMinimumSize()以返回与getPreferredSize()相同的值或其他合理的最小尺寸。

因为这个问题在使用GridBagLayout时很常见,所以解决方案#1是最容易的。

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

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

1

只是这个问题可以通过设置

qtyField[i].setMinimumSize(new Dimension(33,20)); 

功能来解决。设置最小尺寸的原因是GridBagLayout没有足够的空间来正确排列所有这些组件,因此它只使用空的textField的最小尺寸。

qtyField[i] = new JTextField(3); 

后只需添加代码,你也将需要增加的的TabbedPane多一点点的宽度,使可见

enter image description here

组件(qtyField)
0

而不是使用空布局。使用一些其他布局,如边框布局。然后使用一行:

frame.pack(); 

这将显示所有组件的首选大小。

只是一种编码习惯:不是设置边界而是使用面板来添加组件,然后将这些面板添加到主面板。 就像使用一个不同的面板添加订购食品的Jlabel,textarea,Jlabel订单和订购按钮。然后将该面板添加到主面板。

希望你能理解。 :)

+0

问题是关于* GridBagLayout *和* qtyField *。 –

+0

使用'null'布局通常是一种不好的做法,但用户代码中的错误在另一个使用'GridBagLayout'的面板中。 – Enwired