Hallo liebes forum,
ich versuch so etwas wie ein Formular zu erstellen,jedoch bekomme ich das nicht richtig hin.
Ich habe dafür ein code geschrieben,wenn ihr dies mal anschauen könnt würde ich mich sehr freuen.
Danke
ich versuch so etwas wie ein Formular zu erstellen,jedoch bekomme ich das nicht richtig hin.
Ich habe dafür ein code geschrieben,wenn ihr dies mal anschauen könnt würde ich mich sehr freuen.
Danke
Code:
// Fig. 25.21: GridBagFrame.java
// Demonstrating GridBagLayout.
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Component;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
public class GridBagFrame extends JFrame
{
private GridBagLayout layout; // layout of this frame
private GridBagConstraints constraints; // constraints of this layout
// set up GUI
public GridBagFrame()
{
super( "GridBagLayout" );
layout = new GridBagLayout();
setLayout( layout ); // set frame layout
constraints = new GridBagConstraints(); // instantiate constraints
// create GUI components
// JTextArea textArea1 = new JTextArea( "TextArea1", 5, 10 );
JLabel DvdName = new JLabel("DVD Name");
JLabel DvdType = new JLabel("DVD Type");
JLabel DvdPrice = new JLabel("DVD Price");
JTextField textField = new JTextField( "" );
JTextField textField2 = new JTextField( "" );
JTextField textField3 = new JTextField( "" );
JButton button1 = new JButton("Add to Database");
JButton button2 = new JButton("Cancel");
constraints.fill = GridBagConstraints.BOTH;
constraints.gridx = 3; // x-Position
constraints.gridy = 1; // y-Position
addComponent( DvdName, 0, 0, 1, 1);
constraints.fill = GridBagConstraints.BOTH;
constraints.gridx = 3; // x-Position
constraints.gridy = 1; // y-Position
addComponent(DvdPrice, 1, 0, 2, 1);
constraints.fill = GridBagConstraints.BOTH;
constraints.gridx = 3; // x-Position
constraints.gridy = 1; // y-Position
addComponent(DvdType, 2, 0, 3, 1);
constraints.fill = GridBagConstraints.BOTH;
constraints.gridx = 3; // x-Position
constraints.gridy = 1; // y-Position
constraints.gridwidth = 4; // Breite
constraints.gridheight = 1;
addComponent(textField, 0, 1, 1, 2);
constraints.fill = GridBagConstraints.HORIZONTAL;
addComponent(button1, 5, 3, 2, 1);
constraints.fill = GridBagConstraints.HORIZONTAL;
addComponent(button2, 5, 5, 3, 1);
}
// method to set constraints on
private void addComponent( Component component,
int row, int column, int width, int height )
{
constraints.gridx = column; // set gridx
constraints.gridy = row; // set gridy
constraints.gridwidth = width; // set gridwidth
constraints.gridheight = height; // set gridheight
layout.setConstraints( component, constraints ); // set constraints
add( component ); // add component
}
}