2013-04-13 87 views
0

Okey,所以我的GUI出現問題。我得到這個錯誤,我從來沒有得到過,當做同樣的事情。我甚至使用舊的代碼。我正在參加考試,所以我更加強調,因爲我不明白這個問題。不可編譯的源代碼java.awt.Container.add

主:http://pastebin.com/Py9S6nd3

import javax.swing.JFrame; 

public class Jonth_SwingTenta { 

    private static void createAndShowGui() { 
     //Skapar ett fönster där gui komponenter läggs. 
     JFrame frame = new JFrame("ToDo"); 
     //Anger vad som ska hända om fönstret stängs, i detta fall avslutas fönstret när man trycker på X. 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     //Skapar ett objekt av klassen FormularPanel (privat lyssnarklass) 
     //GridBagPanel panel = new GridBagPanel(); 
     PanelGUI panel = new PanelGUI(); 
//  frame.setJMenuBar(panel2.getMenu()); 
     //Lägger på panelen på frame. 
     frame.getContentPane().add(panel); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGui(); 
      } 
     }); 
    } 
} 

GUI:http://pastebin.com/TYFG1XdU

import java.awt.*; 
import javax.swing.*; 
import javax.xml.bind.Marshaller.Listener; 

/** 
* 
* @author h12mjont 
*/ 
public class PanelGUI extends JPanel { 

    private JPanel panel; 
    private GridBagConstraints c; 
    private GridBagLayout gridBag; 
    private JLabel variID, varuNamn, varuKat; 
    private JTextField textField1, textField2; 
    private JTextArea textArea1; 
    private JButton laggTill, taBort, uppdatera, sokPaID, sokKat; 
    private Listener listener; 
    private DAOFactory daofactory; 
    private DAOInterface dao; 
    private JComboBox markeBox; 
    String[] marken = {"Frukt", "Mejeri", "Kött"}; 

    public PanelGUI() { 

     panel = new JPanel(); //Skapar en ny panel där alla komponeneter ska finnas. 
     gridBag = new GridBagLayout(); //Skapar en gridbaglayout 
     c = new GridBagConstraints(); //Skapar constraints. 
     panel.setLayout(gridBag); //Sätter layouten. 
     //----------------col 1----------------------------------- 
     variID = new JLabel("Varu ID"); 
     laggTillGUIGridBag(variID, 0, 0, 1, 1); 
     varuNamn = new JLabel("Varunamn"); 
     laggTillGUIGridBag(varuNamn, 1, 0, 1, 1); 
     varuKat = new JLabel("Varukategori"); 
     laggTillGUIGridBag(varuKat, 2, 0, 1, 1); 
     laggTill = new JButton("Lägg till"); 
     laggTillGUIGridBag(laggTill, 3, 0, 1, 1); 
     uppdatera = new JButton("Uppdatera"); 
     laggTillGUIGridBag(uppdatera, 4, 0, 1, 1); 
     sokKat = new JButton("Sök på Kategori"); 
     laggTillGUIGridBag(sokKat, 5, 0, 2, 1); 
     //-----------------col 2-------------------------------- 
     textField1 = new JTextField(); 
     textField1.setPreferredSize(new Dimension(70, 20)); 
     laggTillGUIGridBag(textField1, 0, 1, 2, 1); 
     textField2 = new JTextField(); 
     textField2.setPreferredSize(new Dimension(70, 20)); 
     laggTillGUIGridBag(textField2, 1, 1, 2, 1); 
     markeBox = new JComboBox(); 
     markeBox.setModel(new DefaultComboBoxModel(marken)); 
     // markeBox.addItemListener(combolyssnare); 
     markeBox.setPreferredSize(new Dimension(70, 20)); 
     laggTillGUIGridBag(markeBox, 2, 1, 1, 1); 
     taBort = new JButton("Ta bort"); 
     laggTillGUIGridBag(taBort, 3, 1, 1, 1); 
     sokPaID = new JButton("Sök på id"); 
     laggTillGUIGridBag(sokPaID, 4, 1, 1, 1); 
     //-------------------col 3-------------------------------- 
     textArea1 = new JTextArea(); 
     textArea1.setPreferredSize(new Dimension(350, 100)); 
     textArea1.setMinimumSize(new Dimension(350, 100)); 
     laggTillGUIGridBag(textArea1, 0, 3, 2, 2); 
     //----------------Slut på GUI ---------------------------- 
     add(panel); 
    } 

    private void laggTillGUIGridBag(Component guiKomponent, int rad, int kolumn, int bredd, int hojd) { 
     c.fill = GridBagConstraints.BOTH; //Gör så komponenten fyller ut sig i storlek åt båda håll. 
     c.gridx = kolumn; 
     c.gridy = rad; 
     c.gridwidth = bredd; 
     c.gridheight = hojd; 
     c.insets = new Insets(5, 5, 5, 5); //Sätter padding runt komponenten 
     gridBag.setConstraints(guiKomponent, c); 
     panel.add(guiKomponent, c); 
    } 
} 

