Fejlen ser sådan ud:
java.lang.Error: Do not use TestGUI.add() use TestGUI.getContentPane().add()
instead at javax.swing.JFrame.createRootPaneException(JFrame.java:402) at
javax.swing.JFrame.addImpl(JFrame.java:428) at
java.awt.Container.add(Container.java:267) at
TestGUI.<init>(TestGUI.java:53) at TestMain.main(TestMain.java:4)
og her er koden:
import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
import java.awt.event.*;
class TestGUI extends JFrame implements ActionListener {
// Erklæringer af variabler
private GridBagLayout layout;
private GridBagConstraints con;
private ButtonGroup LPT1 = new ButtonGroup();
private ButtonGroup LPT2 = new ButtonGroup();
private ButtonGroup LPT3 = new ButtonGroup();
private JTextField navn = new JTextField();
private JButton generer = new JButton( "Generer fil" );
private JLabel bNavn = new JLabel( "Brugerens navn:" );
private JCheckBox f = new JCheckBox( "F-drevet (BFC)" );
private JCheckBox h = new JCheckBox( "H-drevet (Tegne)" );
private JCheckBox i = new JCheckBox( "I-drevet (Alle)" );
private JCheckBox j = new JCheckBox( "J-drevet (Backup)" );
private JCheckBox k = new JCheckBox( "K-drevet (Misc)" );
private JCheckBox n = new JCheckBox( "N-drevet (OK)" );
// Radioknapper til valg af printer på LPT1
private JRadioButton c1, f1, u1, p1, i1a, i1b;
// Radioknapper til valg af printer på LPT2
private JRadioButton c2, f2, u2, p2, i2a, i2b;
// Radioknapper til valg af printer på LPT3
private JRadioButton c3, f3, u3, p3, i3a, i3b;
// Default Konstruktor
public TestGUI() {
super( "Logon-script Generator" );
// Sætter layoutet til GridBagLayout
layout = new GridBagLayout();
getContentPane().setLayout( layout );
JPanel p = new JPanel();
p.setLayout( new GridLayout( 0, 1 ) );
p.add( bNavn );
p.add( navn );
con = createGBC( 0, 0, 4, 1 );
con.fill = GridBagConstraints.HORIZONTAL;
add( p, con );
createJRadioButton( c1 , "Contracts" , LPT1 );
createJRadioButton( f1 , "Finance" , LPT1 );
createJRadioButton( u1 , "HP_4MV_A3" , LPT1 );
createJRadioButton( p1 , "HP_5_Pla" , LPT1 );
createJRadioButton( i1a, "Inv_modtag", LPT1 );
createJRadioButton( i1b, "Inventory" , LPT1 );
con = createGBC( 3, 4, 1, 1 );
con.fill = GridBagConstraints.VERTICAL;
generer.addActionListener( this );
add( generer, con );
// Placering på skærmen
setLocation( 200, 150 );
// Afslutter programmet ved klik på "X"
addWindowListener( new ApplicationTerminator() );
pack();
setVisible( true );
setResizable( false );
}
// Returnerer placering af komponenten
private GridBagConstraints createGBC( int x, int y,
int width, int height ) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = width;
gbc.gridheight = height;
return gbc;
}
private void createJRadioButton( JRadioButton button,
String text,
ButtonGroup bg ) {
button = new JRadioButton( text );
button.setActionCommand( text );
JPanel radioButtonPanel = new JPanel();
radioButtonPanel.setLayout( new GridLayout(0, 1) );
radioButtonPanel.add( button );
bg.add( button );
radioButtonPanel.setBorder( BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(), "LPT1" ) );
con = createGBC( 1, 2, 1, 1 );
add( radioButtonPanel, con );
}
public void actionPerformed( ActionEvent e ) {
System.out.println( "Der blev trykket på knappen" );
}
}
HJÆÆÆLP
|