Salve ragazzi,
ho un problema. Ho preso una classe dal sito di java ufficiale, in pratica una che aveva dei button disposti in una grid.
Ho modificato la classe a mie esigenze; la lancio e tutto funziona.
Ora però avrei bisogno di integrarla con la mia applicazione.
come posso fare per includerla nella mia applicazione il codice della classe è il seguente:
codice:import java.awt.*; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; public class GridBagLayoutDemo { final static boolean shouldFill = true; final static boolean shouldWeightX = true; final static boolean RIGHT_TO_LEFT = false; public static void addComponentsToPane(Container pane) { if (RIGHT_TO_LEFT) { pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); } JLabel labelCodiceLocalita; JTextField textCodiceLocalita ; JLabel labelOraPartenze; JTextField textOraPartenze; JLabel labelCodice1Nota; JTextField textCodice1Nota; JLabel labelCodice2Nota; JTextField textCodice2Nota; JLabel labelOraArrivoDa; JTextField textOraArrivoDa; JLabel labelOraPartenzaPer; JTextField textOraPartenzaPer; JLabel labelCodice1NotaBis; JTextField textCodice1NotaBis; JLabel labelCodice2NotaBis; JTextField textCodice2NotaBis; JLabel labelOraArrivoPer; JTextField textOraArrivoPer; pane.setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); if (shouldFill) { //natural height, maximum width c.fill = GridBagConstraints.HORIZONTAL; } labelCodiceLocalita = new JLabel("Codice Località: "); if (shouldWeightX) { c.weightx = 0.5; } c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 0; c.gridy = 0; pane.add(labelCodiceLocalita, c); textCodiceLocalita = new JTextField(); if (shouldWeightX) { c.weightx = 0.5; } c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 1; c.gridy = 0; pane.add(textCodiceLocalita, c); labelOraPartenze = new JLabel("Ora Partenza: "); c.fill = GridBagConstraints.HORIZONTAL; c.ipady = 20; //make this component tall c.weightx = 0.0; c.gridwidth = 3; c.gridx = 0; c.gridy = 1; pane.add(labelOraPartenze, c); textOraPartenze = new JTextField(); c.fill = GridBagConstraints.HORIZONTAL; c.ipady = 1; //make this component tall c.weightx = 0.0; c.gridwidth = 3; c.gridx = 1; c.gridy = 1; pane.add(textOraPartenze, c); labelCodice1Nota = new JLabel("Codice 1 nota"); c.fill = GridBagConstraints.HORIZONTAL; c.ipady = 20; c.weightx = 0.0; c.gridwidth = 3; c.gridx = 0; c.gridy = 2; pane.add(labelCodice1Nota, c); textCodice1Nota = new JTextField(); c.fill = GridBagConstraints.HORIZONTAL; c.ipady = 1; c.weightx = 0.0; c.gridwidth = 3; c.gridx = 1; c.gridy = 2; pane.add(textCodice1Nota, c); labelCodice2Nota = new JLabel("Codice 2 Nota: "); c.fill = GridBagConstraints.HORIZONTAL; c.ipady = 20; //make this component tall c.weightx = 0.0; c.gridwidth = 3; c.gridx = 0; c.gridy = 3; pane.add(labelCodice2Nota, c); textCodice2Nota = new JTextField(); c.fill = GridBagConstraints.HORIZONTAL; c.ipady = 1; //make this component tall c.weightx = 0.0; c.gridwidth = 3; c.gridx = 1; c.gridy = 3; pane.add(textCodice2Nota, c); labelOraArrivoDa = new JLabel("Ora arrivo da: "); c.fill = GridBagConstraints.HORIZONTAL; c.ipady = 20; //make this component tall c.weightx = 0.0; c.gridwidth = 3; c.gridx = 0; c.gridy = 4; pane.add(labelOraArrivoDa, c); textOraArrivoDa = new JTextField(); c.fill = GridBagConstraints.HORIZONTAL; c.ipady = 1; //make this component tall c.weightx = 0.0; c.gridwidth = 3; c.gridx = 1; c.gridy = 4; pane.add(textOraArrivoDa, c); labelOraPartenzaPer = new JLabel("Ora partenza per: "); c.fill = GridBagConstraints.HORIZONTAL; c.ipady = 20; //make this component tall c.weightx = 0.0; c.gridwidth = 3; c.gridx = 0; c.gridy = 5; pane.add(labelOraPartenzaPer, c); textOraPartenzaPer = new JTextField(); c.fill = GridBagConstraints.HORIZONTAL; c.ipady = 1; //make this component tall c.weightx = 0.0; c.gridwidth = 3; c.gridx = 1; c.gridy = 5; pane.add(textOraPartenzaPer, c); labelCodice1NotaBis = new JLabel("Codice 1° nota bis: "); c.fill = GridBagConstraints.HORIZONTAL; c.ipady = 20; //make this component tall c.weightx = 0.0; c.gridwidth = 3; c.gridx = 0; c.gridy = 6; pane.add(labelCodice1NotaBis, c); textCodice1NotaBis = new JTextField(); c.fill = GridBagConstraints.HORIZONTAL; c.ipady = 1; //make this component tall c.weightx = 0.0; c.gridwidth = 3; c.gridx = 1; c.gridy = 6; pane.add(textCodice1NotaBis, c); labelCodice2NotaBis = new JLabel("Codice 2° nota bis: "); c.fill = GridBagConstraints.HORIZONTAL; c.ipady = 20; //make this component tall c.weightx = 0.0; c.gridwidth = 3; c.gridx = 0; c.gridy = 7; pane.add(labelCodice2NotaBis, c); textCodice2NotaBis = new JTextField(); c.fill = GridBagConstraints.HORIZONTAL; c.ipady = 1; //make this component tall c.weightx = 0.0; c.gridwidth = 3; c.gridx = 1; c.gridy = 7; pane.add(textCodice2NotaBis, c); labelOraArrivoPer = new JLabel("Ora Arrivo per: "); c.gridx = 0; c.gridy = 8; pane.add(labelOraArrivoPer, c); textOraArrivoPer = new JTextField(); c.fill = GridBagConstraints.HORIZONTAL; c.ipady = 1; //make this component tall c.weightx = 0.0; c.gridwidth = 3; c.gridx = 1; c.gridy = 8; pane.add(textOraArrivoPer, c); } /** * Create the GUI and show it. For thread safety, * this method should be invoked from the * event-dispatching thread. */ private static void createAndShowGUI() { //Create and set up the window. JFrame frame = new JFrame("GridBagLayoutDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Set up the content pane. addComponentsToPane(frame.getContentPane()); //Display the window. frame.pack(); frame.setVisible(true); } public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }
nella mia classe invece ho fatto così
nomeClasseJava demo = new nomeClasseJava();
frame.add(demo);
mi genera un errore in quanto dice che demo non è un componente.
Ricordo che frame è un componente di tipo JFrame.
Mi potete aiutare????



Rispondi quotando

