Rav*_*avi 7 java swing awt flowlayout
如何在布局中将组件放置在特定位置.就像我想在第一行中放置2个文本框,在3个组合框下面.
但是当我试图将它们全部显示在一行中并且我使用了flowlayout时.我也使用过边框.当我调整大小时,组件的窗口大小将从边框出来.
你能建议我使用一些布局以及如何使用它吗?
这是我的代码:
topPanel=new JPanel();
topPanel.setLayout(new FlowLayout());
topPanel.setBorder(new TitledBorder(new EtchedBorder(), "Customer Data"));
CNameTextField = new JTextField (20); // create the Customer Name text field
CNameTextField.setEditable(true); // set editable text box
CIDLabel=new JLabel("Customer ID");
C_IDTextField = new JTextField (10);
C_IDTextField.setEditable(true); // set editable text box
topPanel.add(CNameTextField);
topPanel.add(C_IDTextField);
// Create and populate Room type combo box
roomTypeCombo = new JComboBox();
roomTypeCombo.addItem( "Budget($50)" );
// Create and populate Meal type combo box
mealCombo = new JComboBox();
mealCombo.addItem( "None" );
// Create and populate Days combo box
daysCombo = new JComboBox();
for(int i=0;i<31 ; i++) {
// populate combobox with days
daysCombo.addItem(i);
}
// Adding rest of the components to top panel
topPanel.add(roomTypeCombo);
topPanel.add(mealCombo);
topPanel.add(daysCombo);
Run Code Online (Sandbox Code Playgroud)
谢谢.
最具体的布局类型是绝对定位.
警告:绝对定位应该很少使用(如果有的话).原因有很多.这是一个:绝对定位(无布局管理器)与MiGlayout中的绝对定位
- 感谢用户brimborium添加警告的好主意.
话虽如此,这里是如何使用绝对定位:
在上面的代码中,不是将topPanel布局设置为,而是FlowLayout将其设置为null.
topPanel.setLayout(null);
Run Code Online (Sandbox Code Playgroud)
稍后在代码中,在开始添加组件之前topPanel,调用容器的setBounds方法:
someJComponent.setBounds(x-coord, y-coord, width, height);
Run Code Online (Sandbox Code Playgroud)
因此,例如,您创建了一个实例JComboBox()并将其命名roomTypeCombo,以下代码显示了如何绝对定位roomTypeCombo.
topPanel.setLayout(null);
// code...
roomTypeCombo = new JComboBox();
// code...
roomTypeCombo.setBounds(100, 100, 200, 50);
topPanel.add(roomTypeCombo);
Run Code Online (Sandbox Code Playgroud)
setBounds上面使用的方法有四个参数:
int x-coord- 设置roomTypeCombo相对于其父级的x坐标topPanel.int y-coord- 设置roomTypeCombo相对于其父级的y坐标topPanel.int width- 指定roomTypeCombo宽度.int height- 指定roomTypeCombo高度.
我会玩坐标,看看你是否喜欢它的任何东西.可能发生的最糟糕的事情是你回到使用布局,这可能比绝对定位更好.或者您可以实现自己的布局管理器,如果您按照此超链接,第一个答案将讨论如何实现您自己的布局管理器并提供有用的链接.
尝试更改布局. http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html
您可以GridLayout使用两行(例如,有一些其他可能的组合),每行包含3个JComboBox和两个JTextField.
仔细查看文档并查看可在Web上轻松访问的一些示例.
import java.awt.GridLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class SwingResizeJFrame {
public SwingResizeJFrame() {
JTextField TextField1 = new JTextField("firstTextField");
JTextField TextField2 = new JTextField("secondTextField");
JPanel firstPanel = new JPanel();
firstPanel.setLayout(new GridLayout(0, 2, 10, 10));
firstPanel.add(TextField1);
firstPanel.add(TextField2);
JComboBox comboBox1 = new JComboBox(new Object[]{"Ester", "Jordi", "Jordina", "Jorge", "Sergi"});
JComboBox comboBox2 = new JComboBox(new Object[]{"Ester", "Jordi", "Jordina", "Jorge", "Sergi"});
JComboBox comboBox3 = new JComboBox(new Object[]{"Ester", "Jordi", "Jordina", "Jorge", "Sergi"});
JPanel secondPanel = new JPanel();
secondPanel.setLayout(new GridLayout(0, 3, 10, 10));
secondPanel.add(comboBox1);
secondPanel.add(comboBox2);
secondPanel.add(comboBox3);
JFrame frame = new JFrame();
frame.setLayout(new GridLayout(2, 1, 10, 10));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(firstPanel);
frame.add(secondPanel);
frame.pack();
frame.setLocation(150, 150);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
SwingResizeJFrame demo = new SwingResizeJFrame();
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22644 次 |
| 最近记录: |