我正在尝试让按钮在JTextField旁边显示Jlabel,但是当我按下时,JLabel就不再出现了。
如果它导致问题,我使用相同的坐标和一些盒子和标签,我希望它们出现在同一个地方,这取决于我按下的按钮。
单个图片中的问题:
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“视图”,但是如果你绝对需要使用上面的代码,那么不要交换JLabel,而是交换它们显示的文本使用setText(...)
方法。并且不要使JLabel可见或不可见,而是如果它们不可见则不通过setText("")
给它们。
可以在此处找到CardLayout教程:CardLayout tutorial。
例如使用CardLayout:
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,看看我的意思。
答案 1 :(得分:0)
我发现标签消失的原因。我将所有标签设置为按钮内没有文字。这就是全部。一个愚蠢的错误。