2017-02-15 116 views
0

我試圖讓按鈕顯示一個JTextField旁邊的Jlabel,但是當我按下時,JLabel不再出現。爲什麼按下按鈕後JLabel不再出現?

萬一它引起了問題,我使用相同的一些盒子和標籤,我希望它們出現在同一個地方,取決於我按下的按鈕。

在單個畫面問題:

https://i.stack.imgur.com/nNKdT.png

import java.awt.Dimension; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 

public class partegrafica extends JFrame{ 
    private static final long serialVersionUID = 1L; 
    JPanel palumno,pprofe,ptutor,ptodo; 
    JButton balumno,brofe,butor,avance,retroceso,guardar; 
    JTextField boxname,boxdni,boxfecha,boxnrp,boxcial,boxcentrodestino,boxespecialidad,boxsalario,boxciclo; 
    JLabel lavacia,lavaciaver, laname,ladni,lafecha,lanrp,lacial,laciclo,lacentrodestino,laespecialidad,lasalario,latitulo; 

    public partegrafica(String[] args) { 
    super("Catalogador de Humanos"); 

    GridBagConstraints cor = new GridBagConstraints(); 
    cor.anchor = GridBagConstraints.NORTHWEST; 
    cor.weightx=1; 
    cor.weighty=1; 

setLayout(new GridBagLayout()); 

ptodo= new JPanel(new GridBagLayout()); 
setPreferredSize(new Dimension(1200, 800)); 
latitulo =new JLabel("Escoge perfil a introducir"); 
cor.gridx=0; 
cor.gridy=0; 
cor.gridheight=1; 
cor.gridwidth=3; 
ptodo.add(latitulo, cor); 
balumno = new JButton("Insertar alumno"); 
cor.gridx=0; 
cor.gridy=1; 
cor.gridheight=1; 
cor.gridwidth=1; 
ptodo.add(balumno, cor); 
brofe = new JButton("Insertar Profesor"); 
cor.gridx=1; 
cor.gridy=1; 
cor.gridheight=1; 
cor.gridwidth=1; 
ptodo.add(brofe, cor); 
butor= new JButton("Insertar Tutor"); 
cor.gridx=2; 
cor.gridy=1; 
cor.gridheight=1; 
cor.gridwidth=1; 
ptodo.add(butor, cor); 
lavacia=new JLabel("                  "); 
cor.gridx=3; 
cor.gridy=1; 
cor.gridheight=1; 
cor.gridwidth=1; 
ptodo.add(lavacia, cor); 
retroceso=new JButton("<"); 
cor.gridx=4; 
cor.gridy=1; 
cor.gridheight=1; 
cor.gridwidth=1; 
ptodo.add(retroceso, cor); 
avance=new JButton(">"); 
cor.gridx=5; 
cor.gridy=1; 
cor.gridheight=1; 
cor.gridwidth=1; 
ptodo.add(avance, cor); 
laname=new JLabel("Nombre"); 
cor.gridx=0; 
cor.gridy=3; 
cor.gridheight=1; 
cor.gridwidth=1; 
ptodo.add(laname, cor); 
boxname = new JTextField(10); 
cor.gridx=1; 
cor.gridy=3; 
cor.gridheight=1; 
cor.gridwidth=1; 
ptodo.add(boxname, cor); 
ladni= new JLabel("DNI"); 
cor.gridx=0; 
cor.gridy=4; 
cor.gridheight=1; 
cor.gridwidth=1; 
ptodo.add(ladni, cor); 
boxdni = new JTextField(10); 
cor.gridx=1; 
cor.gridy=4; 
cor.gridheight=1; 
cor.gridwidth=1; 
ptodo.add(boxdni, cor); 
lafecha= new JLabel("Fecha Nacimiento"); 
cor.gridx=0; 
cor.gridy=5; 
cor.gridheight=1; 
cor.gridwidth=1; 
ptodo.add(lafecha, cor); 
boxfecha= new JTextField(10); 
cor.gridx=1; 
cor.gridy=5; 
cor.gridheight=1; 
cor.gridwidth=1; 
ptodo.add(boxfecha, cor); 
lanrp= new JLabel("NRP"); 
lanrp.setVisible(false); 
cor.gridx=0; 
cor.gridy=6; 
cor.gridheight=1; 
cor.gridwidth=1; 
ptodo.add(lanrp, cor); 
boxnrp= new JTextField(10); 
boxnrp.setVisible(false); 
cor.gridx=1; 
cor.gridy=6; 
cor.gridheight=1; 
cor.gridwidth=1; 
ptodo.add(boxnrp, cor); 
lacial= new JLabel("CIAL"); 
lacial.setVisible(false); 
cor.gridx=0; 
cor.gridy=6; 
cor.gridheight=1; 
cor.gridwidth=1; 
ptodo.add(lacial, cor); 
boxcial= new JTextField(10); 
boxcial.setVisible(false); 
cor.gridx=1; 
cor.gridy=6; 
cor.gridheight=1; 
cor.gridwidth=1; 
ptodo.add(boxcial, cor); 
laespecialidad= new JLabel("Especialidad (nº)"); 
//laespecialidad.setVisible(false); 
cor.gridx=0; 
cor.gridy=7; 
cor.gridheight=1; 
cor.gridwidth=1; 
ptodo.add(laespecialidad, cor); 
boxespecialidad= new JTextField(10); 
//boxespecialidad.setVisible(false); 
cor.gridx=1; 
cor.gridy=7; 
cor.gridheight=1; 
cor.gridwidth=1; 
ptodo.add(boxespecialidad, cor); 
laciclo= new JLabel("Ciclo"); 
//laciclo.setVisible(false); 
cor.gridx=0; 
cor.gridy=7; 
cor.gridheight=1; 
cor.gridwidth=1; 
ptodo.add(laciclo, cor); 
boxciclo=new JTextField(10); 
//boxciclo.setVisible(false); 
cor.gridx=1; 
cor.gridy=7; 
cor.gridheight=1; 
cor.gridwidth=1; 
ptodo.add(boxciclo, cor); 
lacentrodestino= new JLabel("Centro Destino"); 
//lacentrodestino.setVisible(false); 
cor.gridx=0; 
cor.gridy=8; 
cor.gridheight=1; 
cor.gridwidth=1; 
ptodo.add(lacentrodestino, cor); 
boxcentrodestino= new JTextField(10); 
//boxcentrodestino.setVisible(false); 
cor.gridx=1; 
cor.gridy=8; 
cor.gridheight=1; 
cor.gridwidth=1; 
ptodo.add(boxcentrodestino, cor); 
lasalario= new JLabel("Salario:"); 
lasalario.setVisible(true); 
cor.gridx=0; 
cor.gridy=9; 
cor.gridheight=1; 
cor.gridwidth=1; 
ptodo.add(lasalario, cor); 
boxsalario= new JTextField(10); 
//boxsalario.setVisible(false); 
cor.gridx=1; 
cor.gridy=9; 
cor.gridheight=1; 
cor.gridwidth=1; 
ptodo.add(boxsalario, cor); 
guardar= new JButton("Guardar"); 
cor.gridx=2; 
cor.gridy=3; 
cor.gridheight=3; 
cor.gridwidth=1; 
ptodo.add(guardar, cor); 

balumno.addActionListener(new AAlumno()); 
brofe.addActionListener(new AProfe()); 
butor.addActionListener(new ATutor()); 
avance.addActionListener(new Avanzar()); 
retroceso.addActionListener(new Retroceso()); 
guardar.addActionListener(new Guardar()); 

add(ptodo, cor); 

ptodo.setVisible(true); 
pack(); 
} 

