J
johnny1
Gast
Hi @ all,
ich bin Anfänger in Java und hab ein kleines Problem! Ich hab mir eine Übung vorgenommen mit dem
GridBagLayout und Events! Jedoch möchte ich, dass mein Panel immer in der linken oberen Ecke zu
sehen ist. Wenn ich meine Applikation vertikal sowie auch horizontal vergrößere, verschiebt sich mein
Layout!
Wäre super wenn mir da jemand weiterhelfen könnte oder auch Verbesserungsvorschläge hätte!
ich bin Anfänger in Java und hab ein kleines Problem! Ich hab mir eine Übung vorgenommen mit dem
GridBagLayout und Events! Jedoch möchte ich, dass mein Panel immer in der linken oberen Ecke zu
sehen ist. Wenn ich meine Applikation vertikal sowie auch horizontal vergrößere, verschiebt sich mein
Layout!
Code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class CDLCalc extends JFrame {
JRadioButton[] chs=new JRadioButton[12];
ButtonGroup bandb=new ButtonGroup();
JRadioButton[] kbmb=new JRadioButton[2];
ButtonGroup gkbmb=new ButtonGroup();
JLabel info1=new JLabel("Bitte die Bandbreite auswählen:",JLabel.CENTER);
JLabel info2=new JLabel("Downloadgröße:",JLabel.RIGHT);
JLabel info3=new JLabel("Ungefähre Downloadzeit:",JLabel.RIGHT);
JTextField in=new JTextField(5);
JTextField erg=new JTextField(12);
JButton send=new JButton("Berechnen");
public CDLCalc() {
super("Download Time Calculator");
setSize(325,320);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLookAndFeel();
GridBagLayout gridbag=new GridBagLayout();
GridBagConstraints constraints=new GridBagConstraints();
Container pane=getContentPane();
pane.setLayout(gridbag);
constraints.anchor=GridBagConstraints.WEST;
constraints.fill=GridBagConstraints.NONE;
constraints.insets=new Insets(3,3,3,3);
// Label
buildConstraints(constraints,0,0,5,1,90,0);
gridbag.setConstraints(info1,constraints);
pane.add(info1);
// RadioButtons
chs[0]=new JRadioButton("Modem (56kb/s)");
chs[1]=new JRadioButton("ISDN (64kb/s)");
chs[2]=new JRadioButton("ISDN2 (128kb/s)");
chs[3]=new JRadioButton("DSL-Light (384kb/s)");
chs[4]=new JRadioButton("DSL (768kb/s)");
chs[5]=new JRadioButton("DSL1000 (1024kb/s)");
for(int i=0;i<6;i++) {
buildConstraints(constraints,0,i+1,1,1,0,0);
bandb.add(chs[i]);
gridbag.setConstraints(chs[i],constraints);
pane.add(chs[i]);
}
chs[6]=new JRadioButton("DSL2000 (2048kb/s)");
chs[7]=new JRadioButton("DSL6000 (6016kb/s)");
chs[8]=new JRadioButton("DSL2+ (16.000kb/s)");
chs[9]=new JRadioButton("LAN (10.000kb/s)");
chs[10]=new JRadioButton("LAN (100.000kb/s)");
chs[11]=new JRadioButton("GBLAN (1.000.000kb/s)");
for(int i=6;i<chs.length;i++) {
buildConstraints(constraints,1,i-5,3,1,0,0);
bandb.add(chs[i]);
gridbag.setConstraints(chs[i],constraints);
pane.add(chs[i]);
}
// Eingabe Label
buildConstraints(constraints,0,7,1,1,50,0);
gridbag.setConstraints(info2,constraints);
pane.add(info2);
// Eingabe Downloadgröße
buildConstraints(constraints,1,7,1,1,0,0);
gridbag.setConstraints(in, constraints);
pane.add(in);
// Eingabe Auswahl KB/MB
kbmb[0]=new JRadioButton("KB");
kbmb[1]=new JRadioButton("MB");
gkbmb.add(kbmb[0]);
gkbmb.add(kbmb[1]);
buildConstraints(constraints,2,7,1,1,0,0);
gridbag.setConstraints(kbmb[0],constraints);
pane.add(kbmb[0]);
buildConstraints(constraints,3,7,1,1,0,0);
gridbag.setConstraints(kbmb[1],constraints);
pane.add(kbmb[1]);
// Ausgabe Label
buildConstraints(constraints,0,8,1,1,50,0);
gridbag.setConstraints(info3,constraints);
pane.add(info3);
// Ausgabe Textfeld
buildConstraints(constraints,1,8,3,1,50,0);
erg.setEditable(false);
gridbag.setConstraints(erg,constraints);
pane.add(erg);
// Berechnen Button
constraints.anchor=GridBagConstraints.CENTER;
buildConstraints(constraints,0,9,5,1,90,0);
gridbag.setConstraints(send,constraints);
pane.add(send);
setVisible(true);
}
private void buildConstraints(GridBagConstraints gbc,int gx,int gy,int gw,int gh,
int wx,int wy) {
gbc.gridx=gx;
gbc.gridy=gy;
gbc.gridwidth=gw;
gbc.gridheight=gh;
gbc.weightx=wx;
gbc.weighty=wy;
}
private void setLookAndFeel() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(this);
} catch(Exception e) {
System.err.println("Couldn't use ths system Look and Feel: " + e);
}
}
public static void main(String[] args) {
CDLCalc win=new CDLCalc();
}
}