Hier ist, was ich versuche, ist eine JPanel-Klasse mit JLabel und JField, und eine JPanel-Klasse mit der Option zum Einfügen von Bildern darin, aber wenn ich die StdPoto-Klasse separat ausführen funktioniert offen JFileChooser und zeigen Bild auf dem JPanel, aber wenn ich es in Testing Klasse laufen, Testing Klasse zeigen nur die Komponenten von StudentData Klasse und StdPhoto fehltWie kann ich Bild JPanel in einem anderen JPanel anzeigen
Fototafel, die in Testing Klasse
anscheinend nichtimport java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.io.File;
public class StdPhoto extends JPanel {
JLabel imageLabel = null;
Icon stImage = null;
JFileChooser fileChooser = null;
FileNameExtensionFilter filter = null;
File fChoose = null;
public StdPhoto() {
setLayout(new BorderLayout());
fileChooser = new JFileChooser();
filter = new FileNameExtensionFilter("Image Files", "jpg", "png", "gif", "jpeg");
fileChooser.setFileFilter(filter);
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int result = fileChooser.showOpenDialog(null);
if(result == JFileChooser.APPROVE_OPTION)
fChoose = fileChooser.getSelectedFile();
String sName = fChoose.getAbsolutePath();
stImage = new ImageIcon(sName);
imageLabel = new JLabel("", stImage, JLabel.CENTER);
add(imageLabel, BorderLayout.CENTER);
setPreferredSize(new Dimension(100, 100));
}
});
}
}
Klasse, die in der Testklasse funktioniert
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class StudentData extends JPanel {
/**
* Create the panel.
*/
public StudentData() {
GridBagLayout gbLayout = new GridBagLayout();
setLayout(gbLayout);
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridheight = 1;
gbc.gridwidth = 10;
gbc.weightx = 0.2;
JLabel stNameLabel = new JLabel("Student Name", SwingConstants.LEFT);
stNameLabel.setFont(new Font("Calibri", Font.PLAIN, 14));
gbLayout.setConstraints(stNameLabel, gbc);
add(stNameLabel);
gbc.gridx = 1;
gbc.gridy = 0;
gbc.gridheight = 1;
gbc.gridwidth = 5;
JTextField stNameField = new JTextField(15);
stNameField.setFont(new Font("Calibri", Font.PLAIN, 14));
gbLayout.setConstraints(stNameField, gbc);
add(stNameField);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridheight = 1;
gbc.gridwidth = 5;
JLabel fNameLabel = new JLabel("Father Name", SwingConstants.LEFT);
fNameLabel.setFont(new Font("Calibri", Font.PLAIN, 14));
gbLayout.setConstraints(fNameLabel, gbc);
add(fNameLabel);
gbc.gridx = 1;
gbc.gridy = 1;
gbc.gridheight = 1;
gbc.gridwidth = 1;
JTextField fNameField = new JTextField(15);
fNameField.setFont(new Font("Calibri", Font.PLAIN, 14));
gbLayout.setConstraints(fNameField, gbc);
add(fNameField);
gbc.gridx = 0;
gbc.gridy = 3;
JLabel pAddress = new JLabel("Permenant Address", SwingConstants.LEFT);
pAddress.setFont(new Font("Calibri", Font.PLAIN, 14));
gbLayout.setConstraints(pAddress, gbc);
add(pAddress);
gbc.gridx = 1;
gbc.gridy = 3;
JTextField pAddressField = new JTextField(15);
pAddressField.setFont(new Font("Calibri", Font.PLAIN, 14));
gbLayout.setConstraints(pAddressField, gbc);
add(pAddressField);
gbc.gridx = 0;
gbc.gridy = 4;
JLabel contactNo = new JLabel("Mobile No.", SwingConstants.LEFT);
contactNo.setFont(new Font("Calibri", Font.PLAIN, 14));
gbLayout.setConstraints(contactNo, gbc);
add(contactNo);
gbc.gridx = 1;
JTextField contactNoField = new JTextField(15);
contactNoField.setFont(new Font("Calibri", Font.PLAIN, 14));
gbLayout.setConstraints(contactNoField, gbc);
add(contactNoField);
}
}
Testing Klasse, wo ich zusammen die beiden Rahmen setzen
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Testing extends JFrame {
public Testing() {
setSize(400, 200);
JPanel jp1 = new JPanel();
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
jp1.setLayout(gbl);
gbc.gridx = 0;
gbc.gridy = 0;
gbl.setConstraints(new StudentData(), gbc);
jp1.add(new StudentData());
gbc.gridx = 1;
gbc.gridy = 0;
gbl.setConstraints(new StdPhoto(), gbc);
jp1.add(new StdPhoto());
add(jp1);
setVisible(true);
}
public static void main(String[] args) {
new Testing();
}
}
Können Sie Ihrem ersten Absatz bitte eine Interpunktion hinzufügen, damit Sie leichter verstehen, was Sie sagen möchten? Es ist zur Zeit schwierig herauszufinden, welche Wörter zu welchem Satz gehören und was sie eigentlich bedeuten. – cello
Das größte Problem ist die Standardgröße Ihrer 'StdPhoto' Komponente ist' 0x0' – MadProgrammer
meinst du setPreferredSize(), setMaximumSize() und setMinimumSize() zu StdPhoto hinzuzufügen, wenn dies nicht funktioniert – user8297824