    public class AAlumno implements ActionListener{ 
     public void actionPerformed(ActionEvent e){ 

     lacial.setVisible(true); 
     boxcial.setVisible(true); 
     laciclo.setVisible(true); 
     boxciclo.setVisible(true); 

     lanrp.setVisible(false); 
     boxnrp.setVisible(false); 
     laespecialidad.setVisible(false); 
     boxespecialidad.setVisible(false); 
     lacentrodestino.setVisible(false); 
     boxcentrodestino.setVisible(false); 
     lasalario.setVisible(false); 
     boxsalario.setVisible(false); 

     lanrp.setText(""); 
     boxnrp.setText(""); 
     laespecialidad.setText(""); 
     boxespecialidad.setText(""); 
     lacentrodestino.setText(""); 
     boxcentrodestino.setText(""); 
     lasalario.setText(""); 
     boxsalario.setText(""); 
     lacial.setText(""); 
     boxcial.setText(""); 
     laciclo.setText(""); 
     boxciclo.setText(""); 
     validate(); 

     } 
    } 
    public class AProfe implements ActionListener{ 
     public void actionPerformed(ActionEvent e){ 

     lacial.setVisible(false); 
     boxcial.setVisible(false); 
     laciclo.setVisible(false); 
     boxciclo.setVisible(false); 

     lanrp.setVisible(true); 
     boxnrp.setVisible(true); 
     laespecialidad.setVisible(true); 
     boxespecialidad.setVisible(true); 
     lacentrodestino.setVisible(true); 
     boxcentrodestino.setVisible(true); 
     lasalario.setVisible(true); 
     boxsalario.setVisible(true); 
     lacial.setText(""); 
     boxcial.setText(""); 
     laciclo.setText(""); 
     boxciclo.setText(""); 
     lanrp.setText(""); 
     boxnrp.setText(""); 
     laespecialidad.setText(""); 
     boxespecialidad.setText(""); 
     lacentrodestino.setText(""); 
     boxcentrodestino.setText(""); 
     lasalario.setText(""); 
     boxsalario.setText(""); 
     validate(); 
     } 
    } 
    public class ATutor implements ActionListener{ 
     public void actionPerformed(ActionEvent e){ 

     } 
    } 
    public class Avanzar implements ActionListener{ 
     public void actionPerformed(ActionEvent e){ 

     } 
    } 
    public class Retroceso implements ActionListener{ 
     public void actionPerformed(ActionEvent e){ 

     } 
    } 
    public class Guardar implements ActionListener{ 
     public void actionPerformed(ActionEvent e){ 

     } 
    } 
     } 