我不明白是什麼問題。對不起,它是瑞典語,但你仍然可以理解我在做什麼。

感謝您的幫助!

+0

你能後你做了什麼錯誤? –

+0

你可以嘗試一個乾淨的構建或新的項目。 DAO類是否相關? – trashgod

回答

1

看起來沒有問題,所有setPreferredSize()調用應該是avoided

image

import java.awt.Component; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Insets; 
import javax.swing.DefaultComboBoxModel; 
import javax.swing.JButton; 
import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JTextArea; 
import javax.swing.JTextField; 
import javax.xml.bind.Marshaller.Listener; 

public class Jonth_SwingTenta { 

    private static void createAndShowGui() { 
     JFrame frame = new JFrame("ToDo"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     PanelGUI panel = new PanelGUI(); 
     frame.add(panel); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       createAndShowGui(); 
      } 
     }); 
    } 

    private static class PanelGUI extends JPanel { 

     private JPanel panel; 
     private GridBagConstraints c; 
     private GridBagLayout gridBag; 
     private JLabel variID, varuNamn, varuKat; 
     private JTextField textField1, textField2; 
     private JTextArea textArea1; 
     private JButton laggTill, taBort, uppdatera, sokPaID, sokKat; 
     private Listener listener; 
     private JComboBox markeBox; 
     String[] marken = {"Frukt", "Mejeri", "Kött"}; 

     public PanelGUI() { 

      panel = new JPanel(); 
      gridBag = new GridBagLayout(); 
      c = new GridBagConstraints(); 
      panel.setLayout(gridBag); 
      //----------------col 1----------------------------------- 
      variID = new JLabel("Varu ID"); 
      laggTillGUIGridBag(variID, 0, 0, 1, 1); 
      varuNamn = new JLabel("Varunamn"); 
      laggTillGUIGridBag(varuNamn, 1, 0, 1, 1); 
      varuKat = new JLabel("Varukategori"); 
      laggTillGUIGridBag(varuKat, 2, 0, 1, 1); 
      laggTill = new JButton("Lägg till"); 
      laggTillGUIGridBag(laggTill, 3, 0, 1, 1); 
      uppdatera = new JButton("Uppdatera"); 
      laggTillGUIGridBag(uppdatera, 4, 0, 1, 1); 
      sokKat = new JButton("Sök på Kategori"); 
      laggTillGUIGridBag(sokKat, 5, 0, 2, 1); 
      //-----------------col 2-------------------------------- 
      textField1 = new JTextField(); 
      laggTillGUIGridBag(textField1, 0, 1, 2, 1); 
      textField2 = new JTextField(); 
      laggTillGUIGridBag(textField2, 1, 1, 2, 1); 
      markeBox = new JComboBox(); 
      markeBox.setModel(new DefaultComboBoxModel(marken)); 
      // markeBox.addItemListener(combolyssnare); 
      laggTillGUIGridBag(markeBox, 2, 1, 1, 1); 
      taBort = new JButton("Ta bort"); 
      laggTillGUIGridBag(taBort, 3, 1, 1, 1); 
      sokPaID = new JButton("Sök på id"); 
      laggTillGUIGridBag(sokPaID, 4, 1, 1, 1); 
      //-------------------col 3-------------------------------- 
      textArea1 = new JTextArea(); 
      laggTillGUIGridBag(textArea1, 0, 3, 2, 2); 
      //----------------Slut på GUI ---------------------------- 
      add(panel); 
     } 

     private void laggTillGUIGridBag(Component guiKomponent, 
       int rad, int kolumn, int bredd, int hojd) { 
      c.fill = GridBagConstraints.BOTH; 
      c.gridx = kolumn; 
      c.gridy = rad; 
      c.gridwidth = bredd; 
      c.gridheight = hojd; 
      c.insets = new Insets(5, 5, 5, 5); 
      gridBag.setConstraints(guiKomponent, c); 
      panel.add(guiKomponent, c); 
     } 
    } 
} 
+0

哇你得到它的工作?但我之前使用過setPreferredSize()調用?即時通訊只是使用舊的代碼,現在它不能正常工作?:s' – user2064844

+0

[誤用](http://stackoverflow.com/q/7229226/230513)'setPreferredSize()',雖然很重要,但不會導致您的問題。它可能是您的構建環境的人造物。你可以運行我上面發佈的[sscce](http://sscce.org/)嗎? – trashgod

+0

是的,我可以運行它,但我不知道我是否允許在主類中做到這一點。因爲這就是我們在課堂上如何做到的,儘管我不知道,謝謝! – user2064844