ورود

View Full Version : Netbeans GUI Builder problem



handinux
سه شنبه 20 مرداد 1388, 01:41 صبح
سلام دوستان
من در حال آماده کردن اینترفیس برنامم با GUI Builder و کامپوننتهای Swing هستم . مشکلم اینه که اشیا در حال ران تایم هم تغییر اندازه می دن!مثلا TextField ها وقتی تکست اونها طولش بیشتر از طول text field باشه text field اندازش بیشتر می شه!مشکل چیه؟کسی می تونه کمک کنه؟

java.source.ir
سه شنبه 20 مرداد 1388, 06:27 صبح
سلام
مشکل شما در نوع لایه بندی است که برای فرم خود انتخاب کرده اید. به این مثال نگاه کن:

// Fig. 13.20: GridBagDemo.java
// Demonstrating GridBagLayout.
// Java core packages
import java.awt.*;
import java.awt.event.*;
// Java extension packages
import javax.swing.*;
public class GridBagDemo extends JFrame {
private Container container;
private GridBagLayout layout;
private GridBagConstraints constraints;
// set up GUI
public GridBagDemo()
{
super( "GridBagLayout" );
container = getContentPane();
layout = new GridBagLayout();
container.setLayout( layout );
// instantiate gridbag constraints
constraints = new GridBagConstraints();
// create GUI components
JTextArea textArea1 = new JTextArea( "TextArea1", 5, 10 );
JTextArea textArea2 = new JTextArea( "TextArea2", 2, 2 );
String names[] = { "Iron", "Steel", "Brass" };
JComboBox comboBox = new JComboBox( names );
JTextField textField = new JTextField( "TextField" );
JButton button1 = new JButton( "Button 1" );
JButton button2 = new JButton( "Button 2" );
JButton button3 = new JButton( "Button 3" );
// textArea1
// weightx and weighty are both 0: the default
// anchor for all components is CENTER: the default
constraints.fill = GridBagConstraints.BOTH;
addComponent( textArea1, 0, 0, 1, 3 );
// button1
// weightx and weighty are both 0: the default
constraints.fill = GridBagConstraints.HORIZONTAL;
addComponent( button1, 0, 1, 2, 1 );
// comboBox
// weightx and weighty are both 0: the default
// fill is HORIZONTAL
addComponent( comboBox, 2, 1, 2, 1 );
// button2
constraints.weightx = 1000; // can grow wider
constraints.weighty = 1; // can grow taller
constraints.fill = GridBagConstraints.BOTH;
addComponent( button2, 1, 1, 1, 1 );
// button3
// fill is BOTH
constraints.weightx = 0;
constraints.weighty = 0;
addComponent( button3, 1, 2, 1, 1 );
// textField
// weightx and weighty are both 0, fill is BOTH
addComponent( textField, 3, 0, 2, 1 );
// textArea2
// weightx and weighty are both 0, fill is BOTH
addComponent( textArea2, 3, 2, 1, 1 );
setSize( 300, 150 );
setVisible( true );
}
// method to set constraints on
private void addComponent( Component component,
int row, int column, int width, int height )
{
// set gridx and gridy
constraints.gridx = column;
constraints.gridy = row;
// set gridwidth and gridheight
constraints.gridwidth = width;
constraints.gridheight = height;
// set constraints and add component
layout.setConstraints( component, constraints );
container.add( component );
}
// execute application
public static void main( String args[] )
{
GridBagDemo application = new GridBagDemo();
application.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE );
}
} // end class GridBagDemo

فکر کنم مثال فوق جواب تمام سوالات شما را بدهد. در این مثال JTextArea دومی که در پایین فرم قرار دارد، به محض اینکه داده ای در آن وارد می شود، سبب تغییر اندازه آن می گردد. در نتیجه، این تغییر اندازه بر روی JTextField و سایر مولفه های موجود در فرم اثر می گذارد.

موفق و سربلند باشید.

java.source.ir
سه شنبه 20 مرداد 1388, 06:44 صبح
در ضمن، بنده به شما پیشنهاد می کنم که از لایه بندی BorderLayout برای فرم خود استفاده نمایید. در اینصورت دیگر هیچ گونه مشکلی نخواهید داشت. برای آشنایی با این مدل لایه بندی نیز بهتر است به تاپیک زیر مراجعه نمایید:

http://barnamenevis.org/forum/showthread.php?t=172440