+0

請考慮顯示您想要實現的內容以及您實際看到的內容。不要擔心無法發佈。如果您嘗試發佈它們,我們可以爲您修復它們以便它們展示。 –

回答

1

我的主要建議是交換的JPanel「意見」用一個JPanel,但如果你確實需要使用上面的代碼,然後不要不交換JLabels,而是使用setText(...)方法替換它們顯示的文本。並且不要讓JLabels可見或不可見,而是通過setText("")給他們沒有文字,如果他們是不可見的。

CardLayout教程可以在這裏找到:CardLayout tutorial

例如與CardLayout:

enter image description here

import java.awt.BorderLayout; 
import java.awt.CardLayout; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Insets; 
import java.awt.event.ActionEvent; 
import java.awt.event.KeyEvent; 
import java.util.HashMap; 
import java.util.Map; 

import javax.swing.*; 

@SuppressWarnings("serial") 
public class SwapViews extends JPanel { 
    public static final String ALUMNO = "Insertar Alumno"; 
    public static final String PROFESSOR = "Insertar Profesor"; 
    public static final String[] ALUMNO_TEXTS = { "Nombre", "DNI", "Fecha Nacimiento", "CIAL", 
      "Ciclo" }; 
    public static final String[] PROFESOR_TEXTS = { "Nombre", "DNI", "Fecha Nacimiento", "NRP", 
      "Especialidad (nº)", "Centro Destino", "Salario:", }; 
    private static final int EB_GAP = 2; 
    private CardLayout cardLayout = new CardLayout(); 
    private JPanel cardHolderPanel = new JPanel(cardLayout); 
    private DataEntryPanel alumnoPanel = new DataEntryPanel(ALUMNO, ALUMNO_TEXTS); 
    private DataEntryPanel profesorPanel = new DataEntryPanel(PROFESSOR, PROFESOR_TEXTS); 

    public SwapViews() { 
     JPanel alumnoWrapperPanel = new JPanel(new BorderLayout()); 
     alumnoWrapperPanel.add(alumnoPanel, BorderLayout.PAGE_START); 

     JPanel profesorWrapperPanel = new JPanel(new BorderLayout()); 
     profesorWrapperPanel.add(profesorPanel, BorderLayout.PAGE_START); 

     cardHolderPanel.add(alumnoWrapperPanel, ALUMNO); 
     cardHolderPanel.add(profesorWrapperPanel, PROFESSOR); 

     JPanel buttonPanel = new JPanel(); 
     buttonPanel.add(new JButton(new ShowCardAction(ALUMNO, KeyEvent.VK_A))); 
     buttonPanel.add(new JButton(new ShowCardAction(PROFESSOR, KeyEvent.VK_P))); 

     setBorder(BorderFactory.createEmptyBorder(EB_GAP, EB_GAP, EB_GAP, EB_GAP)); 
     setLayout(new BorderLayout()); 
     add(cardHolderPanel, BorderLayout.CENTER); 
     add(buttonPanel, BorderLayout.PAGE_END); 
    } 

    private class ShowCardAction extends AbstractAction { 
     public ShowCardAction(String name, int mnemonic) { 
      super(name); 
      putValue(MNEMONIC_KEY, mnemonic); 
     } 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      cardLayout.show(cardHolderPanel, getValue(NAME).toString()); 
     } 
    } 

    private static void createAndShowGui() { 
     SwapViews mainPanel = new SwapViews(); 

     JFrame frame = new JFrame("SwapViews"); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     frame.getContentPane().add(mainPanel); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(() -> createAndShowGui()); 
    } 
} 

@SuppressWarnings("serial") 
class DataEntryPanel extends JPanel { 
    private static final int COLUMNS = 10; 
    private static final Insets WEST_INSETS = new Insets(4, 4, 4, 4); 
    private static final Insets EAST_INSETS = new Insets(4, 10, 4, 4); 
    private String[] labels; 
    private Map<String, JTextField> fieldMap = new HashMap<>(); 

    public DataEntryPanel(String title, String[] labels) { 
     super(new GridBagLayout()); 
     setBorder(BorderFactory.createTitledBorder(title)); 
     this.labels = labels; 
     for (int i = 0; i < labels.length; i++) { 
      String text = labels[i]; 
      JLabel label = new JLabel(text); 
      JTextField textField = new JTextField(COLUMNS); 
      fieldMap.put(text, textField); 

      add(label, getGbc(0, i)); 
      add(textField, getGbc(1, i)); 
     } 
    } 

    public String[] getLabels() { 
     return labels; 
    } 

    public static GridBagConstraints getGbc(int x, int y) { 
     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.gridx = x; 
     gbc.gridy = y; 
     gbc.anchor = x == 0 ? GridBagConstraints.WEST : GridBagConstraints.EAST; 
     gbc.fill = GridBagConstraints.HORIZONTAL; 
     gbc.weightx = 1.0; 
     gbc.weighty = 1.0; 
     gbc.insets = x == 0 ? WEST_INSETS : EAST_INSETS; 
     return gbc; 
    } 

    public String getLabelText(String key) { 
     return fieldMap.get(key).getText(); 
    } 

} 

可能是你的代碼工作不正常,正是因爲你在同一個位置的GridBagLayout添加多個組件。再次保留一組重用的JLabel會更好。

根據我的評論,你還設置了一些JLabel的文本,你設置爲可見的「」,你不想這樣做。仔細檢查您的ActionListeners以瞭解我的意思。

+0

這可能是一個解決方案,我會花費接下來的幾分鐘。然而......我想知道爲什麼這個代碼工作不正常的原因。只是爲了避免未來類似的情況。 –

+0

@ M.M .:可能無法將組件添加到同一個網格佈局位置,而沒有副作用的風險。但不管怎樣,都有更清晰的解決方案請參閱上面的編輯。 –

+0

@ M.M .:你還設置了一些JLabel的文本,你設置爲''「'可見,你不想這樣做。仔細檢查您的ActionListeners以瞭解我的意思。 –

0

我發現標籤消失的原因。我將所有標籤設置爲按鈕內沒有文字。就這樣。一個愚蠢的錯誤。

+0

是的,我也發現了這一點,並在發佈時添加評論。再次校對,校對和校對。 –